python实现图像拼接


Posted in Python onMarch 05, 2020

本文实例为大家分享了python实现图像拼接的具体代码,供大家参考,具体内容如下

1.待拼接的图像

python实现图像拼接

python实现图像拼接

2. 基于SIFT特征点和RANSAC方法得到的图像特征点匹配结果

python实现图像拼接

3.图像变换结果

python实现图像拼接

4.代码及注意事项

import cv2
import numpy as np
 
 
def cv_show(name, image):
 cv2.imshow(name, image)
 cv2.waitKey(0)
 cv2.destroyAllWindows()
 
 
def detectAndCompute(image):
 image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 sift = cv2.xfeatures2d.SIFT_create()
 (kps, features) = sift.detectAndCompute(image, None)
 kps = np.float32([kp.pt for kp in kps]) # 得到的点需要进一步转换才能使用
 return (kps, features)
 
 
def matchKeyPoints(kpsA, kpsB, featuresA, featuresB, ratio = 0.75, reprojThresh = 4.0):
 # ratio是最近邻匹配的推荐阈值
 # reprojThresh是随机取样一致性的推荐阈值
 matcher = cv2.BFMatcher()
 rawMatches = matcher.knnMatch(featuresA, featuresB, 2)
 matches = []
 for m in rawMatches:
  if len(m) == 2 and m[0].distance < ratio * m[1].distance:
   matches.append((m[0].queryIdx, m[0].trainIdx))
 kpsA = np.float32([kpsA[m[0]] for m in matches]) # 使用np.float32转化列表
 kpsB = np.float32([kpsB[m[1]] for m in matches])
 (M, status) = cv2.findHomography(kpsA, kpsB, cv2.RANSAC, reprojThresh)
 return (M, matches, status) # 并不是所有的点都有匹配解,它们的状态存在status中
 
 
def stich(imgA, imgB, M):
 result = cv2.warpPerspective(imgA, M, (imgA.shape[1] + imgB.shape[1], imgA.shape[0]))
 result[0:imageA.shape[0], 0:imageB.shape[1]] = imageB
 cv_show('result', result)
 
 
def drawMatches(imgA, imgB, kpsA, kpsB, matches, status):
 (hA, wA) = imgA.shape[0:2]
 (hB, wB) = imgB.shape[0:2]
 # 注意这里的3通道和uint8类型
 drawImg = np.zeros((max(hA, hB), wA + wB, 3), 'uint8')
 drawImg[0:hB, 0:wB] = imageB
 drawImg[0:hA, wB:] = imageA
 for ((queryIdx, trainIdx),s) in zip(matches, status):
  if s == 1:
   # 注意将float32 --> int
   pt1 = (int(kpsB[trainIdx][0]), int(kpsB[trainIdx][1]))
   pt2 = (int(kpsA[trainIdx][0]) + wB, int(kpsA[trainIdx][1]))
   cv2.line(drawImg, pt1, pt2, (0, 0, 255))
 cv_show("drawImg", drawImg)
 
 
