Runs the GrabCut algorithm
mask = cv.grabCut(img, rect)
mask = cv.grabCut(img, mask)
[mask, bgdmodel, fgdmodel] = cv.grabCut(...)
[...] = cv.grabCut(..., 'OptionName', optionValue, ...)
Input
- img Input 8-bit 3-channel image.
- rect ROI containing a segmented object. A 1-by-4 vector
[x y w h]
.
It will automatically create the mask
. The pixels outside of the ROI are
marked as "obvious background" with label 0, and label 3 for foreground
(see mask
). Using this variant will set Mode
to 'InitWithRect'.
- mask Input 8-bit single-channel mask of same size as
img
and type
uint8
. Its elements may have one of the following values:
- 0 an obvious background pixel
- 1 an obvious foreground (object) pixel
- 2 a possible background pixel
- 3 a possible foreground pixel
Output
- mask output 8-bit single-channel updated mask. The mask is initialized
by the function when
Mode
is set to 'InitWithRect' (see rect
).
- bgdmodel Output array for the background model, to be used for next
iteration. Do not modify it while you are processing the same image.
- fgdmodel Output array for the foreground model, to be used for next
iteration. Do not modify it while you are processing the same image.
Options
- BgdModel Initial array for the background model. A 1x65 double vector.
- FgdModel Initial array for the foreground model. A 1x65 double vector.
- IterCount Number of iterations the algorithm should make before
returning the result. Note that the result can be refined with further
calls with
Mode
as 'InitWithMask' or 'Eval'. Default 10
- Mode Operation mode, default 'Eval'. Could be one of the following:
- InitWithRect The function initializes the state and the mask using
the provided rectangle. After that it runs
IterCount
iterations of the
algorithm. This should only be used with the variant of the function
that takes rect
as input.
- InitWithMask The function initializes the state using the provided
mask
. Then, all the pixels outside of the ROI are automatically
initialized as background with label 0. This should only be used with
the variant of the function that takes mask
as input.
- Eval The value means that the algorithm should just resume.
The function implements the GrabCut image segmentation algorithm:
GrabCut.