Search code examples
c++opencvimage-stitchingopencv-stitching

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;
imagesToStitch.push_back(frontLensImage);
imagesToStitch.push_back(rearLensImage);

Mat panorama;
Stitcher stitcher = Stitcher::createDefault();
if(!imagesToStitch.empty()){
    stitcher.stitch(imagesToStitch, panorama);
    imshow("test", panorama);
    waitKey(0);
}
else{
    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-2.4.9.1+dfsg/modules/imgproc/src/imgwarp.cpp, line 1834
terminate called after throwing an instance of 'cv::Exception'
what():  /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/imgproc/src/imgwarp.cpp:1834: error: (-215) ssize.area() > 0 in function resize

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


Solution

  • 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.