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进阶教程之函数参数的多种传递方法
Aug 30 Python
Python的Flask框架与数据库连接的教程
Apr 20 Python
Python抓取框架 Scrapy的架构
Aug 12 Python
python线程池threadpool实现篇
Apr 27 Python
Django 登陆验证码和中间件的实现
Aug 17 Python
Python安装pycurl失败的解决方法
Oct 15 Python
django写用户登录判定并跳转制定页面的实例
Aug 21 Python
python 调试冷知识(小结)
Nov 11 Python
Python之变量类型和if判断方式
May 05 Python
Pytorch损失函数nn.NLLLoss2d()用法说明
Jul 07 Python
基于python实现判断字符串是否数字算法
Jul 10 Python
python开发入门——set的使用
Sep 03 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和ACCESS写聊天室(四)
2006/10/09 PHP
php自动加载autoload机制示例分享
2014/02/20 PHP
php实现斐波那契数列的简单写法
2014/07/19 PHP
CI框架给视图添加动态数据
2014/12/01 PHP
PHP调用全国天气预报数据接口查询天气示例
2019/02/20 PHP
详解phpstorm2020最新破解方法
2020/09/17 PHP
jQuery 表单验证插件formValidation实现个性化错误提示
2009/06/23 Javascript
JS实现随机数生成算法示例代码
2013/08/08 Javascript
jquery处理页面弹出层查询数据等待操作实例
2015/03/25 Javascript
浅谈JavaScript中小数和大整数的精度丢失
2016/05/31 Javascript
详解Node全局变量global模块
2017/09/28 Javascript
node前端模板引擎Jade之标签的基本写法
2018/05/11 Javascript
Node.js命令行/批处理中如何更改Linux用户密码浅析
2018/07/22 Javascript
详解VUE单页应用骨架屏方案
2019/01/17 Javascript
微信小程序云开发(数据库)详解
2019/05/17 Javascript
JS 实现发送短信验证码的“59秒后重新发送验证短信”功能
2019/08/23 Javascript
jQuery实现日历效果
2020/09/11 jQuery
推荐下python/ironpython:从入门到精通
2007/10/02 Python
零基础写python爬虫之HTTP异常处理
2014/11/05 Python
Python中的推导式使用详解
2015/06/03 Python
Python随机数用法实例详解【基于random模块】
2017/04/18 Python
python使用pycharm环境调用opencv库
2018/02/11 Python
Tensorflow实现卷积神经网络用于人脸关键点识别
2018/03/05 Python
如何使用Python实现斐波那契数列
2019/07/02 Python
Windows上安装tensorflow  详细教程(图文详解)
2020/02/04 Python
细数nn.BCELoss与nn.CrossEntropyLoss的区别
2020/02/29 Python
python接口自动化之ConfigParser配置文件的使用详解
2020/08/03 Python
Python自动创建Excel并获取内容
2020/09/16 Python
Book Depository亚太地区:一家领先的国际图书零售商
2019/05/05 全球购物
罗马尼亚购物网站:Vivantis.ro
2019/07/20 全球购物
应急管理培训方案
2014/06/12 职场文书
个人遵守党的政治纪律情况对照检查材料
2014/09/26 职场文书
幼儿园六一主持词
2015/06/30 职场文书
同学会感言
2015/07/30 职场文书
会计岗位工作总结
2015/08/12 职场文书
女人创业励志语录,句句蕴含能量,激发你的潜能
2019/08/20 职场文书