Search code examples
c++iosopencvopencv-stitching

OpenCV on iOS simple stitching example fails with resize error


I am trying to run a simple photo stitching example using OpenCV XCFramework in an iOS app, but it fails with the following error during the call to stitcher->stitch(imgs, pano)

ibc++abi: terminating with uncaught exception of type cv::Exception: OpenCV(4.6.0-dev) /Users/gpdawson/Documents/Apps/OpenCV/opencv/modules/imgproc/src/resize.cpp:4065: error: (-215:Assertion failed) inv_scale_x > 0 in function 'resize'

The code that is being called is as follows:

+ (void)testStitchSimple {

UIImage *uiImage;
cv::Mat opencvImage1;
cv::Mat opencvImage2;
cv::Mat opencvImage3;
vector<cv::Mat> imgs;

uiImage = [UIImage imageNamed:@"first.jpg"];
UIImageToMat(uiImage, opencvImage1);
imgs.push_back(opencvImage1);

uiImage = [UIImage imageNamed:@"second.jpg"];
UIImageToMat(uiImage, opencvImage2);
imgs.push_back(opencvImage2);

uiImage = [UIImage imageNamed:@"third.jpg"];
UIImageToMat(uiImage, opencvImage3);
imgs.push_back(opencvImage3);

Stitcher::Mode mode = Stitcher::PANORAMA;
Stitcher *stitcher = Stitcher::create(mode);  // THIS IS THE CAUSE - SEE ANSWER

Mat pano;
Stitcher::Status status;
status = stitcher->stitch(imgs, pano); // FAILS WITH RESIZE ERROR MESSAGE

}

Note that I have tried using different source images and also reading the images from file directly into opencvImage using OpenCV's imread() function. (The image types are CV_8UC4 according to OpenCV's typeToString function).

I've also tried calling estimateTransform instead of stitch and error is the same. This is presumably because stitch itself calls estimateTransform, which is where the same error must be occuring in both cases.

Any help with finding the cause of this error or else at least working around it would be much appreciated.


Solution

  • I have discovered the cause of the problem. My code to create the stitcher object is apparently faulty, and is presumably a result of inappropriately mixing Objective-C object pointer with C++ Ptr.

    Stitcher *stitcher = Stitcher::create(mode); // INCORRECT
    
    cv::Ptr<Stitcher> stitcher = Stitcher::create(mode); // CORRECT
    

    By making the incorrect call, a usable stitcher object was created, but its internal storage (and hence all its initial default settings) were all nil/zero values. Due to these incorrect default values, the subsequent call to stitch was failing.

    Once the object was correctly created, the stitching executed successfully.