2

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!

neobanshee
  • 31
  • 5
  • Shouldn't it be: `Mat rearLensImage(srcImage, Rect(srcImage.cols / 2, 0, srcImage.cols / 2, srcImage.rows));`? However, you need the images to overlap... so you can try with: `Mat frontLensImage(srcImage, Rect(0, 0, 3 * srcImage.cols / 4, srcImage.rows)); Mat rearLensImage(srcImage, Rect(srcImage.cols / 4, 0, 3 * srcImage.cols / 4, srcImage.rows));` – Miki Oct 26 '16 at 15:48
  • Thanks for the fast reply! I didn't know about the overlap so that's good to know. It still doesn't work using your code though. I get this error: `Assertion failed (size.width>0 && size.height>0) in imshow` – neobanshee Oct 26 '16 at 16:27

1 Answers1

1

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.

neobanshee
  • 31
  • 5