# 读取图像
imageA = cv2.imread('./right_01.png')
cv_show("imageA", imageA)
imageB = cv2.imread('./left_01.png')
cv_show("imageB", imageB)
# 计算SIFT特征点和特征向量
(kpsA, featuresA) = detectAndCompute(imageA)
(kpsB, featuresB) = detectAndCompute(imageB)
# 基于最近邻和随机取样一致性得到一个单应性矩阵
(M, matches, status) = matchKeyPoints(kpsA, kpsB, featuresA, featuresB)
# 绘制匹配结果
drawMatches(imageA, imageB, kpsA, kpsB, matches, status)
# 拼接
stich(imageA, imageB, M)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
利用Python中的输入和输出功能进行读取和写入的教程
Apr 14 Python
Python的Flask框架中实现登录用户的个人资料和头像的教程
Apr 20 Python
Python将多个excel表格合并为一个表格
Feb 22 Python
详解Python循环作用域与闭包
Mar 21 Python
python实现网站微信登录的示例代码
Sep 18 Python
Python 用三行代码提取PDF表格数据
Oct 13 Python
新手学python应该下哪个版本
Jun 11 Python
使用tensorflow进行音乐类型的分类
Aug 14 Python
python使用多线程查询数据库的实现示例
Aug 17 Python
python通过函数名调用函数的几种场景
Sep 23 Python
matplotlib制作雷达图报错ValueError的实现
Jan 05 Python
Python列表删除重复元素与图像相似度判断及删除实例代码
May 07 Python
Python求两个字符串最长公共子序列代码实例
Mar 05 #Python
Python操作MongoDb数据库流程详解
Mar 05 #Python
Python文字截图识别OCR工具实例解析
Mar 05 #Python
win10下opencv-python特定版本手动安装与pip自动安装教程
Mar 05 #Python
python+OpenCV实现图像拼接
Mar 05 #Python
windows下Pycharm安装opencv的多种方法
Mar 05 #Python
解决pycharm中opencv-python导入cv2后无法自动补全的问题(不用作任何文件上的修改)
Mar 05 #Python
You might like
德生H-501的评价与改造
2021/03/02 无线电
用header 发送cookie的php代码
2007/03/16 PHP
Http 1.1 Etag 与 Last-Modified提高php效率
2008/01/10 PHP
php ob_flush,flush在ie中缓冲无效的解决方法
2010/05/09 PHP
PHP+MySQL统计该库中每个表的记录数并按递减顺序排列的方法
2016/02/15 PHP
php的debug相关函数用法示例
2016/07/11 PHP
jquery 最简单易用的表单验证插件
2010/02/27 Javascript
在多个页面使用同一个HTML片段的代码
2011/03/04 Javascript
jquery动画1.加载指示器
2012/08/24 Javascript
jquery 层次选择器siblings与nextAll的区别介绍
2013/08/02 Javascript
js简单实现根据身份证号码识别性别年龄生日
2013/11/29 Javascript
使用js画图之饼图
2015/01/12 Javascript
原生JS:Date对象全面解析
2016/09/06 Javascript
jQuery图片加载显示loading效果
2016/11/04 Javascript
浅谈Angular的$q, defer, promise
2016/12/20 Javascript
微信小程序 获取二维码实例详解
2017/06/23 Javascript
微信小程序支付及退款流程详解
2017/11/30 Javascript
JS实现模糊查询带下拉匹配效果
2018/06/21 Javascript
webpack+vue+express(hot)热启动调试简单配置方法
2018/09/19 Javascript
微信小程序使用二次贝塞尔曲线画波浪
2018/12/25 Javascript
jQuery实现当拉动滚动条到底部加载数据的方法分析
2019/01/24 jQuery
解决angular 使用原生拖拽页面卡顿及表单控件输入延迟问题
2020/04/21 Javascript
Python 高级专用类方法的实例详解
2017/09/11 Python
Python如何将图像音视频等资源文件隐藏在代码中(小技巧)
2020/02/16 Python
html5唤醒APP小记
2019/03/27 HTML / CSS
单位领导证婚词
2014/01/14 职场文书
母亲追悼会答谢词
2014/01/27 职场文书
四风问题自查报告剖析材料
2014/02/08 职场文书
自荐信的基本格式
2014/02/22 职场文书
小学毕业典礼主持词
2014/03/27 职场文书
空乘英文求职信
2014/04/13 职场文书
专升本学生毕业自我鉴定
2014/10/04 职场文书
2014年城市管理工作总结
2014/12/02 职场文书
java Nio使用NioSocket客户端与服务端交互实现方式
2021/06/15 Java/Android
手把手教你实现PyTorch的MNIST数据集
2021/06/28 Python
Python 中 Shutil 模块详情
2021/11/11 Python