stitching.py 1.7 KB

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