15

I'm trying to use OpenCV Stitcher class with Python, with no luck. My code is:

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

I get a tuple with (1, None).

Following the C++ example, I tried to pass a numpy array as a second argument to stitch() with no luck.

Carlos
  • 151
  • 1
  • 1
  • 4
  • This is a very interesting problem. I do not find any python documentation for this function, though it is there. – tfv Apr 11 '16 at 18:11

1 Answers1

22

You're using it right, be the process failed for some reason.

The first value of the result tuple is an error code, with 0 indicating success. Here you got 1, which means, according to stitching.hpp, that the process needs more images.

enum Status
{
    OK = 0,
    ERR_NEED_MORE_IMGS = 1,
    ERR_HOMOGRAPHY_EST_FAIL = 2,
    ERR_CAMERA_PARAMS_ADJUST_FAIL = 3
};

ERR_NEED_MORE_IMGS usually indicates that you don't have enough keypoints in your images.

If you need more details about why the error occurs, you could switch to C++ and debug the process in details.


Edit : providing working example

Same code as OP, just added result save and absolute paths.

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])

with these images: (I hope you love koalas)

foo.png

foo.png

bar.png

bar.png

result.jpg

result.jpg

Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76
Gwen
  • 1,436
  • 3
  • 23
  • 31
  • Gwen, thanks for taking interest in that problem. We want to have a python solution, can you give an example in python with images you provide? – tfv Apr 15 '16 at 10:59
  • Does not work for me yet, have demonstrated my problems with separate answer above. Can you help? – tfv Apr 15 '16 at 11:45
  • I can't comment your answer (not enough reputation...). There is a bug in OpenCV 3.0+. It remains quite unclear but seems to be linked to OpenCL integration (see [this bug report](https://github.com/Itseez/opencv/issues/5667) for example). You need to rebuild OpenCV without OpenCL support, and maybe comment line 163 of `cv2.cpp` (`//CV_Error(Error::StsAssert, "The data should normally be NULL!");`) – Gwen Apr 15 '16 at 11:47
  • @tfv and I'm using python 2.7 and OpenCV 3.1.0 too. – Gwen Apr 15 '16 at 11:53
  • OK, I'll have to look into that in more detail tonight. Commenting my post should be working now :-) – tfv Apr 15 '16 at 11:54
  • Yay, thanks :-) If you can't rebuild OpenCV 3.1, you could try to downgrade to a previous version (2.4.12) where the bug is not present. – Gwen Apr 15 '16 at 11:58
  • as described above: Tried it with 2.4.11, result is: AttributeError: 'module' object has no attribute 'createStitcher'. So only way seems compilation. Can you confirm that it works for your with your code above and binary version of 2.4.12? – tfv Apr 16 '16 at 14:59
  • No, I didn't try with 2.4, that was just an idea. – Gwen Apr 16 '16 at 15:02
  • 8
    That's a Koala Bear! Also the bug which causes error on line 163, seems to be resolved in OpenCV 3.3.0. Thanks for the well written answer. – Gaurav Agarwal Aug 17 '17 at 07:01