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::Exception'
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
GrabCut is an image segmentation method based on graph cuts. Starting with a user-specified bounding box around the object to be segmented, the algorithm estimates the color distribution of the target object and that of the background using a Gaussian mixture model.
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:
imageSize
has not a zero size) andGC_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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With