import cv2 as cv import numpy as np from glob import glob # the directory of the image database database_dir = "image.orig" def is_same_image_sift(img1_path, img2_path): # Load images img1 = cv.imread(img1_path) img2 = cv.imread(img2_path) if img1 is None or img2 is None: print("Error loading images!") return False # Detect keypoints and compute descriptors minHessian = 400 detector = cv.SIFT_create() keypoints1, descriptors1 = detector.detectAndCompute(img1, None) keypoints2, descriptors2 = detector.detectAndCompute(img2, None) # Match descriptors matcher = cv.DescriptorMatcher_create(cv.DescriptorMatcher_BRUTEFORCE) matches = matcher.match(descriptors1, descriptors2) # Filter out low-quality matches matches = sorted(matches, key=lambda x: x.distance) min_dist = matches[0].distance good_matches = tuple(filter(lambda x: x.distance <= 2 * min_dist, matches)) # Calculate similarity or distance if len(good_matches) > 0: # You can calculate the similarity or distance based on the number of good matches similarity = len(good_matches) / len(matches) print(f"Similarity: {similarity}") return similarity > 0.5 else: print("No good matches found.") return False def main(): # img = cv.imread("beach.jpg") # cv.imshow("Image", img) # from matplotlib import pyplot as plt # plt.hist(img.ravel(),10,[0,256]); plt.show() # gray_img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # cv.imshow("Gray Image", gray_img) # cv.waitKey() # Example usage img1_path = "test_img/flower.jpg" img2_path = "image.orig/684.jpg" if is_same_image_sift(img1_path, img2_path): print("Images are likely the same.") else: print("Images are likely different.") main()