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()