本篇文章为大家展示了怎么在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拼接图像,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。