SoFunction
Updated on 2024-11-16

Python calls stitcher class to automate multiple image stitching fusion function

You need to be careful with the stitcher, as the image is too large to report errors and slow to compute.

Characteristics and scope of application: Images need to haveSufficient overlapSame feature area.

Advantages: adapts to partial tilt/scale transformations and distortion situations, good stitching, easy to use, can stitch multiple images at once.

Disadvantages: need to have enough identical feature regions for matching, slower (related to image size).

Original image (downloadable)

Code (two images stitched together)

import sys
import cv2
 
if __name__ == "__main__":
    img1 = ('C:/Users/Guaguan/Desktop/img/')    # the absolute path of the image.
    img2 = ('C:/Users/Guaguan/Desktop/img/')
 
    # stitcher = (False) # Older OpenCV versions with this one
    stitcher = (cv2.Stitcher_PANORAMA)  # I've got OpenCV4
 
    (status, pano) = ((img1, img2))
    if status != cv2.Stitcher_OK:
        print("Cannot splice images, error code = %d" % status)
        (-1)
    print("Splice successful.")
    ('pano', pano)
    # ("", pano)
    (0)

splicing result

original figure

Code (automatic stitching of multiple images)

import os
import sys
import cv2
import win32ui
 
 
# ? python based Stitcher image stitching
 
 
def imgstitcher(imgs):  # Pass in image data List[] to implement image stitching
    stitcher = (cv2.Stitcher_PANORAMA)
    _result, pano = (imgs)
 
    if _result != cv2.Stitcher_OK:
        print("Cannot splice images, error code = %d" % _result)
        (-1)
 
    output = 'result' + '.png'
    (output, pano)
    print("Splice successful. %s saved!" % output)
 
 
if __name__ == "__main__":
    # imgPath is the relative path to the folder where the image is located
    imgPath = 'C:/Users/Guaguan/Desktop/img'
    
    imgList = (imgPath)
    imgs = []
    for imgName in imgList:
        pathImg = (imgPath, imgName)
        img = (pathImg)
        if img is None:
            print("Image cannot be read:" + imgName)
            (-1)
        (img)
 
    imgstitcher(imgs)    # Splice
 
    (0)
    ()

in the end

to this article on the python call stitcher class automatically realize the fusion of multiple image splicing article is introduced to this, more related python image splicing fusion content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!