Main Content

grabcut

Segment image into foreground and background using iterative graph-based segmentation

Description

example

BW = grabcut(A,L,ROI) segments the image A into foreground and background regions. The label matrix L specifies the subregions of the image. ROI is a logical mask designating the initial region of interest.

BW = grabcut(A,L,ROI,foremask,backmask) segments the image A, where foremask and backmask are masks designating pixels in the image as foreground and background, respectively.

BW = grabcut(A,L,ROI,foreind,backind) segments the image A, where foreind and backind specify the linear indices of the pixels in the image marked as foreground and background, respectively.

BW = grabcut(___,Name,Value) segments the image using name-value pairs to control aspects of the segmentation.

Examples

collapse all

Read an RGB image into the workspace.

RGB = imread('peppers.png');

Generate label matrix.

L = superpixels(RGB,500);

Specify a region of interest and create a mask image.

imshow(RGB)
h1 = drawpolygon('Position',[72,105; 1,231; 0,366; 104,359;...
        394,307; 518,343; 510,39; 149,72]);

Figure contains an axes object. The axes object contains 2 objects of type image, images.roi.polygon.

roiPoints = h1.Position;
roi = poly2mask(roiPoints(:,1),roiPoints(:,2),size(L,1),size(L,2));

Perform the grab cut operation, specifying the original image, the label matrix and the ROI.

BW = grabcut(RGB,L,roi);
imshow(BW)

Figure contains an axes object. The axes object contains an object of type image.

Create masked image.

maskedImage = RGB;
maskedImage(repmat(~BW,[1 1 3])) = 0;
imshow(maskedImage)

Figure contains an axes object. The axes object contains an object of type image.

Load 3-D volumetric data.

load mristack
V = mristack;

Create a 2-D mask for initial foreground and background seed points.

seedLevel = 10;
fseed = V(:,:,seedLevel) > 75;
bseed = V(:,:,seedLevel) == 0;

Display foreground and background seed points.

imshow(fseed)

Figure contains an axes object. The axes object contains an object of type image.

imshow(bseed)

Figure contains an axes object. The axes object contains an object of type image.

Place seed points into empty 3-D mask.

fmask = zeros(size(V));
bmask = fmask;
fmask(:,:,seedLevel) = fseed;
bmask(:,:,seedLevel) = bseed;

Create initial region of interest.

roi = false(size(V));
roi(10:end-10,10:end-10,:) = true;

Generate label matrix.

L = superpixels3(V,500);

Perform GrabCut.

bw = grabcut(V,L,roi,fmask,bmask);

Display 3D segmented image.

montage(reshape(bw,size(V)))

Figure contains an axes object. The axes object contains an object of type image.

Input Arguments

collapse all

Input image or volume, specified as a 2-D grayscale image, 2-D truecolor image, or 3-D grayscale volume. Only grayscale images can be data type int16.

Data Types: single | double | int16 | uint8 | uint16

Label matrix, specified as a numeric array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Region of interest, specified as a logical array. All pixels that define the region of interest are equal to true.

Data Types: logical

Foreground mask, specified as a logical array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Background mask, specified as a logical array.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Indices of pixels in foreground, specified as a vector of linear indices.

Data Types: double

Indices of pixels in background, specified as a vector of linear indices.

Data Types: double

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: grabcut(A,L,ROI,Connectivity=4)

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: grabcut(A,L,ROI,'Connectivity',4)

Connectivity of connected components, specified as one of the following values. The default connectivity is 8 for 2–D images, and 26 for 3–D images.

Value

Meaning

Two-Dimensional Connectivities

4

Pixels are connected if their edges touch. Two adjoining pixels are part of the same object if they are both on and are connected along the horizontal or vertical direction.

Center pixel connected to four pixels

Current pixel is shown in gray.

8

Pixels are connected if their edges or corners touch. Two adjoining pixels are part of the same object if they are both on and are connected along the horizontal, vertical, or diagonal direction.

Center pixel connected to eight pixels

Current pixel is shown in gray.

Three-Dimensional Connectivities

6

Pixels are connected if their faces touch. Two adjoining pixels are part of the same object if they are both on and are connected in:

  • One of these directions: in, out, left, right, up, and down

Center pixel connected to the faces of 6 pixels

Current pixel is center of cube.

18

Pixels are connected if their faces or edges touch. Two adjoining pixels are part of the same object if they are both on and are connected in:

  • One of these directions: in, out, left, right, up, and down

  • A combination of two directions, such as right-down or in-up

Center pixel connected to the faces of 6 pixels and the edges of 12 pixels

Current pixel is center of cube.

26

Pixels are connected if their faces, edges, or corners touch. Two adjoining pixels are part of the same object if they are both on and are connected in:

  • One of these directions: in, out, left, right, up, and down

  • A combination of two directions, such as right-down or in-up

  • A combination of three directions, such as in-right-up or in-left-down

Center pixel connected to the faces of 6 pixels, the edges of 12 pixels, and the corners of 8 pixels

Current pixel is center of cube.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Maximum number of iterations performed by the algorithm. The algorithm can converge to a solution before reaching the maximum number of iterations.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

collapse all

Segmented image, returned as binary image of the same size as the label matrix L.

Tips

  • For double and single images, grabcut assumes the range of the image to be [0 1]. For uint16, int16, and uint8 images, grabcut assumes the range to be the full range for the given data type.

  • For grayscale images, the size of L, foremask, and backmask must match the size of the image A. For color and multi-channel images, L, foremask, and backmask must be 2-D arrays with the first two dimensions identical to the first two dimensions of the image A.

Algorithms

  • The algorithm treats all subregions fully or spatially outside the ROI mask as belonging to the background. To get an optimal segmentation, make sure the object to be segmented is fully contained within the ROI, surrounded by a small number of background pixels.

  • Do not mark a subregion of the label matrix as belonging to both the foreground mask and the background mask. If a region of the label matrix contains pixels belonging to both the foreground mask and background mask, the algorithm treats the region as unmarked.

  • The algorithm assumes all subregions outside the region of interest belong to the background. Marking one of these subregions as belonging to foreground or background mask has no effect on the resulting segmentation.

References

[1] Rother, C., V. Kolmogorov, and A. Blake. "GrabCut - Interactive Foreground Extraction using Iterated Graph Cuts". ACM Transactions on Graphics (SIGGRAPH). Vol. 23, Number 3, 2004, pp. 309–314.

Version History

Introduced in R2018a