脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python调用stitcher类自动实现多个图像拼接融合功能

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

2021-10-18 09:51Helloworld-睖 Python

这篇文章主要介绍了python调用stitcher类自动实现多个图像拼接融合功能,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

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

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

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

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

原图(可下载)

python调用stitcher类自动实现多个图像拼接融合功能python调用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)

拼接结果

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

原图

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

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

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()

结果

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

到此这篇关于python调用stitcher类自动实现多个图像拼接融合的文章就介绍到这了,更多相关python图像拼接融合内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_36623595/article/details/116018818

延伸 · 阅读

精彩推荐