This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

lazysnapping

Segment image into foreground and background using graph-based segmentation

Syntax

BW = lazysnapping(I,L,foremask,backmask)
BW = lazysnapping(I,L,foreind,backind)
BW = lazysnapping(___,Name,Value)

Description

example

BW = lazysnapping(I,L,foremask,backmask) segments the image or volume I into foreground and background regions using lazy snapping. The label matrix L specifies the subregions of the image. foremask and backmask are masks designating pixels in the image as foreground and background, respectively.

BW = lazysnapping(I,L,foreind,backind) segments the image or volume I into foreground and background regions. foreind and backind specify the linear indices of the pixels in the image marked as foreground and background, respectively.

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

Examples

collapse all

Read an image into the workspace.

RGB = imread('peppers.png');

Mark locations on image as foreground.

figure; 
imshow(RGB)
h1 = impoly(gca,[34,298;114,140;195,135;...
    259,200;392,205;467,283;483,104],'Closed',false);

Convert the locations into linear indices.

foresub = getPosition(h1);
foregroundInd = sub2ind(size(RGB),foresub(:,2),foresub(:,1));

Mark locations on image as background.

figure; 
imshow(RGB)
h2 = impoly(gca,[130,52;170,32],'Closed',false);

Convert the locations into linear indices.

backsub = getPosition(h2);
backgroundInd = sub2ind(size(RGB),backsub(:,2),backsub(:,1));

Generate label matrix.

L = superpixels(RGB,500);

Perform lazy snapping.

BW = lazysnapping(RGB,L,foregroundInd,backgroundInd);

Create masked image.

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

Load 3-D volumetric image into the workspace.

D = load('mri.mat');
V  = squeeze(D.D);  

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

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

 figure; 
 imshow(bseed)

Place seed points into empty 3-D mask.

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

Generate a 3-D label matrix.

 L = superpixels3(V,500);

Segment the image into foreground and background using Lazy Snapping.

bw = lazysnapping(V,L,fmask,bmask);

Display the 3-D segmented image.

figure;
p = patch(isosurface(double(bw)));
p.FaceColor = 'red';
p.EdgeColor = 'none';
daspect([1 1 27/128]);
camlight; lighting phong

Input Arguments

collapse all

Input image, specified as a numeric array in one of these formats.

Image TypeData Format
2-D grayscale image2-D matrix of size h-by-w
2-D color image3-D array of size h-by-w-by-3
2-D multispectral image with c channels3-D array of size h-by-w-by-c
3-D volumetric grayscale image with depth d3-D array of size h-by-w-by-d

lazysnapping expects pixel values of data type double and single to be in the range [0, 1]. You can use the rescale function to adjust pixel values to the expected range. lazysnapping expects pixel values of data type uint16, int16, and uint8 to be the full range for the given data type. If the values do not match the expected range, scale the image to the expected range or adjust EdgeWeightScaleFactor to improve results.

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

Label matrix of the input image or volume, specified as numeric array. For 2-D images, L is a 2-D array of size h-by-w. For 3-D volumes, L is a 3-D array of size h-by-w-by-d.

Do not mark a given 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, then lazysnapping segments the region as background.

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

Mask image that defines the foreground, specified as a logical array. For 2-D images, foremask is a 2-D array of size h-by-w. For 3-D volumes, foremask is a 3-D array of size h-by-w-by-d.

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

Mask image that defines the background, specified as a logical array. For 2-D images, backmask is a 2-D array of size h-by-w. For 3-D volumes, backmask is a 3-D array of size h-by-w-by-d.

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

Linear index of pixels in the label matrix, specified as a vector of positive integers. You can use an ROI drawing tool, such as drawpolyline, to select points in the foreground. Then, convert the coordinates of the points to linear indices using sub2ind. If the coordinates are non-integer values, then round the values before converting to linear indices.

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

Linear index of pixels that define the background, specified as a vector of positive integers. You can use an ROI drawing tool, such as drawpolyline, to select points in the background. Then, convert the coordinates of the points to linear indices using sub2ind. If the coordinates are non-integer values, then round the values before converting to linear indices.

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

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: 'Connectivity',18

Connectivity of connected components, specified as one of the values in this table. The default connectivity is 8 for 2-D images, and 26 for 3-D images.

Value

Meaning

Two-Dimensional Connectivities

4-connected

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.

8-connected

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.

Three-Dimensional Connectivities

6-connected

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

18-connected

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

26-connected

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

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

Scale factor for edge weights between the subregions of the label matrix, specified as the comma-separated pair consisting of 'EdgeWeightScaleFactor' and a positive scalar. Typical values range from [10, 1000]. Increasing this value increases the likelihood that lazysnapping labels neighboring subregions together as either foreground or background.

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

Output Arguments

collapse all

Segmented image, returned as a numeric array of the same size as the label matrix, L.

Algorithms

The Lazy Snapping algorithm developed by Li et al. clusters foreground and background values using the K-means method. This implementation of the Lazy Snapping algorithm does not cluster similar foreground or background pixels. To improve performance, reduce the number of pixels with similar values that are identified as foreground or background.

References

[1] Y. Li, S. Jian, C. Tang, H. Shum. Lazy Snapping. In Proceedings from the 31st International Conference on Computer Graphics and Interactive Techniques, 2004.

Introduced in R2017a