用于查找差异图像的 opencv 阈值问题

opencv threshold problem for finding diff images

我有 2 张如下图。我想让其中两个有差异。

我尝试了一些带有阈值的代码。但没有门槛不同。两幅图像阈值图像全黑。如何区分阈值上的白色?

(区别就在左上角,像个小黑点)

before = cv2.imread('1.png')
after = cv2.imread('2.png')

threshb = cv2.threshold(before, 0, 255, cv2.THRESH_BINARY_INV)[1]
thresha = cv2.threshold(after, 0, 255, cv2.THRESH_BINARY_INV)[1]

cv.imshow("before",threshb)
cv.imshow("after",thresha)

不是:我用“structural_similarity”link here来寻找差异,但它发现了很多差异:(

我不需要小的像素差异。我需要人眼所见的差异。

处理这个问题的方法是先做 absdiff,然后在 Python/OpenCV 中获得一些增益。当您对这些图像进行 absdiff 时,差异很小,因此不会比黑色高出太多。所以你必须增加增益才能看到差异。 (或者你可以设置一个合适的阈值)

输入 1:

输入 2:

import cv2
import numpy as np
from skimage import exposure as exposure

# read image 1
img1 = cv2.imread('gray1.png', cv2.IMREAD_GRAYSCALE)

# read image 2
img2 = cv2.imread('gray2.png', cv2.IMREAD_GRAYSCALE)

# do absdiff
diff = cv2.absdiff(img1,img2)

# apply gain
result1 = cv2.multiply(diff, 5)

# or do threshold
result2 = cv2.threshold(diff, 10, 255, cv2.THRESH_BINARY)[1]

# save result
cv2.imwrite('gray1_gray2_diff1.png', result1)
cv2.imwrite('gray1_gray2_diff2.png', result2)

# display result
cv2.imshow('diff', diff)
cv2.imshow('result1', result1)
cv2.imshow('result2', result2)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果 1(增益):

结果 2(阈值):