import cv2 import numpy as np # Load the images img1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE) img2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE) # Initialize the KAZE feature detector kaze = cv2.KAZE_create() # Find the KAZE features and descriptors in both images keypoints1, descriptors1 = kaze.detectAndCompute(img1, None) keypoints2, descriptors2 = kaze.detectAndCompute(img2, None) # Create a brute-force matcher with cross-checking enabled bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True) # Match the descriptors using the brute-force matcher matches = bf.match(descriptors1, descriptors2) # Apply RANSAC to filter out incorrect matches src_pts = np.float32([keypoints1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2) dst_pts = np.float32([keypoints2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) # Draw the inliers (correct matches) on a new image matchesMask = mask.ravel().tolist() draw_params = dict(matchColor = (0, 255, 0), singlePointColor = None, matchesMask = matchesMask, flags = 2) img3 = cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches, None, **draw_params) # Show the image with inliers cv2.imshow('Inliers', img3) cv2.waitKey(0) cv2.destroyAllWindows()