GrabCut - bgdModel & fgdModel empty - approval error

I am trying to segment an image using the GrabCut algorithm in OpenCV2.1 (C ++)

Here is 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 get 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?

thank

+5
source share
1 answer

One case where this error can occur is when your image has zero for its width or height (but not both) due to this error: https://code.ros.org/trac/opencv/ticket/691 (which seems to be fixed after OpenCV 2.1).

If the image dimensions are not zero, you should also check that the ROI rect:

  • (imageSize )
  • .

GC_INIT_WITH_RECT "", - ", ", , (, , ), ( ", " ).

+8

All Articles