Image Stitching
Module: Computer Vision | Difficulty: Intermediate
Homography Estimation
RANSAC for Homography
- Sample 4 point correspondences
- Compute homography
- Count inliers where
- Keep with most inliers
Multi-Band Blending
where is the Gaussian pyramid and is the Laplacian pyramid.
Cylindrical Projection
import cv2
import numpy as np
def stitch_images(img1, img2):
orb = cv2.ORB_create(nfeatures=1000)
kp1, des1 = orb.detectAndCompute(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY), None)
kp2, des2 = orb.detectAndCompute(cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY), None)
bf = cv2.BFMatcher(cv2.NORM_HAMMING)
matches = bf.knnMatch(des1, des2, k=2)
good = [m for m, n in matches if m.distance < 0.75 * n.distance]
pts1 = np.float32([kp1[m.queryIdx].pt for m in good])
pts2 = np.float32([kp2[m.trainIdx].pt for m in good])
H, mask = cv2.findHomography(pts1, pts2, cv2.RANSAC, 5.0)
h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]
result = cv2.warpPerspective(img1, H, (w1 + w2, max(h1, h2)))
result[0:h2, 0:w2] = img2
return result
Key Takeaways
- RANSAC robustly estimates homography despite outliers
- Multi-band blending creates seamless transitions
- Cylindrical projection handles wide field-of-view panoramas