python+OpenCV realizes image stitching

The examples in this article share the use of python and OpenCV to realize image stitching for your reference. The specific content is as follows

python+OpenCV implements image stitching

The C++ version of the Stitcher class can be found in the latest OpenCV official documentation, but the python version has not been updated in time. This article gives a brief introduction to the implementation of the python version.

Since there is no description of the Python version of the Stitcher class in the official documentation, you can only go to the GitHub source code to find it yourself. The following is a sample of stitching:

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)

There is a lot of written on it, but if you use it directly, you can use the following code, simple and rude

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),Called according to different OpenCV versions(_result, pano)= stitcher.stitch((img1, img2))
 cv2.imshow('pano',pano)
 cv2.waitKey(0)

The effect is as follows:

Original image:

The stitched image:

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

python+OpenCV realizes image stitching
Python implements image stitching
Python realizes stitching pictures
python opencv for image stitching
Python implements image stitching function
Python realizes image recognition car function
Python dry goods | remote sensing image stitching