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的几个高级语法概念浅析(lambda表达式闭包装饰器)
May 28 Python
获取python的list中含有重复值的index方法
Jun 27 Python
使用Python写一个量化股票提醒系统
Aug 22 Python
Django中使用Whoosh进行全文检索的方法
Mar 31 Python
使用Python做定时任务及时了解互联网动态
May 15 Python
python:动态路由的Flask程序代码
Nov 22 Python
在Python中使用MongoEngine操作数据库教程实例
Dec 03 Python
Python实现括号匹配方法详解
Feb 10 Python
python 使用递归回溯完美解决八皇后的问题
Feb 26 Python
150行Python代码实现带界面的数独游戏
Apr 04 Python
Python中Selenium模块的使用详解
Oct 09 Python
Python基于unittest实现测试用例执行
Nov 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
javascript KeyDown、KeyPress和KeyUp事件的区别与联系
2009/12/03 Javascript
javascript自适应宽度的瀑布流实现思路
2013/02/20 Javascript
button没写type=button会导致点击时提交
2014/03/06 Javascript
js防止页面被iframe调用的方法
2014/10/30 Javascript
2014 HTML5/CSS3热门动画特效TOP10
2014/12/07 Javascript
DOM基础教程之模型中的模型节点
2015/01/19 Javascript
AngularJS iframe跨域打开内容时报错误的解决办法
2015/01/26 Javascript
IE下使用jQuery重置iframe地址时内存泄露问题解决办法
2015/02/05 Javascript
jQuery弹出层插件Lightbox_me使用指南
2015/04/21 Javascript
jquery插件hiAlert实现网页对话框美化
2015/05/03 Javascript
js生成验证码并直接在前端判断
2015/05/15 Javascript
JS实现带有抽屉效果的产品类网站多级导航菜单代码
2015/09/15 Javascript
JS实现根据文件字节数返回文件大小的方法
2016/08/02 Javascript
判断数组的最佳方法(推荐)
2016/10/11 Javascript
关于页面刷新vuex数据消失问题解决方案
2017/07/03 Javascript
小程序实现日历左右滑动效果
2019/10/21 Javascript
详解python播放音频的三种方法
2019/09/23 Python
python库matplotlib绘制坐标图
2019/10/18 Python
python3实现单目标粒子群算法
2019/11/14 Python
django model object序列化实例
2020/03/13 Python
基于python计算滚动方差(标准差)talib和pd.rolling函数差异详解
2020/06/08 Python
css3.0 图形构成实例练习二
2013/03/19 HTML / CSS
芬兰攀岩、山地运动和户外活动用品购物网站:Bergfreunde
2016/10/06 全球购物
英国领先的男装设计师服装独立零售商:Repertoire Fashion
2020/10/19 全球购物
入党自我评价范文
2014/02/02 职场文书
元宵节晚会主持人串词
2014/03/25 职场文书
《生命 生命》教学反思
2014/04/19 职场文书
计算机多媒体专业自荐信
2014/07/04 职场文书
商务英语专业毕业生求职信
2014/07/06 职场文书
高中同学会活动方案
2014/08/14 职场文书
在职证明书范本(2014新版)
2014/09/25 职场文书
教师考核评语大全
2014/12/31 职场文书
手残删除python之后的补救方法
2021/06/26 Python
一篇文章带你掌握SQLite3基本用法
2022/06/14 数据库
SpringCloud中分析讲解Feign组件添加请求头有哪些坑梳理
2022/06/21 Java/Android
Apache自带的ab压力测试工具的实现
2022/07/23 Servers