I am attempting image segmentation using GrabCut algorithm in OpenCV2.1 (C++)
Here my code:
Mat rgbWorkImage = imread(argv[1]);
Mat mask;
mask = Scalar(0);
Mat bgdModel, fgdModel;
Rect rect = Rect(Point(0,0), imageSize);
grabCut(rgbWorkImage, mask, rect, bgdModel, fgdModel, 0, GC_INIT_WITH_RECT);
grabCut(rgbWorkImage, mask, rect, bgdModel, fgdModel, 2, GC_EVAL);
Unfortunately I am getting this runtime error:
OpenCV Error: Assertion failed (!bgdSamples.empty() && !fgdSamples.empty()) in initGMMs, file /build/buildd/opencv-2.1.0/src/cv/cvgrabcut.cpp, line 368
terminate called after throwing an instance of 'cv::Excep开发者_StackOverflow社区tion'
what(): /build/buildd/opencv-2.1.0/src/cv/cvgrabcut.cpp:368: error: (-215) !bgdSamples.empty() && !fgdSamples.empty() in function initGMMs
What am I missing here?
Thanks
One case where that error could happen is when your image has zero for either its width or height (but not for both) because of this bug: https://code.ros.org/trac/opencv/ticket/691 (which seems to be fixed after OpenCV 2.1).
If the image dimensions are non zero, you should also check that the ROI rect:
- is not empty (
imageSize
has not a zero size) and - doesn't cover the entire image.
GC_INIT_WITH_RECT
marks all pixels outside the given rect as "background" and all pixels inside the rect as "probably foreground", and the assert expect that there is pixels in both foreground (or "probably foreground") and background (or "probably background") list.
@alexisdm is right,
my rect was rect = (0,0,img.shape[0],img.shape[1]) because I did not know, where foreground and background is.
The solution is not to cover the entire image by changing it to rect = (1,1,img.shape[0],img.shape[1]).
精彩评论