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的urllib库提交WEB表单
Feb 24 Python
Python写的Tkinter程序屏幕居中方法
Mar 10 Python
Python有序字典简单实现方法示例
Sep 28 Python
Python random模块用法解析及简单示例
Dec 18 Python
PyQt5实现拖放功能
Apr 25 Python
pandas 透视表中文字段排序方法
Nov 16 Python
matlab中imadjust函数的作用及应用举例
Feb 27 Python
使用sklearn的cross_val_score进行交叉验证实例
Feb 28 Python
基于jupyter代码无法在pycharm中运行的解决方法
Apr 21 Python
Python+OpenCV图像处理—— 色彩空间转换
Oct 22 Python
如何用Python和JS实现的Web SSH工具
Feb 23 Python
浅谈Python中的函数(def)及参数传递操作
May 25 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学习笔记(三)操作符与控制结构
2011/08/06 PHP
php使用百度天气接口示例
2014/04/22 PHP
php实现图片上传并利用ImageMagick生成缩略图
2016/03/14 PHP
php变量与数组相互转换的方法(extract与compact)
2016/12/02 PHP
关于php支持的协议与封装协议总结(推荐)
2017/11/17 PHP
jQuery 打造动态下滑菜单实现说明
2010/04/15 Javascript
javascript 正则表达式相关应介绍
2012/11/27 Javascript
js禁止document element对象选中文本实现代码
2013/03/21 Javascript
jQuery中innerHeight()方法用法实例
2015/01/19 Javascript
学习JavaScript设计模式之策略模式
2016/01/12 Javascript
jQuery时间插件jquery.clock.js用法实例(5个示例)
2016/01/14 Javascript
快速掌握Node.js之Window下配置NodeJs环境
2016/03/21 NodeJs
BootStrap中Datetimepicker和uploadify插件应用实例小结
2016/05/26 Javascript
AngularJS 模型详细介绍及实例代码
2016/07/27 Javascript
浅谈js的异步执行
2016/10/18 Javascript
详解Node.js项目APM监控之New Relic
2017/05/12 Javascript
nodejs+websocket实时聊天系统改进版
2017/05/18 NodeJs
Angular项目中$scope.$apply()方法的使用详解
2017/07/26 Javascript
nodejs 图片预览和上传的示例代码
2017/09/30 NodeJs
小程序图片剪裁加旋转的示例代码
2018/07/10 Javascript
JS实现简单的星期格式转换功能示例
2018/07/23 Javascript
JS严格模式原理与用法实例分析
2020/04/27 Javascript
Python绘制堆叠柱状图的实例
2019/07/09 Python
Python selenium文件上传下载功能代码实例
2020/04/13 Python
Keras自定义实现带masking的meanpooling层方式
2020/06/16 Python
Canvas与图片压缩的示例代码
2017/11/28 HTML / CSS
荷兰在线体育用品商店:Avantisport.nl
2018/07/04 全球购物
雅诗兰黛澳大利亚官网:Estée Lauder澳大利亚
2019/05/31 全球购物
目标责任书范文
2014/04/14 职场文书
团队队名口号大全
2014/06/06 职场文书
安全月宣传标语
2014/10/07 职场文书
学习三严三实心得体会
2014/10/13 职场文书
2015年质量管理工作总结范文
2015/05/18 职场文书
法律意见书范文
2015/05/20 职场文书
vue3语法糖内的defineProps及defineEmits
2022/04/14 Vue.js
PYTHON基于Pyecharts绘制常见的直角坐标系图表
2022/04/28 Python