python+OpenCV实现图像拼接


Posted in Python onMarch 05, 2020

本文实例为大家分享了利用python和OpenCV实现图像拼接,供大家参考,具体内容如下

python+OpenCV实现image stitching

在最新的OpenCV官方文档中可以找到C++版本的Stitcher类的说明, 但是python版本的还没有及时更新, 本篇对python版本的实现做一个简单的介绍.

由于官方文档中还没有python版本的Stitcher类的说明, 因此只能自己去GitHub源码上找, 以下是stitching的样例:

from __future__ import print_function
import cv2 as cv
import numpy as np
import argparse
import sys

modes = (cv.Stitcher_PANORAMA, cv.Stitcher_SCANS)

parser = argparse.ArgumentParser(description='Stitching sample.')
parser.add_argument('--mode',
 type = int, choices = modes, default = cv.Stitcher_PANORAMA,
 help = 'Determines configuration of stitcher. The default is `PANORAMA` (%d), '
   'mode suitable for creating photo panoramas. Option `SCANS` (%d) is suitable '
   'for stitching materials under affine transformation, such as scans.' % modes)
parser.add_argument('--output', default = 'result.jpg',
 help = 'Resulting image. The default is `result.jpg`.')
parser.add_argument('img', nargs='+', help = 'input images')
args = parser.parse_args()

# read input images
imgs = []
for img_name in args.img:
 img = cv.imread(img_name)
 if img is None:
  print("can't read image " + img_name)
  sys.exit(-1)
 imgs.append(img)

stitcher = cv.Stitcher.create(args.mode)
status, pano = stitcher.stitch(imgs)

if status != cv.Stitcher_OK:
 print("Can't stitch images, error code = %d" % status)
 sys.exit(-1)

cv.imwrite(args.output, pano);
print("stitching completed successfully. %s saved!" % args.output)

上面写了一大堆, 然鹅, 直接拿来用的话, 用下面的代码可以了, 简单粗暴

import numpy as np
import cv2
from cv2 import Stitcher

if __name__ == "__main__":
 img1 = cv2.imread('1.jpg')
 img2 = cv2.imread('2.jpg')
 stitcher = cv2.createStitcher(False)
 #stitcher = cv2.Stitcher.create(cv2.Stitcher_PANORAMA), 根据不同的OpenCV版本来调用
 (_result, pano) = stitcher.stitch((img1, img2))
 cv2.imshow('pano',pano)
 cv2.waitKey(0)

效果如下:

原图:

python+OpenCV实现图像拼接

python+OpenCV实现图像拼接

拼接后的图像:

python+OpenCV实现图像拼接

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

Python 相关文章推荐
python代码检查工具pylint 让你的python更规范
Sep 05 Python
Python通过递归遍历出集合中所有元素的方法
Feb 25 Python
详解Python中with语句的用法
Apr 15 Python
python执行子进程实现进程间通信的方法
Jun 02 Python
Python初学时购物车程序练习实例(推荐)
Aug 08 Python
Python3使用PyQt5制作简单的画板/手写板实例
Oct 19 Python
解决pycharm运行程序出现卡住scanning files to index索引的问题
Jun 27 Python
如何在Django配置文件里配置session链接
Aug 06 Python
python中matplotlib条件背景颜色的实现
Sep 02 Python
使用pyinstaller逆向.pyc文件
Dec 20 Python
Python如何使用PIL Image制作GIF图片
May 16 Python
python中的unittest框架实例详解
Feb 05 Python
windows下Pycharm安装opencv的多种方法
Mar 05 #Python
解决pycharm中opencv-python导入cv2后无法自动补全的问题(不用作任何文件上的修改)
Mar 05 #Python
浅谈Python的方法解析顺序(MRO)
Mar 05 #Python
python环境下安装opencv库的方法
Mar 05 #Python
Python序列化pickle模块使用详解
Mar 05 #Python
Window系统下Python如何安装OpenCV库
Mar 05 #Python
Python bytes string相互转换过程解析
Mar 05 #Python
You might like
腾讯QQ php程序员面试题目整理
2010/06/08 PHP
js select常用操作控制代码
2010/03/16 Javascript
简单的两种Extjs formpanel加载数据的方式
2013/11/09 Javascript
Nodejs实现批量下载妹纸图
2015/05/28 NodeJs
使用Javascript写的2048小游戏
2015/11/25 Javascript
jQuery form 表单验证插件(fieldValue)校验表单
2016/01/24 Javascript
通过jquery-ui中的sortable来实现拖拽排序的简单实例
2016/05/24 Javascript
js倒计时小实例(多次定时)
2016/12/08 Javascript
微信小程序 支付简单实例及注意事项
2017/01/06 Javascript
JavaScript实现大图轮播效果
2017/01/11 Javascript
jQuery操作css样式
2017/05/15 jQuery
jquery实现图片放大点击切换
2017/06/06 jQuery
浅谈Vue的加载顺序探讨
2017/10/25 Javascript
JavaScript中call和apply方法的区别实例分析
2018/08/03 Javascript
解决angularjs service中依赖注入$scope报错的问题
2018/10/02 Javascript
JavaScript多种滤镜算法实现代码实例
2019/12/10 Javascript
详解JavaScript中的数据类型,以及检测数据类型的方法
2020/09/17 Javascript
react-intl实现React国际化多语言的方法
2020/09/27 Javascript
centos系统升级python 2.7.3
2014/07/03 Python
Python判断变量是否已经定义的方法
2014/08/18 Python
python实现无证书加密解密实例
2014/10/27 Python
python模块smtplib实现纯文本邮件发送功能
2018/05/22 Python
使用Py2Exe for Python3创建自己的exe程序示例
2018/10/31 Python
深入了解python中元类的相关知识
2019/08/29 Python
python使用openCV遍历文件夹里所有视频文件并保存成图片
2020/01/14 Python
Python实现投影法分割图像示例(二)
2020/01/17 Python
python_array[0][0]与array[0,0]的区别详解
2020/02/18 Python
python代码中怎么换行
2020/06/17 Python
Python pickle模块常用方法代码实例
2020/10/10 Python
爱普生美国官网:Epson美国
2018/11/05 全球购物
教师年终个人自我评价
2013/10/04 职场文书
初中数学教学反思
2014/01/16 职场文书
迎新生标语大全
2014/10/06 职场文书
2014年民警工作总结
2014/11/25 职场文书
2014年学前班工作总结
2014/12/08 职场文书
关于感恩的作文
2019/08/26 职场文书