How to Stitch Panoramic Images with OpenCV: SIFT, RANSAC, and Homography
This article explains the complete workflow for creating a panoramic image by detecting SIFT keypoints, matching them with KNN, filtering matches using RANSAC, computing a homography matrix, and finally warping and blending the two overlapping photos using OpenCV in Python.
Reading font
Basic Introduction
Panoramic image stitching, also called "image stitching," merges two overlapping photos into a single wide‑view picture. The process relies on computer‑vision techniques such as keypoint detection, local invariant features, keypoint matching, RANSAC (Random Sample Consensus), and perspective transformation.
Specific Steps
Detect SIFT keypoints and extract local invariant features from the left and right images.
Use knnMatch to match SIFT features between the two images.
Compute the perspective transformation matrix H and warp the right image.
Place the left image on the appropriate side of the warped image to obtain the final panorama.
Code
import cv2 as cv # import OpenCV package
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 using 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 = []
ratio = 0.6
for m, n in matches:
if m.distance < ratio * n.distance:
good.append(m)
return good
# Compute homography and warp image
def Panorama_stitching(image_right, image_left):
_, kp_right, feat_right = sift_keypoints_detect(image_right)
_, kp_left, feat_left = sift_keypoints_detect(image_left)
goodMatch = get_feature_point_ensemble(feat_right, feat_left)
if len(goodMatch) > 4:
ptsR = np.float32([kp_right[m.queryIdx].pt for m in goodMatch]).reshape(-1, 1, 2)
ptsL = np.float32([kp_left[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]))
# Show keypoint detection results
kp_img_right, kp_right, feat_right = sift_keypoints_detect(image_right)
kp_img_left, kp_left, feat_left = sift_keypoints_detect(image_left)
cv.imshow('Left Image Keypoints', np.hstack((image_left, kp_img_left)))
cv.waitKey(0)
cv.destroyAllWindows()
cv.imshow('Right Image Keypoints', np.hstack((image_right, kp_img_right)))
cv.waitKey(0)
cv.destroyAllWindows()
# Draw all matches
goodMatch = get_feature_point_ensemble(feat_right, feat_left)
all_goodmatch_image = cv.drawMatches(
image_right, kp_right, image_left, kp_left, goodMatch, None, None, None, None, flags=2)
cv.imshow('All SIFT Matches', all_goodmatch_image)
cv.waitKey(0)
cv.destroyAllWindows()
# Stitch and save panorama
Panorama = Panorama_stitching(image_right, image_left)
cv.namedWindow('Panorama', cv.WINDOW_AUTOSIZE)
cv.imshow('Panorama', Panorama)
cv.imwrite('./panorama.jpg', Panorama)
cv.waitKey(0)
cv.destroyAllWindows()Result Images
For questions about attribution, corrections or content rights, please contactand we will review it promptly.