OpenCV: implementing Image Stitching and Keypoint Matching Using ORB
This might be a silly question, but I'm trying to stitch two images together using OpenCV's ORB feature detector, but I'm running into issues where the keypoints do not seem to match correctly, resulting in a distorted stitched image. I've been using OpenCV version 4.5.3. Here's the relevant part of my code: ```python import cv2 import numpy as np # Load the images img1 = cv2.imread('image1.jpg') img2 = cv2.imread('image2.jpg') # Initialize ORB detector orb = cv2.ORB_create() # Find keypoints and descriptors kp1, des1 = orb.detectAndCompute(img1, None) kp2, des2 = orb.detectAndCompute(img2, None) # Create a BFMatcher object bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) # Match descriptors matches = bf.match(des1, des2) # Sort them in ascending order of distance matches = sorted(matches, key=lambda x: x.distance) # Draw the matches img_matches = cv2.drawMatches(img1, kp1, img2, kp2, matches[:10], None, flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS) # Show matched images cv2.imshow('Matches', img_matches) cv2.waitKey(0) cv2.destroyAllWindows() ``` Despite the code running without errors, the stitched result appears misaligned and does not resemble a coherent panorama. I've experimented with adjusting the number of matches used and have tried filtering out good matches using a ratio test, but it hasn't yielded better results. I've also considered switching to SIFT or SURF for potentially better performance, but I want to first resolve the ORB issues. Is there something I'm missing here in terms of preprocessing the images or adjusting the matching parameters? Any guidance would be appreciated! Any help would be greatly appreciated! I'm on Windows 11 using the latest version of Python.