抓取功能中的错误-Objective-C ++ OpenCV [英] Error in grabcut function - Objective-C++ OpenCV
问题描述
我用OpenCV库用Objective-C ++编写的抓取算法有问题. 我想用抓斗来探测球,就像在这张照片中看到的那样:
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:
这是我的代码:
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!
推荐答案
根据错误消息,代码正在尝试分配巨大的图像.
The code is trying to allocate a huge image, according to the error message.
我会直接做的几件事:
-
替换
replace
NSInteger xInt = [x integerValue]; NSInteger yInt = [y integerValue];
NSInteger xInt = [x integerValue]; NSInteger yInt = [y integerValue];
使用
int xInt = [x intValue];
int yInt = [y intValue];
因为我不太确定cv :: Rect()是否会获取发送到此方法的实际输入.
because I am not very sure if cv::Rect() will be getting the actual inputs sent to this method.
- 添加检查以查看矩形的原点是否仍在图像内,如果矩形的原点不在图像中,则对其进行调整(不过,grabCut似乎可以解决这个问题).
我将代码转换为C ++,并且在摆脱了NSIntegers之后,它运行没有错误.在手机上尝试此操作后将更新答案,但请尝试上述更改.
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.
这篇关于抓取功能中的错误-Objective-C ++ OpenCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!