单应性矩阵变换
单应性矩阵是一个 3x3 的可逆矩阵,它描述了两个平面之间的投影变换关系。在图像领域,单应性矩阵可以将一幅图像中的点映射到另一幅图像中的对应点,前提是这两幅图像是从不同视角拍摄的同一平面场景。
常见的应用场景:
- 图像拼接 :将多幅有重叠区域的图像拼接成一幅全景图像。
- 增强现实 :将虚拟物体正确地投影到现实场景中。
- 相机位姿估计 :通过已知的三维点和对应的图像点,估计相机的位置和姿态。
在 OpenCV 中,可以使用 cv2.findHomography
函数来计算单应性矩阵。该函数通常结合特征点检测和匹配算法使用,步骤如下:
- 特征点检测 :使用 SIFT、SURF、ORB 等算法在两幅图像中检测特征点。
- 特征点匹配 :通过特征描述子匹配两幅图像中的特征点,找到对应的点对。
- 计算单应性矩阵 :使用匹配的点对调用
cv2.findHomography
函数计算单应性矩阵。
代码示例
import cv2
import numpy as np
def 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("图像读取失败,请检查文件路径和完整性。")
左侧图片:
右侧图片:
拼接效果: