Check image dissimilar areas using SIFT
I want to see which areas in image are dissimilar. Attaching 2 images in which there is a difference in top left part. It shouldn't show the difference if some part is scaled, translated or skewed. I am trying to use SIFT matching to see the non matched points. .
import cv2
import numpy as np
from matplotlib import pyplot as plt
from scipy.spatial.distance import euclidean
img1 = cv2.imread('plant.png')
img2 = cv2.imread('plantErase.png')
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
sift = cv2.SIFT_create()
kp1, des1 = sift.detectAndCompute(gray1, None)
kp2, des2 = sift.detectAndCompute(gray2, None)
im_with_keypoints1 = cv2.drawKeypoints(gray1, kp1, np.array([]))
im_with_keypoints2 = cv2.drawKeypoints(gray2, kp2, np.array([]))
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1,des2,k=2)
MIN_MATCH_COUNT = 10
good = []
for m,n in matches:
if m.distance < 0.7*n.distance:
good.append(m)
if len(good)>MIN_MATCH_COUNT:
src_pts = np.float32([ kp1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
dst_pts = np.float32([ kp2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
kp1_matched=([ kp1[m.queryIdx] for m in good ])
kp2_matched=([ kp2[m.trainIdx] for m in good ])
kp1_miss_matched=[kp for kp in kp1 if kp not in kp1_matched]
kp2_miss_matched=[kp for kp in kp2 if kp not in kp2_matched]
img1_miss_matched_kp = cv2.drawKeypoints(img1,kp1_miss_matched, None,color=(255,0,0), flags=0)
plt.imshow(img1_miss_matched_kp),plt.show()
img2_miss_matched_kp = cv2.drawKeypoints(img2,kp2_miss_matched, None,color=(255,0,0), flags=0)
plt.imshow(img2_miss_matched_kp),plt.show()
and what is your actual question here ?
The non-matched keypoints are not enough to identify which area is different. Ratio of matched and non matched keypoints is almost 1. Ss, I wanted to know how to identify which area is different irrespective of translation/skewing/rotation of some part.
I am new to opencv. I would be thankful if you could provide any resources in this direction.
this is no surprise, it's not meant to be used like that
maybe try to find a homography between the 2 images, using your SIFT matching, then warp the 2nd image to the 1st and use a straight absdiff() to find differences
Do you mean homography b/w a particular part of both pictures? Could you tell me what module to use to warp? And if absdiff will ignore the parts that are skewed/rotated/translated?
will there be parts in the image that are skewed/rotated/translated? or one whole image moved wrt. the other ? have another look at the tutorials if it'S the latter, you could align both images. using this
@berak. That tutorial doesn't accurately. The newest OpenCV 4.5.0 version will get u nicely accurately.
what ? try to write a complete emglish sentence, not garbage ...
Yes, parts of the image will be changed. I understand the latter can be solved by absdiff().
did you try someting like this
Hi @strukmen, this would show the difference even if say, a rectangle in the whole diagram is moved a little bit to the right when in reality, this is not a difference.