Panorama Stitching with OpenCV: SIFT Feature Detection and RANSAC
This article explains how to create a panoramic image by detecting SIFT keypoints, matching features with KNN, estimating a homography using RANSAC, and warping the images with OpenCV, providing full Python code and step‑by‑step instructions.
Image panorama stitching combines two overlapping images into a single wide view using computer‑vision techniques such as SIFT keypoint detection, feature matching, RANSAC homography estimation and perspective warping.
Steps
(1) Detect SIFT keypoints and extract local invariant descriptors for both left and right images. (2) Match the descriptors with a k‑nearest‑neighbors matcher and filter matches using the ratio test. (3) Compute the homography matrix H from the matched points with RANSAC. (4) Warp the right image with cv.warpPerspective using H and blend the left image onto the warped result to obtain the final panorama.
Code
<code>import cv2 as cv # import OpenCV
import numpy as np # import NumPy for matrix operations
# Detect SIFT keypoints
def sift_keypoints_detect(image):
gray_image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
sift = cv.xfeatures2d.SIFT_create()
keypoints, features = sift.detectAndCompute(image, None)
keypoints_image = cv.drawKeypoints(
gray_image, keypoints, None, flags=cv.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS)
return keypoints_image, keypoints, features
# Match features with KNN
def get_feature_point_ensemble(features_right, features_left):
bf = cv.BFMatcher()
matches = bf.knnMatch(features_right, features_left, k=2)
matches = sorted(matches, key=lambda x: x[0].distance / x[1].distance)
good = []
for m, n in matches:
ratio = 0.6
if m.distance < ratio * n.distance:
good.append(m)
return good
# Panorama stitching
def Panorama_stitching(image_right, image_left):
_, kp_r, f_r = sift_keypoints_detect(image_right)
_, kp_l, f_l = sift_keypoints_detect(image_left)
goodMatch = get_feature_point_ensemble(f_r, f_l)
if len(goodMatch) > 4:
ptsR = np.float32([kp_r[m.queryIdx].pt for m in goodMatch]).reshape(-1,1,2)
ptsL = np.float32([kp_l[m.trainIdx].pt for m in goodMatch]).reshape(-1,1,2)
ransacReprojThreshold = 4
Homography, status = cv.findHomography(ptsR, ptsL, cv.RANSAC, ransacReprojThreshold)
Panorama = cv.warpPerspective(image_right, Homography,
(image_right.shape[1] + image_left.shape[1],
image_right.shape[0]))
Panorama[0:image_left.shape[0], 0:image_left.shape[1]] = image_left
return Panorama
if __name__ == '__main__':
image_left = cv.imread("./Left.jpg")
image_right = cv.imread("./Right.jpg")
image_right = cv.resize(image_right, None, fx=0.4, fy=0.24)
image_left = cv.resize(image_left, (image_right.shape[1], image_right.shape[0]))
# Additional display and saving steps can be added here
</code>Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.