Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

lazysnapping

Segment image into foreground and background using graph-based segmentation

Syntax

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

Description

example

BW = lazysnapping(A,L,foremask,backmask) segments the image A 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(A,L,foreind,backind) segments the image A into foreground and background regions. foreind andbackind specify the linear indices of the pixels in the image marked as foreground and background, respectively

example

BW = lazysnapping(V,___) segments the volume V into foreground and background regions.

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 real, finite, nonsparse numeric array. For double and single images, lazysnapping assumes the range of the image to be [0 1]. For uint16, int16, and uint8 images, lazysnapping assumes the range to be the full range for the given data type. If the values do not match the expected range based on the data type, scale the image to the expected range or adjust EdgeWeightScaleFactor to improve results.

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

Input volume, specified as a real, finite, nonsparse numeric array.

Example:

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

Label matrix of the input image or volume, specified as numeric array. For 2-D grayscale images and 3-D volumetric grayscale images, the size of L must match the size of the input image A. For color images and multichannel images, L must be a 2-D array where the first two dimensions match the first two dimensions of the input image A.

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, 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 grayscale images and 3-D volumetric grayscale images, the size of foremask must match the size of the input image A. For color images and multichannel images, foremask must be a 2-D array where the first two dimensions match the first two dimensions of the input image A.

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 grayscale images and 3-D volumetric grayscale images, the size of backmask must match the size of the input image A. For color images and multichannel images, backmask must be a 2-D array where the first two dimensions match the first two dimensions of the input image A.

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

Linear index of pixels in the label matrix, specified as a numeric vector.

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

Linear index of pixels that define the background, specified as a numeric vector.

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 single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example:

collapse all

Connectivity of connected components, specified as 4 or 8, for 2-D images, and 6, 18, or 26 for 3-D images (volumes).

Example:

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 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

Output image, returned as a numeric array 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


Was this topic helpful?