如果 absdiff() 相同图像(只是稍微垂直移动)差异 > 则正确15%?
如果我有 2 个几乎完全相同的图像(除了其中一个图像的所有像素向上移动 2 个像素),并且我使用 cv2.absdiff() 比较它们,则差异 > > 15%。这对我来说听起来不太合适?
我本以为差距会小很多。这是正确的吗?还是我的数学不正确?
以下是我的代码:
def calculate_difference_measure():
# Load in same image twice
img1 = cv2.imread('./i1.png')
img2 = img1.copy()
# Translate one image slightly up
img2 = imutils.translate(img2, 0, 2)
# Difference the 2 images and get all values below 15
diff = cv2.absdiff(img1, img2)
gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
thresh_diff = cv2.threshold(gray_diff, 15, 255, cv2.THRESH_BINARY)[1]
# Calculate the difference between the 2 images
total_pixels = img1.shape[0] * img1.shape[1] * 1.0
diff_on_pixels = cv2.countNonZero(thresh_diff) * 1.0
difference_measure = diff_on_pixels / total_pixels
# Outputs 'difference_measure: 0.1768186919702071'
print('difference_measure: {}'.format(difference_measure))
If I have 2 images that are pretty much exactly the same (except one image has all its pixels shifted 2 pixels up) and I compare them using cv2.absdiff()
the difference is > 15%. That doesn't sound right to me?
I was thinking the difference would be much lower. Is this correct? Or is my maths incorrect?
Below is my code:
def calculate_difference_measure():
# Load in same image twice
img1 = cv2.imread('./i1.png')
img2 = img1.copy()
# Translate one image slightly up
img2 = imutils.translate(img2, 0, 2)
# Difference the 2 images and get all values below 15
diff = cv2.absdiff(img1, img2)
gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
thresh_diff = cv2.threshold(gray_diff, 15, 255, cv2.THRESH_BINARY)[1]
# Calculate the difference between the 2 images
total_pixels = img1.shape[0] * img1.shape[1] * 1.0
diff_on_pixels = cv2.countNonZero(thresh_diff) * 1.0
difference_measure = diff_on_pixels / total_pixels
# Outputs 'difference_measure: 0.1768186919702071'
print('difference_measure: {}'.format(difference_measure))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,据我从 cv2.absdiff 的文档中了解到,它计算像素的差异。为了形象化,我创建了一个棋盘并移动了 1 行 - 这导致了 100% 的差异。
Yes, as far as I understood from the documentation of cv2.absdiff, it calculates the difference of pixels. to visualize i created a chessboard and shifted 1 row - that resulted in 100% difference.