GrabCut - bgdModel & fgdModel empty - Assertion error

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.