Skip to contents

grabCut performs image segmentation (i.e., partition of the image into coherent regions) using the GrabCut method.

Usage

grabCut(
  image,
  mask,
  rect = rep(1, 4),
  bgdModel,
  fgdModel,
  iter = 1,
  mode = "EVAL"
)

Arguments

image

An 8-bit (8U), 3-channel Image object to segment.

mask

An 8-bit (8U), single-channel Image object. Each pixel can take any of the following 4 values:

  • 0: an obvious background pixels.

  • 1: an obvious foreground (object) pixel.

  • 2: a possible background pixel.

  • 3: a possible foreground pixel.

rect

A vector defining the region of interest containing a segmented object. The pixels outside of the region of interest are marked as "obvious background". rect must be a 4-element numeric vector which elements correspond to - in this order - the x and y coordinates of the bottom left corner of the region of interest, and to its width and height. The parameter is only used when mode="RECT" (default: rep(1, 4)).

bgdModel

A 1x65, single-channel, 64-bit (64F) Image object to set and store the parameters of the background model.

fgdModel

A 1x65, single-channel, 64-bit (64F) Image object to set and store the parameters of the foreground model.

iter

Number of iterations (default: 1) the algorithm should make before returning the result. Note that the result can be refined with further calls with mode="MASK" or mode="MASK".

mode

A character string indicating the operation mode of the function. It can be any of the following:

  • "RECT": The function initializes the state and the mask using the provided rect. After that it runs iter iterations of the algorithm.

  • "MASK":The function initializes the state using the provided mask.

  • "EVAL":The value means that the function should just resume.

  • "FREEZE":The value means that the function should just run the grabCut algorithm (a single iteration) with the fixed model.

Value

This function returns nothing. It modifies in place mask,

bgdModel, and fgdModel.

See also

Author

Simon Garnier, garnier@njit.edu

Examples

balloon <- image(system.file("sample_img/balloon1.png", package = "Rvision"))
mask <- zeros(nrow(balloon), ncol(balloon), 1)
bgdModel <- zeros(1, 65, 1, "64F")
fgdModel <- zeros(1, 65, 1, "64F")
grabCut(balloon, mask, c(290, 170, 160, 160), bgdModel, fgdModel, iter = 5, mode = "RECT")