怎么在python中使用OpenCV拼接图像

发布时间:2021-04-16 17:26:16 作者:Leah
来源:亿速云 阅读:377

本篇文章为大家展示了怎么在python中使用OpenCV拼接图像,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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拼接图像,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 如何在python中利用opencv拼接图像
  2. Python+OpenCV实现图像的全景拼接

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python opencv

上一篇:使用java怎么连接Mongodb并实现增删改查操作

下一篇:怎么在springboot中实现一个@WebFilter注解过滤器

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》