Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GrabCut - bgdModel & fgdModel empty - Assertion error

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

like image 458
Mayank Avatar asked Sep 25 '11 14:09

Mayank


People also ask

What is GrabCut algorithm?

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.


1 Answers

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.

like image 61
alexisdm Avatar answered Sep 23 '22 15:09

alexisdm