單應性矩陣變換
單應性矩陣是一個 3x3 的可逆矩陣,它描述了兩個平面之間的投影變換關系。在圖像領域,單應性矩陣可以將一幅圖像中的點映射到另一幅圖像中的對應點,前提是這兩幅圖像是從不同視角拍攝的同一平面場景。
常見的應用場景:
- 圖像拼接 :將多幅有重疊區域的圖像拼接成一幅全景圖像。
- 增強現實 :將虛擬物體正確地投影到現實場景中。
- 相機位姿估計 :通過已知的三維點和對應的圖像點,估計相機的位置和姿態。
在 OpenCV 中,可以使用 cv2.findHomography
函數來計算單應性矩陣。該函數通常結合特征點檢測和匹配算法使用,步驟如下:
- 特征點檢測 :使用 SIFT、SURF、ORB 等算法在兩幅圖像中檢測特征點。
- 特征點匹配 :通過特征描述子匹配兩幅圖像中的特征點,找到對應的點對。
- 計算單應性矩陣 :使用匹配的點對調用
cv2.findHomography
函數計算單應性矩陣。
代碼示例
import cv2
import numpy as npdef stitch_images(img1, img2):# 1. 特征檢測與匹配detector = cv2.SIFT_create()kp1, des1 = detector.detectAndCompute(img1, None)kp2, des2 = detector.detectAndCompute(img2, None)matcher = cv2.BFMatcher()matches = matcher.knnMatch(des1, des2, k=2)# 篩選優質匹配good = []for m, n in matches:if m.distance < 0.75 * n.distance:good.append(m)# 2. 計算單應性矩陣if len(good) >= 4:src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)H, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)else:print("匹配點不足,無法計算單應性矩陣")return None# 計算變換后圖像的四個角點h1, w1 = img1.shape[:2]h2, w2 = img2.shape[:2]pts1 = np.float32([[0, 0], [0, h1], [w1, h1], [w1, 0]]).reshape(-1, 1, 2)pts2 = np.float32([[0, 0], [0, h2], [w2, h2], [w2, 0]]).reshape(-1, 1, 2)pts1_transformed = cv2.perspectiveTransform(pts1, H)# 合并所有角點pts = np.concatenate((pts2, pts1_transformed), axis=0)# 找到新圖像的邊界[x_min, y_min] = np.int32(pts.min(axis=0).ravel() - 0.5)[x_max, y_max] = np.int32(pts.max(axis=0).ravel() + 0.5)# 調整單應性矩陣以補償偏移translation = np.array([[1, 0, -x_min], [0, 1, -y_min], [0, 0, 1]], dtype=np.float32)H = translation.dot(H)# 3. 透視變換result = cv2.warpPerspective(img1, H, (x_max - x_min, y_max - y_min))result[-y_min:h2 - y_min, -x_min:w2 - x_min] = img2# 6. 裁剪黑色邊緣gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)_, thresh = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)if len(contours) > 0:x, y, w, h = cv2.boundingRect(contours[0])result = result[y:y + h, x:x + w]return result# 使用示例
if __name__ == "__main__":img1 = cv2.imread("left.jpg")img2 = cv2.imread("right.jpg")if img1 is not None and img2 is not None:panorama = stitch_images(img1, img2)if panorama is not None:cv2.imwrite("result.jpg", panorama)else:print("圖像讀取失敗,請檢查文件路徑和完整性。")
左側圖片:
右側圖片:
拼接效果: