基于单应性矩阵变换的图像拼接融合

单应性矩阵变换

单应性矩阵是一个 3x3 的可逆矩阵,它描述了两个平面之间的投影变换关系。在图像领域,单应性矩阵可以将一幅图像中的点映射到另一幅图像中的对应点,前提是这两幅图像是从不同视角拍摄的同一平面场景。

常见的应用场景:

  • 图像拼接 :将多幅有重叠区域的图像拼接成一幅全景图像。
  • 增强现实 :将虚拟物体正确地投影到现实场景中。
  • 相机位姿估计 :通过已知的三维点和对应的图像点,估计相机的位置和姿态。

在 OpenCV 中,可以使用 cv2.findHomography 函数来计算单应性矩阵。该函数通常结合特征点检测和匹配算法使用,步骤如下:

  1. 特征点检测 :使用 SIFT、SURF、ORB 等算法在两幅图像中检测特征点。
  2. 特征点匹配 :通过特征描述子匹配两幅图像中的特征点,找到对应的点对。
  3. 计算单应性矩阵 :使用匹配的点对调用 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("图像读取失败,请检查文件路径和完整性。")


左侧图片:
1

右侧图片:
2

拼接效果:
3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小龙在山东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值