Reputation:
I have a problem with my grabcut algorithm written in Objective-C++ with the OpenCV library. I want to detect balls with grabcut like you can see in this picture:
Here is my code:
Mat imageMat;
Mat grab;
Mat result;
Mat bgdModel;
Mat fgdModel;
//x,y: user selection
NSInteger xInt = [x integerValue];
NSInteger yInt = [y integerValue];
UIImageToMat(image, imageMat);
cv::cvtColor(imageMat , grab , CV_RGBA2RGB);
cv::Rect rectangle( xInt-125,yInt-125,250,250);
cv::grabCut(grab, result, rectangle, bgdModel, fgdModel, 1, cv::GC_INIT_WITH_RECT );
cv::compare(result,cv::GC_PR_FGD,result,cv::CMP_EQ);
cv::Mat foreground(grab.size(),CV_8UC3, cv::Scalar(255,255,255));
result=result&1;
grab.copyTo(foreground, result);
return MatToUIImage(grab);
And here is the error message I get:
Boule(18731,0x1b3b33b40) malloc: *** mach_vm_map(size=1560002560) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
Do you have an idea what's wrong? Thanks a lot in advance!
Upvotes: 2
Views: 429
Reputation: 3408
The code is trying to allocate a huge image, according to the error message.
A few things that I would do straightaway:
replace
NSInteger xInt = [x integerValue]; NSInteger yInt = [y integerValue];
with
int xInt = [x intValue];
int yInt = [y intValue];
because I am not very sure if cv::Rect() will be getting the actual inputs sent to this method.
I converted the code to C++, and it runs with no error after getting rid of the NSIntegers. Will update the answer after trying this on a phone, but do try the above changes.
Upvotes: 1