OpenCV  4.0.1
Open Source Computer Vision
samples/python/stitching.py

A basic example on image stitching in Python.

1 #!/usr/bin/env python
2 
3 '''
4 Stitching sample
5 ================
6 
7 Show how to use Stitcher API from python in a simple way to stitch panoramas
8 or scans.
9 '''
10 
11 from __future__ import print_function
12 import cv2 as cv
13 import numpy as np
14 import argparse
15 import sys
16 
17 modes = (cv.Stitcher_PANORAMA, cv.Stitcher_SCANS)
18 
19 parser = argparse.ArgumentParser(description='Stitching sample.')
20 parser.add_argument('--mode',
21  type = int, choices = modes, default = cv.Stitcher_PANORAMA,
22  help = 'Determines configuration of stitcher. The default is `PANORAMA` (%d), '
23  'mode suitable for creating photo panoramas. Option `SCANS` (%d) is suitable '
24  'for stitching materials under affine transformation, such as scans.' % modes)
25 parser.add_argument('--output', default = 'result.jpg',
26  help = 'Resulting image. The default is `result.jpg`.')
27 parser.add_argument('img', nargs='+', help = 'input images')
28 args = parser.parse_args()
29 
30 # read input images
31 imgs = []
32 for img_name in args.img:
33  img = cv.imread(img_name)
34  if img is None:
35  print("can't read image " + img_name)
36  sys.exit(-1)
37  imgs.append(img)
38 
39 stitcher = cv.Stitcher.create(args.mode)
40 status, pano = stitcher.stitch(imgs)
41 
42 if status != cv.Stitcher_OK:
43  print("Can't stitch images, error code = %d" % status)
44  sys.exit(-1)
45 
46 cv.imwrite(args.output, pano);
47 print("stitching completed successfully. %s saved!" % args.output)