neobanshee neobanshee - 10 months ago 128
C++ Question

OpenCV (C++) stitcher not writing to OutputArray

I'm currently playing around with a 360° camera and want to use OpenCV's spherical warper for that. However, each time I try to run a simple program that makes use of the stitcher functionality, it fails to return a stitched image. I'm basically just taking the 360° picture, divide it into two separate pictures (front- and rear lens) and want to stitch them back together.

Here's the code:

Mat srcImage = imread("assets/360_0043.JPG");
Mat frontLensImage(srcImage, Rect(0, 0, srcImage.cols / 2, srcImage.rows));
Mat rearLensImage(srcImage, Rect(srcImage.rows, 0, srcImage.cols / 2, srcImage.rows));

vector<Mat> imagesToStitch;

Mat panorama;
Stitcher stitcher = Stitcher::createDefault();
stitcher.stitch(imagesToStitch, panorama);
imshow("test", panorama);
cout << "ERROR: Image array empty" << endl;

return 0;

When trying to run, it returns this error:

OpenCV Error: Assertion failed (ssize.area() > 0) in resize, file /build/opencv-SviWsf/opencv-, line 1834
terminate called after throwing an instance of 'cv::Exception'
what(): /build/opencv-SviWsf/opencv- error: (-215) ssize.area() > 0 in function resize

When debugging,
is an empty object even though I pass it as the
. I searched the web thoroughly and couldn't find a solution, so any help would be greatly appreciated!

Answer Source

Kinda solved it. Apparently, OpenCVs memory management doesn't like you referencing the same address all the time. Since both of my images are dependant on srcImage I assume this is where the error was. I did a quick workaround which looks like this:

Mat unprocessedFrontLensImage(srcImage, Rect(0, 0, 3 * srcImage.cols / 4, srcImage.rows));
Mat unprocessedRearLensImage(srcImage, Rect(srcImage.cols / 4, 0, 3 * srcImage.cols / 4, srcImage.rows));
imwrite("left.jpg", unprocessedFrontLensImage);
imwrite("right.jpg", unprocessedRearLensImage);
Mat frontLensImage = imread("left.jpg");
Mat rearLensImage = imread("right.jpg");

Works like a charm. Don't teach me about redundancy, I know. I'm gonna clean up and refactor it, this is just my workaround for now.