I'm creating a Swift UI
application to process images on a device and use the OpenCV
library, specifically the BackgroundSubtractor algorithm. I was able to follow the OpenCV iOS guide and perform basic image operations, like take an image, convert to Matrix, make it gray or perform bitwise operations with OpenCV functions but when I try to use the BackgroundSubtractor.apply method, the app crashes with EXC_BAD_ACCESS
error:
Thread 1: EXC_BAD_ACCESS (code=2, address=0x10455aa40)
Here is my code:
(UIImage *)toMyGreatImage:(UIImage *)source {
Mat sourceMat = [OpenCVWrapper _matFrom:source];
Mat result;
BackgroundSubtractor *backSub = createBackgroundSubtractorMOG2();
Mat mask;
backSub->apply(sourceMat, mask); // crashes here
return [OpenCVWrapper _imageFrom:mask];
}
Just to compare, my other function works perfectly fine with the same source image:
(UIImage *)toGray:(UIImage *)source {
return [OpenCVWrapper _imageFrom:[OpenCVWrapper _grayFrom:[OpenCVWrapper _matFrom:source]]];
}
I have also tested the background subtractor code in python and it works as expected. What did I miss in my iOS setup?
CodePudding user response:
you need a "smart pointer" to capture your newly created BackgroundSubtractor instance (not a "raw" one), else it will self-destruct, and crash when you call a method on an invalid pointer:
cv::Ptr<BackgroundSubtractor> backSub = createBackgroundSubtractorMOG2();
(note, that cv::Ptr
is just an alias for std::shared_ptr
nowadays)
(also, tutorial)