I have some images. In that some images are cropped versions.
like here is the original image big image
and the cropped image small image.
Note that the shape(resolution) of images are not same.
I have a few pairs like this. The original images are kept in a folder and the cropped images are kept in another folder.
Ultimately I want to find the pairs of original image and cropped image from these images.
So I want to iterate to the images from both folders and check if the cropped image is a part of bigger image or not.
But I can’t find any algorithm that giving such results with images of different shapes(resolutions).
I have already tried cv2.matchTemplate
and skimage.metrics.structural_similarity
but they are only working for images of similar shape(resolution).
Finds similarities between two images using SIFT. Retrieves well-matched points and uses these points to identify the cut region in the original image
import cv2
import numpy as np
image1 = cv2.imread('img1.jpg')
image2 = cv2.imread('img2.jpg')
sift = cv2.SIFT_create()
kp1, des1 = sift.detectAndCompute(image1, None)
kp2, des2 = sift.detectAndCompute(image2, None)
bf = cv2.BFMatcher()
matches = bf.knnMatch(des1, des2, k=2)
good_matches = [m for m, n in matches if m.distance < 0.75 * n.distance]
if len(good_matches) > 50:
src_pts = np.float32([kp1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2)
dst_pts = np.float32([kp2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2)
M, _ = cv2.findHomography(dst_pts, src_pts, cv2.RANSAC, 5.0)
h, w = image2.shape[:2]
pts = np.float32([[0, 0], [0, h-1], [w-1, h-1], [w-1, 0]]).reshape(-1, 1, 2)
dst = cv2.perspectiveTransform(pts, M)
image1 = cv2.polylines(image1, [np.int32(dst)], True, (0, 255, 0), 3)
cv2.imshow('Result', cv2.resize(image1, (0, 0), fx=0.5, fy=0.5))
cv2.waitKey(0)
cv2.destroyAllWindows()
Result