OpenCV  4.10.0-dev
Open Source Computer Vision
Loading...
Searching...
No Matches
samples/python/stitching.py

A basic example on image stitching in Python.

1#!/usr/bin/env python
2
3'''
4Stitching sample
5================
6
7Show how to use Stitcher API from python in a simple way to stitch panoramas
8or scans.
9'''
10
11# Python 2/3 compatibility
12from __future__ import print_function
13
14import numpy as np
15import cv2 as cv
16
17import argparse
18import sys
19
20modes = (cv.Stitcher_PANORAMA, cv.Stitcher_SCANS)
21
22parser = argparse.ArgumentParser(prog='stitching.py', description='Stitching sample.')
23parser.add_argument('--mode',
24 type = int, choices = modes, default = cv.Stitcher_PANORAMA,
25 help = 'Determines configuration of stitcher. The default is `PANORAMA` (%d), '
26 'mode suitable for creating photo panoramas. Option `SCANS` (%d) is suitable '
27 'for stitching materials under affine transformation, such as scans.' % modes)
28parser.add_argument('--output', default = 'result.jpg',
29 help = 'Resulting image. The default is `result.jpg`.')
30parser.add_argument('img', nargs='+', help = 'input images')
31
32__doc__ += '\n' + parser.format_help()
33
34def main():
35 args = parser.parse_args()
36
37 # read input images
38 imgs = []
39 for img_name in args.img:
40 img = cv.imread(cv.samples.findFile(img_name))
41 if img is None:
42 print("can't read image " + img_name)
43 sys.exit(-1)
44 imgs.append(img)
45
46 #![stitching]
47 stitcher = cv.Stitcher.create(args.mode)
48 status, pano = stitcher.stitch(imgs)
49
50 if status != cv.Stitcher_OK:
51 print("Can't stitch images, error code = %d" % status)
52 sys.exit(-1)
53 #![stitching]
54
55 cv.imwrite(args.output, pano)
56 print("stitching completed successfully. %s saved!" % args.output)
57
58 print('Done')
59
60
61if __name__ == '__main__':
62 print(__doc__)
63 main()
static Ptr< Stitcher > create(Mode mode=Stitcher::PANORAMA)
Creates a Stitcher configured in one of the stitching modes.
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
void destroyAllWindows()
Destroys all of the HighGUI windows.
CV_EXPORTS_W bool imwrite(const String &filename, InputArray img, const std::vector< int > &params=std::vector< int >())
Saves an image to a specified file.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3