我正在比較兩個影像的差異。
問題是當我從網上下載一些影像時它作業正常,但當我嘗試比較從手機相機捕獲的影像時它不起作用。知道我在哪里做錯了嗎?
我在谷歌 Colab 作業。我嘗試使用 'structural_similarity' 和dilateandfindContours方法,兩者都不適用于相機影像。
我嘗試使用模板匹配然后對齊影像,然后嘗試捕捉差異但仍然得到相同的結果。
正如您在圖片中看到的 - 它顯示了所有 nits n 位差異,但沒有將較大的物件“滑鼠”顯示為差異。
手機拍攝的影像1:

手機拍攝的影像2:

這是我的代碼:
import cv2
from skimage.metrics import structural_similarity
import imutils
ref = cv2.imread('/content/drive/My Drive/Image Comparison/1.png')
target = cv2.imread('/content/drive/My Drive/Image Comparison/2.png')
gray_ref = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY)
gray_compare = cv2.cvtColor(target, cv2.COLOR_BGR2GRAY)
(score, diff) = structural_similarity(gray_ref,gray_compare, full=True)
diff = (diff * 255).astype("uint8")
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(contours)
no_of_differences = 0
for c in contours:
(x, y, w, h) = cv2.boundingRect(c)
rect_area = w*h
if rect_area > 10:
no_of_differences =1
cv2.rectangle(ref, (x, y), (x w, y h), (0, 0, 255), 2)
cv2.rectangle(target, (x, y), (x w, y h), (0, 0, 255), 2)
print("# of differences = ", no_of_differences)
scale_percent = 60 # percent of original size
width = int(ref.shape[1] * scale_percent / 100)
height = int(target.shape[0] * scale_percent / 100)
dim = (width, height)
# resize image
resized_ref = cv2.resize(ref, dim, interpolation = cv2.INTER_AREA)
resized_target = cv2.resize(target, dim, interpolation = cv2.INTER_AREA)
cv2_imshow(resized_ref)
cv2_imshow(resized_target)
cv2.waitKey(0)
uj5u.com熱心網友回復:
您的解決方案有兩個主要問題:
structural_similarity回傳正值和負值(范圍 [-1, 1])。
轉換:diff = (diff * 255).astype("uint8")適用范圍 [0, 1],但對于范圍 [-1, 1],我們可以使用以下轉換:diff = ((diff 1) * 127.5).astype("uint8") # Convert from range [-1, 1] to [0, 255].使用
cv2.threshold自動閾值cv2.THRESH_OTSU不足以對影像進行閾值處理(至少在應用之后structural_similarity)。
我們可以將其替換為
diff = ((diff 1) * 127.5).astype("uint8"):

原文
thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]:
如您所見,大約一半thresh是白色的。結果
thresh = cv2.adaptiveThreshold(diff, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 175, 30):

resized_target:

resized_ref:

備注:
您發布的圖片包含標記,并且尺寸不一樣。
下次嘗試發布干凈的影像而不是螢屏截圖...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/463074.html上一篇:繪制影像出現偏移
