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 相关文章推荐
pyside写ui界面入门示例
Jan 22 Python
python重试装饰器示例
Feb 11 Python
python中readline判断文件读取结束的方法
Nov 08 Python
python调用java模块SmartXLS和jpype修改excel文件的方法
Apr 28 Python
Python中的anydbm模版和shelve模版使用指南
Jul 09 Python
Python的Django框架中的数据库配置指南
Jul 17 Python
Python实现快速排序算法及去重的快速排序的简单示例
Jun 26 Python
Python 创建新文件时避免覆盖已有的同名文件的解决方法
Nov 16 Python
python内置模块collections知识点总结
Dec 19 Python
Python imutils 填充图片周边为黑色的实现
Jan 19 Python
Python使用文件操作实现一个XX信息管理系统的示例
Jul 02 Python
Python绘制数码晶体管日期
Feb 19 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
php利用腾讯ip分享计划获取地理位置示例分享
2014/01/20 PHP
ThinkPHP登录功能的实现方法
2014/08/20 PHP
Yii调试查看执行SQL语句的方法
2016/07/15 PHP
php中访问修饰符的知识点总结
2019/01/27 PHP
PHP实时统计中文字数和区别
2019/02/28 PHP
JQuery 操作Javascript对象和数组的工具函数小结
2010/01/22 Javascript
JavaScript 页面编码与浏览器类型判断代码
2010/06/03 Javascript
Prototype源码浅析 Enumerable部分之each方法
2012/01/16 Javascript
jQuery实现动画效果的实例代码
2013/05/07 Javascript
IE下window.onresize 多次调用与死循环bug处理方法介绍
2013/11/12 Javascript
js截取固定长度的中英文字符的简单实例
2013/11/22 Javascript
js判断IE浏览器版本过低示例代码
2013/11/22 Javascript
使用jQuery获得内容以及内容的属性
2015/02/26 Javascript
JS+CSS实现类似QQ好友及黑名单效果的树型菜单
2015/09/22 Javascript
浅析Nodejs npm常用命令
2016/06/14 NodeJs
使用JS读取XML文件的方法
2016/11/25 Javascript
nodejs 实现钉钉ISV接入的加密解密方法
2017/01/16 NodeJs
javascript简单写的判断电话号码实例
2017/05/24 Javascript
angular4自定义表单控件[(ngModel)]的实现
2018/11/23 Javascript
微信小程序 确认框的实现(附代码)
2019/07/23 Javascript
layui 实现二级弹窗弹出之后 关闭一级弹窗的方法
2019/09/18 Javascript
Python中捕捉详细异常信息的代码示例
2014/09/18 Python
python Opencv将图片转为字符画
2021/02/19 Python
python通过微信发送邮件实现电脑关机
2018/06/20 Python
Appium+python自动化之连接模拟器并启动淘宝APP(超详解)
2019/06/17 Python
在Pandas中处理NaN值的方法
2019/06/25 Python
django中瀑布流写法实例代码
2019/10/14 Python
django实现模板中的字符串文字和自动转义
2020/03/31 Python
使用HTML5 Canvas绘制直线或折线等线条的方法讲解
2016/03/14 HTML / CSS
澳大利亚墨尔本的在线时装店:LORETA
2018/09/14 全球购物
上班迟到检讨书
2014/01/10 职场文书
《愚公移山》教学反思
2014/02/20 职场文书
进步之星获奖感言
2014/02/22 职场文书
初中班主任培训心得体会
2016/01/07 职场文书
话题作文之学会尊重
2019/12/16 职场文书
vue+iview实现手机号分段输入框
2022/03/25 Vue.js