Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OpenCV Stitcher class with Python?

Tags:

python

opencv

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.

like image 380
Carlos Avatar asked Dec 18 '15 19:12

Carlos


1 Answers

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

like image 52
Gwen Avatar answered Oct 11 '22 07:10

Gwen