pythonopencvimage-stitching

How to Set Panorama Mode in OpenCV Python Stitcher Class?


Based on this question/answer, I have been trying to use OpenCV's Stitcher class in python. My code is basically the same as in the answer.

import cv2

stitcher = cv2.createStitcher(False)
foo = cv2.imread("D:/foo.png")
bar = cv2.imread("D:/bar.png")
result = stitcher.stitch((foo,bar))

cv2.imwrite("D:/result.jpg", result[1])

The issue is I want to change the mode from Panoramic to Scans. In the c++ documentation, the create method has an input of mode. However, the createStitcher class in Python only takes one input- whether or not to try gpu. Is there a way to specify the mode in Python?

When I tried createStitcherScans, I get an error

stitcher = cv2.createStitcherScans(False)
"AttributeError: 'module' object has no attribute 'createStitcherScans'"

I did find this GitHub issue that seems relevant, regarding Python bindings missing something. But this is over my head and I am not sure how to edit the opencv code to do this properly. I tried adding this to the stitching.hpp:

typedef Stitcher::Mode Mode;

But nothing happened. createStitcher(1, False) still give me the attribute error. Any help would be greatly appreciated.


Solution

  • This has been resolved in Open CV 4 (at least in 4.1.0 which I'm running). You can now specify mode as a keyword argument to cv2.Stitcher.create:

    stitcher = cv2.Stitcher.create(mode = 1)
    

    It seems that the enumeration labels for the different modes have not yet been ported but it works with numbers. PANORAMA is 0, SCANS is 1 as defined here.

    Note, if you're working on Google Colab then you can change the version of Open CV like this:

    !pip3 install opencv-python==4.1.0.25