1

I'm using OpenCV in an iOS app via Objective-C wrapper and it works generally fine but an attempt to read a background image from a background subtractor crashes the app:

cv::Ptr<BackgroundSubtractor> backSub = createBackgroundSubtractorMOG2();
for (int i = 0; i < arr.count; i++) { // arr - NSArray of UIImage
    Mat mask;
    Mat frame = [OpenCVWrapper _matFrom:arr[i]]; // converts UIImage to OpenCV.Mat
    backSub->apply(frame, mask); // this works great, I can read the mask and it's valid
}
    
Mat background;
backSub->getBackgroundImage(background); // this crashes

The error implies that my frame has an invalid format. But in my case, I'm just creating a variable to be used by the getBackgroundImage. I tried to create a new Mat with the expected type CV_8UC1 (and CV_8UC3) with no luck, the app is still crashing with the same error.

libc++abi: terminating with uncaught exception of type cv::Exception: OpenCV(4.3.0) /Volumes/build-storage/build/master_iOS-mac/opencv/modules/video/src/bgfg_gaussmix2.cpp:929: error: (-215:Assertion failed) frameType == CV_8UC1 || frameType == CV_8UC3 || frameType == CV_32FC1 || frameType == CV_32FC3 in function 'getBackgroundImage'

dyld4 config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktraceRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib

Same code works flawlessly in my python test (declare variable and store the background there):

background = backSub.getBackgroundImage()

How should I properly access a background image from createBackgroundSubtractorMOG2 in Objective-C?

DreamTeam Mobile
  • 531
  • 3
  • 15

0 Answers0