当前位置: 首页 > news >正文

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

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

import cv2
import numpy as npdef 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)

http://www.lryc.cn/news/261453.html

相关文章:

  • 基于JNI 实现 嵌套 List 类型参数解析
  • 探索灵活性与可维护性的利器:策略(Strategy)模式详解
  • 压缩包文件暴力破解 -Server2005(解析)
  • mars3d加载arcgis发布的服务,⽀持4523坐标
  • 『K8S 入门』二:深入 Pod
  • 十七、如何将MapReduce程序提交到YARN运行
  • 华为云CodeArts Deploy常见问答汇总
  • 前后端交互—开发一个完整的服务器
  • 前端框架的虚拟DOM(Virtual DOM)
  • 什么是http状态码?
  • linux/CentOS 7安装Nginx
  • 软件工程期末复习+数据仓库ETL
  • 学习C语言——体会计算机中的0和1
  • PyTorch官网demo解读——第一个神经网络(1)
  • 升华 RabbitMQ:解锁一致性哈希交换机的奥秘【RabbitMQ 十】
  • vue3 element-plus 日期选择器 el-date-picker 汉化
  • 剑指 Offer(第2版)面试题 35:复杂链表的复制
  • 自定义指令Custom Directives
  • 预测性维护对制造企业设备管理的作用
  • 华为、新华三、锐捷常用命令总结
  • 链路追踪详解(四):分布式链路追踪的事实标准 OpenTelemetry 概述
  • Node.js 工作线程与子进程:应该使用哪一个
  • python matplotlib 三维图形添加文字且不随图形变动而变动
  • Ubuntu设置kubelet启动脚本关闭swap分区
  • MySQL数据库存储
  • verilog语法进阶,时钟原语
  • 案例069:基于微信小程序的计算机实验室排课与查询系统
  • C语言:将三个数从大到小输出
  • 基于Hadoop的铁路货运大数据平台设计与应用
  • Java基础题2:类和对象