python图像拼接融合 python调用stitcher类自动实现多个图像拼接融合功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python图像拼接融合 python调用stitcher类自动实现多个图像拼接融合功能

Helloworld-睖   2021-04-23 我要评论
想了解python调用stitcher类自动实现多个图像拼接融合功能的相关内容吗,Helloworld-睖在本文为您仔细讲解python图像拼接融合的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python图像拼接融合,python,stitcher类图像拼接融合,下面大家一起来学习吧。

使用stitcher需要注意,图像太大会报错而且计算慢。

特点和适用范围:图像需有足够重合相同特征区域。

优点:适应部分倾斜/尺度变换和畸变情形,拼接效果好,使用简单,可以一次拼接多张图片。

缺点:需要有足够的相同特征区域进行匹配,速度较慢(和图像大小有关)。

原图(可下载)

代码(两张图片拼接)

import sys
import cv2
 
if __name__ == "__main__":
    img1 = cv2.imread('C:/Users/Guaguan/Desktop/img/1.jpg')    # 图片绝对路径,
    img2 = cv2.imread('C:/Users/Guaguan/Desktop/img/2.jpg')
 
    # stitcher = cv2.createStitcher(False)    # 老的OpenCV版本,用这一个
    stitcher = cv2.Stitcher.create(cv2.Stitcher_PANORAMA)  # 我的是OpenCV4
 
    (status, pano) = stitcher.stitch((img1, img2))
    if status != cv2.Stitcher_OK:
        print("不能拼接图片, error code = %d" % status)
        sys.exit(-1)
    print("拼接成功.")
    cv2.imshow('pano', pano)
    # cv2.imwrite("pano.jpg", pano)
    cv2.waitKey(0)

拼接结果

原图

代码(多个图像自动拼接)

import os
import sys
import cv2
import win32ui
 
 
# ? python基于Stitcher图像拼接
 
 
def imgstitcher(imgs):  # 传入图像数据 列表[] 实现图像拼接
    stitcher = cv2.Stitcher.create(cv2.Stitcher_PANORAMA)
    _result, pano = stitcher.stitch(imgs)
 
    if _result != cv2.Stitcher_OK:
        print("不能拼接图片, error code = %d" % _result)
        sys.exit(-1)
 
    output = 'result' + '.png'
    cv2.imwrite(output, pano)
    print("拼接成功. %s 已保存!" % output)
 
 
if __name__ == "__main__":
    # imgPath为图片所在的文件夹相对路径
    imgPath = 'C:/Users/Guaguan/Desktop/img'
    
    imgList = os.listdir(imgPath)
    imgs = []
    for imgName in imgList:
        pathImg = os.path.join(imgPath, imgName)
        img = cv2.imread(pathImg)
        if img is None:
            print("图片不能读取:" + imgName)
            sys.exit(-1)
        imgs.append(img)
 
    imgstitcher(imgs)    # 拼接
 
    cv2.waitKey(0)
    cv2.destroyAllWindows()

结果

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们