import cv2def main():image1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE)image2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE)surf = cv2.xfeatures2d.SURF_create()keypoints1, descriptors1 = surf.detectAndCompute(image1, None)keypoints2, descriptors2 = surf.detectAndCompute(image2, None)result_image1 = cv2.drawKeypoints(image1, keypoints1, None, (0, 255, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)result_image2 = cv2.drawKeypoints(image2, keypoints2, None, (0, 255, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)cv2.imshow("Image 1", result_image1)cv2.imshow("Image 2", result_image2)cv2.waitKey(0)cv2.destroyAllWindows()if __name__ == "__main__":main()
import cv2
import numpy as npdef main():image1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE)image2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE)surf = cv2.xfeatures2d.SURF_create()keypoints1, descriptors1 = surf.detectAndCompute(image1, None)keypoints2, descriptors2 = surf.detectAndCompute(image2, None)matcher = cv2.DescriptorMatcher_create(cv2.DescriptorMatcher_FLANNBASED)matches = matcher.match(descriptors1, descriptors2)matches = sorted(matches, key=lambda x: x.distance)good_matches = matches[:10]result_image = cv2.drawMatches(image1, keypoints1, image2, keypoints2, good_matches, None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)cv2.imshow("Matches", result_image)cv2.waitKey(0)cv2.destroyAllWindows()if __name__ == "__main__":main()