实现两张图片的接缝线拼接

使用ORB算法检测特征点,并通过BFMatcher进行特征点匹配。然后,根据Lowe's ratio test选择好的匹配点,并使用findHomography计算单应性矩阵。最后,使用warpPerspective将图像进行透视变换,然后将第二张图像粘贴到变换后的图像上。

import cv2
import numpy as np

def find_homography_and_blend(image1, image2, output_path):
    # 读取两张图片
    img1 = cv2.imread(image1)
    img2 = cv2.imread(image2)

    # 转换为灰度图像
    gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
    gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

    # 使用ORB算法检测特征点
    orb = cv2.ORB_create()
    keypoints1, descriptors1 = orb.detectAndCompute(gray1, None)
    keypoints2, descriptors2 = orb.detectAndCompute(gray2, None)

    # 使用BFMatcher进行特征点匹配
    bf = cv2.BFMatcher()
    matches = bf.knnMatch(descriptors1, descriptors2, k=2)

    # 根据Lowe's ratio test选择好的匹配点
    good_matches = []
    for m, n in matches:
        if m.distance < 0.75 * n.distance:
            good_matches.append(m)

    # 获取匹配点的坐标
    src_pts = np.float32([keypoints1[m.queryIdx].pt for m in good_matches]).reshape(-1, 1, 2)
    dst_pts = np.float32([keypoints2[m.trainIdx].pt for m in good_matches]).reshape(-1, 1, 2)

    # 使用findHomography计算单应性矩阵
    homography, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)

    # 使用warpPerspective将图像进行透视变换
    result = cv2.warpPerspective(img1, homography, (img1.shape[1] + img2.shape[1], img1.shape[0]))

    # 将第二张图像粘贴到变换后的图像上
    result[0:img2.shape[0], 0:img2.shape[1]] = img2

    # 保存融合后的图像
    cv2.imwrite(output_path, result)

# 设置两张图片的路径和融合后的输出路径
image_path1 = "path/to/image1.jpg"
image_path2 = "path/to/image2.jpg"
output_path = "path/to/output.jpg"

# 调用融合函数
find_homography_and_blend(image_path1, image_path2, output_path)

相关推荐

  1. 实现图片线拼接

    2023-12-17 07:30:02       67 阅读
  2. LabVIEW实现图像拼接

    2023-12-17 07:30:02       26 阅读
  3. 【Vue】Pdf转图片功能+多图片拼接封装

    2023-12-17 07:30:02       24 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-17 07:30:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-17 07:30:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-17 07:30:02       82 阅读
  4. Python语言-面向对象

    2023-12-17 07:30:02       91 阅读

热门阅读

  1. Webpack5

    2023-12-17 07:30:02       63 阅读
  2. 打包速度优化

    2023-12-17 07:30:02       56 阅读
  3. Python学习之复习MySQL-Day4(DCL)

    2023-12-17 07:30:02       63 阅读
  4. 算法工程师-机器学习面试题总结(7)

    2023-12-17 07:30:02       44 阅读
  5. 2023-12-17 创业日记-创业方向的选择

    2023-12-17 07:30:02       55 阅读
  6. 神经网络基础

    2023-12-17 07:30:02       61 阅读
  7. CDN加速在游戏服务商中的关键作用

    2023-12-17 07:30:02       51 阅读
  8. 《C++20设计模式》---桥接模式学习笔记

    2023-12-17 07:30:02       75 阅读
  9. Ansible如何处理play错误的?Ansible角色?

    2023-12-17 07:30:02       65 阅读
  10. Electron学习第一天 ,启动项目

    2023-12-17 07:30:02       66 阅读