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

opencv-全景图像拼接

运行环境

python3.6 + opencv 3.4.1.15

stitcher.py

import numpy as np
import cv2class Stitcher:#拼接函数def stitch(self, images, ratio=0.75, reprojThresh=4.0,showMatches=False):#获取输入图片(imageB, imageA) = images#检测A、B图片的SIFT关键特征点,并计算特征描述子(kpsA, featuresA) = self.detectAndDescribe(imageA)(kpsB, featuresB) = self.detectAndDescribe(imageB)# 匹配两张图片的所有特征点,返回匹配结果M = self.matchKeypoints(kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh)# 如果返回结果为空,没有匹配成功的特征点,退出算法if M is None:return None# 否则,提取匹配结果# H是3x3视角变换矩阵      (matches, H, status) = M# 将图片A进行视角变换,result是变换后图片result = cv2.warpPerspective(imageA, H, (imageA.shape[1] + imageB.shape[1], imageA.shape[0]))self.cv_show('result', result)# 将图片B传入result图片最左端result[0:imageB.shape[0], 0:imageB.shape[1]] = imageBself.cv_show('result', result)# 检测是否需要显示图片匹配if showMatches:# 生成匹配图片vis = self.drawMatches(imageA, imageB, kpsA, kpsB, matches, status)# 返回结果return (result, vis)# 返回匹配结果return resultdef cv_show(self,name,img):cv2.imshow(name, img)cv2.waitKey(0)cv2.destroyAllWindows()def detectAndDescribe(self, image):# 将彩色图片转换成灰度图gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 建立SIFT生成器descriptor = cv2.xfeatures2d.SIFT_create()# 检测SIFT特征点,并计算描述子(kps, features) = descriptor.detectAndCompute(image, None)# 将结果转换成NumPy数组kps = np.float32([kp.pt for kp in kps])# 返回特征点集,及对应的描述特征return (kps, features)def matchKeypoints(self, kpsA, kpsB, featuresA, featuresB, ratio, reprojThresh):# 建立暴力匹配器matcher = cv2.BFMatcher()# 使用KNN检测来自A、B图的SIFT特征匹配对,K=2rawMatches = matcher.knnMatch(featuresA, featuresB, 2)matches = []for m in rawMatches:# 当最近距离跟次近距离的比值小于ratio值时,保留此匹配对if len(m) == 2 and m[0].distance < m[1].distance * ratio:# 存储两个点在featuresA, featuresB中的索引值matches.append((m[0].trainIdx, m[0].queryIdx))# 当筛选后的匹配对大于4时,计算视角变换矩阵if len(matches) > 4:# 获取匹配对的点坐标ptsA = np.float32([kpsA[i] for (_, i) in matches])ptsB = np.float32([kpsB[i] for (i, _) in matches])# 计算视角变换矩阵(H, status) = cv2.findHomography(ptsA, ptsB, cv2.RANSAC, reprojThresh)# 返回结果return (matches, H, status)# 如果匹配对小于4时,返回Nonereturn Nonedef drawMatches(self, imageA, imageB, kpsA, kpsB, matches, status):# 初始化可视化图片,将A、B图左右连接到一起(hA, wA) = imageA.shape[:2](hB, wB) = imageB.shape[:2]vis = np.zeros((max(hA, hB), wA + wB, 3), dtype="uint8")vis[0:hA, 0:wA] = imageAvis[0:hB, wA:] = imageB# 联合遍历,画出匹配对for ((trainIdx, queryIdx), s) in zip(matches, status):# 当点对匹配成功时,画到可视化图上if s == 1:# 画出匹配对ptA = (int(kpsA[queryIdx][0]), int(kpsA[queryIdx][1]))ptB = (int(kpsB[trainIdx][0]) + wA, int(kpsB[trainIdx][1]))cv2.line(vis, ptA, ptB, (0, 255, 0), 1)# 返回可视化结果return vis

ImageStiching.py

from Stitcher import Stitcher
import cv2# 读取拼接图片
imageA = cv2.imread("left_01.png")
imageB = cv2.imread("right_01.png")# 把图片拼接成全景图
stitcher = Stitcher()
(result, vis) = stitcher.stitch([imageA, imageB], showMatches=True)# 显示所有图片
cv2.imshow("Image A", imageA)
cv2.imshow("Image B", imageB)
cv2.imshow("Keypoint Matches", vis)
cv2.imshow("Result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

 

 

 

 

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

相关文章:

  • 如何将下载的安装包导入PyCharm
  • 【redis问题】Caused by: io.netty.channel
  • Elasticsearch 处理地理信息
  • ARM开发,stm32mp157a-A7核IIC实验(采集温湿度传感器值)
  • 021-从零搭建微服务-短信服务(一)
  • 基于jenkins自动化部署PHP环境
  • 数据库表结构导出为word、html、markdown【转载,已解决,已验证,开源】
  • 【计算机视觉|生成对抗】用于高保真自然图像合成的大规模GAN训练用于高保真自然图像合成的大规模GAN训练(BigGAN)
  • 三维重建_体素重建_空间雕刻法/体素着色法
  • 4-redis哨兵搭建安装
  • 架构评估-架构师之路(十二)
  • 手写模拟SpringBoot核心流程(二):实现Tomcat和Jetty的切换
  • Python土力学与基础工程计算.PDF-土的三项组成
  • 危化安全生产信息化平台在煤化领域的应用
  • Linux(CentOS)运维脚本工具集合
  • 【Java alibabahutool】JSON、Map、实体对象间的相互转换
  • 按软件开发阶段的角度划分:单元测试、集成测试、系统测试、验收测试
  • 【python】Leetcode(primer-dict-list)
  • 网络安全(黑客)入门
  • 在CSS中,盒模型中的padding、border、margin是什么意思?
  • 有线耳机插入电脑没声音
  • 【面试 反思】Retrofit源码与设计 7 连问
  • flutter 雷达图
  • 机器学习之损失函数(Loss Function)
  • 创邻科技张晨:图数据库,激活数据要素的新基建
  • 使用端口映射实现Spring Boot服务端接口的公网远程调试:详细配置与步骤解析
  • stm32之点亮LED
  • SA8000认证的难点及注意事项
  • Java可视化物联网智慧工地SaaS平台源码:人脸识别考勤
  • 告别数字化系统“物理叠加”,华为云推动智慧门店价值跃迁