Python-基于OpenCV多画面全景拼接

如何将多张图像进行拼接?
如何对将一个画面全景图复原出来?

多个摄像头画面拼接,我们在这里是基于python的OpenCV库进行拼接;

python = 3.7.0
OpenCV > 3.0

在这里插入图片描述
在这里插入图片描述

"""
导入基本库
"""
import os
import cv2
import imutils
import numpy as np
import imutils


这里我们将要拼接的图像放在一个文件夹里面,循环读取(笨方法);


img_dir = 'H:\\ruanjain'
names = os.listdir(img_dir)
 
images = []
for name in names_2:
    img_path = os.path.join(img_dir, name)#路径拼接
    image = cv2.imread(img_path)#读取图片
    images.append(image)


造图像拼接对象stitcher

stitcher = cv2.createStitcher() if imutils.is_cv3() else cv2.Stitcher_create()

把图像列表传入.stitch函数,该函数会返回状态和拼接好的全景图
如果图像拼接成功会返回 status = 0,stitched 为拼接成功后的图像

status, stitched = stitcher.stitch(images)

在拼接成功后,我们进行图像保存


if status==0:
    print(1)
    cv2.imwrite('H:\stitch_1.jpg', stitched)

在这里插入图片描述
在图像拼接成功后我们可以将图像进行拉伸处理,将图像的高变回原来图像的“高”。这样图像的拼接就成功了;

完整代码

import os
import cv2
import imutils
import numpy as np
img_dir = 'H://frg'#拼接图像的文件夹路劲
names = os.listdir(img_dir)
images = []
for name in names:
    img_path = os.path.join(img_dir, name)
    image = cv2.imread(img_path)
    #image = cv2.resize(image,(1919,1078))
    images.append(image)
stitcher = cv2.createStitcher() if imutils.is_cv3() else cv2.Stitcher_create()
status, stitched = stitcher.stitch(images)
print(status)
if status==0:
    print(1)
    cv2.imwrite('H:\stitch_2.jpg', stitched)#保存拼接图像

特别声明:图片来自软件杯数据集,如有版权问题,请告知;

猜你喜欢

转载自blog.csdn.net/qq_44936246/article/details/115050158#comments_29829167