Segment image into foreground and background using active contour
bw = activecontour(A,mask)
bw = activecontour(A,mask,n)
bw = activecontour(A,mask,method)
bw = activecontour(A,mask,n,method)
bw = activecontour(___,Name,Value)
bw = activecontour(
A into foreground (object) and background
regions using active contour segmentation. Active contour evolves
the segmentation using an iterative process. By default,
mask is a binary image that specifies
the initial state of the active contour. The boundaries of the object
regions (white) in
mask define the initial contour
position used for contour evolution to segment the image. The output
bw is a binary image where the foreground
is white (logical true) and the background is black (logical false).
To obtain faster and more accurate segmentation results, specify an initial contour position that is close to the desired object boundaries.
This example shows how to segment an image using the default settings of the
Read a grayscale image and display it.
I = imread('coins.png'); imshow(I) title('Original Image')
Specify the initial contour and display it.
mask = zeros(size(I)); mask(25:end-25,25:end-25) = 1; figure imshow(mask) title('Initial Contour Location')
Segment the image using the default method and 300 iterations.
bw = activecontour(I,mask,300);
Display the result.
figure imshow(bw) title('Segmented Image')
Read image and display it.
I = imread('toyobjects.png'); imshow(I) hold on title('Original Image');
Specify initial contour location close to the object that is to be segmented.
mask = false(size(I)); mask(50:150,40:170) = true;
Display the initial contour on the original image in blue.
Segment the image using the
'edge' method and 200 iterations.
bw = activecontour(I, mask, 200, 'edge');
Display the final contour on the original image in red.
visboundaries(bw,'Color','r'); title('Initial contour (blue) and final contour (red)');
Display segmented image.
figure, imshow(bw) title('Segmented Image');
Read image into the workspace and display it. Display instructions to specify initial contour location.
I = imread('toyobjects.png'); imshow(I) str = 'Click to select initial contour location. Double-click to confirm and proceed.'; title(str,'Color','b','FontSize',12); disp(sprintf('\nNote: Click close to object boundaries for more accurate result.'))
Specify initial contour interactively.
mask = roipoly; figure, imshow(mask) title('Initial MASK');
Segment the image, specifying 200 iterations.
maxIterations = 200; bw = activecontour(I, mask, maxIterations, 'Chan-Vese'); % Display segmented image figure, imshow(bw) title('Segmented Image');
Load 3-D volumetric image data, removing the singleton dimension.
D = load('mri.mat'); A = squeeze(D.D);
Create 2-D mask for initial seed points.
seedLevel = 10; seed = A(:,:,seedLevel) > 75; figure imshow(seed)
Create an empty 3-D seed mask and put the seed points into it.
mask = zeros(size(A)); mask(:,:,seedLevel) = seed;
Perform the segmentation using active contours, specifying the seed mask.
bw = activecontour(A,mask,300);
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
A— Image to be segmented
Image to segmented, specified as a nonsparse, 2-D or 3-D, numeric array.
mask— Initial contour at which the evolution of the segmentation begins
Initial contour at which the evolution of the segmentation begins,
specified as a binary image the same size as
For 2-D and 3-D grayscale images, the size of
match the size of the image
A. For color and
mask must be a 2-D logical
array where the first two dimensions match the first two dimensions
of the image
n— Maximum number of iterations to perform in evolution of the segmentation
Maximum number of iterations to perform in evolution of the
segmentation, specified as a numeric scalar.
the evolution of the active contour when it reaches the maximum number
activecontour also stops the evolution
if the contour position in the current iteration is the same as the
contour position in one of the most recent five iterations.
If the initial contour position (specified by
is far from the object boundaries, specify higher values of
achieve desired segmentation results.
method— Active contour method used for segmentation
comma-separated pairs of
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
bw = activecontour(I, mask, 200, 'edge','SmoothFactor',1.5);
'SmoothFactor'— Degree of smoothness or regularity of the boundaries of the segmented regions
'Chan-Vese'; 1 for
'edge'(default) | positive numeric scalar
Degree of smoothness or regularity of the boundaries of the
segmented regions, specified as the comma-separated pair consisting
'SmoothFactor' and a positive numeric scalar.
Higher values produce smoother region boundaries but can also smooth
out finer details. Lower values produce more irregularities (less
smoothing) in the region boundaries but allow finer details to be
captured. The default smoothness value depends on the method chosen.
bw = activecontour(I, mask, 200, 'edge','SmoothFactor',1.5);
'ContractionBias'— Tendency of the contour to grow outwards or shrink inwards
'edge'(default) | scalar
Tendency of the contour to grow outwards or shrink inwards,
specified as the comma-separated pair consisting of
a scalar. Positive values bias the contour to shrink inwards (contract).
Negative values bias the contour to grow outwards (expand). This parameter
does not guarantee that the contour contracts (or expands). It is
possible that even with a positive value for this parameter, the contour
could actually expand. However, by specifying a bias, you slow the
expansion when compared to an unbiased contour. Typical values for
this parameter are between -1 and 1.
bw = activecontour(I, mask, 200, 'edge','ContractionBias',0.4);
bw— Segmented image
Segmented image, returned as a binary image the same size as
the input image
A. The foreground is white (logical
true) and the background is black (logical false).
activecontour uses the boundaries
of the regions in
mask as the initial state of
the contour from where the evolution starts.
with holes can cause unpredictable results. Use
imfill to fill any holes in the regions
If a region touches the image borders,
a single-pixel layer from the region, before further processing, so
that the region does not touch the image border.
To get faster and more accurate results, specify an
initial contour position that is close to the desired object boundaries,
especially for the
'edge' method, the active
contour is naturally biased towards shrinking inwards (collapsing).
In the absence of any image gradient, the active contour shrinks on
its own. Conversely, with the
where the contour is unbiased, the contour is free to either shrink
or expand based on the image features.
To achieve an accurate segmentation with the
specify an initial contour that lies outside the boundaries of the
object. The active contour with the
is biased to shrink, by default.
If object regions are of significantly different grayscale
'Chan-Vese' method  might not segment all objects in the image. For
example, if the image contains objects that are brighter than the
background and some that are darker, the
typically segments out either the dark or the bright objects only.
activecontour uses the Sparse-Field level-set
method, similar to the method described in ,
for implementing active contour evolution.
 T. F. Chan, L. A. Vese, Active contours without edges. IEEE Transactions on Image Processing, Volume 10, Issue 2, pp. 266-277, 2001
 V. Caselles, R. Kimmel, G. Sapiro, Geodesic active contours. International Journal of Computer Vision, Volume 22, Issue 1, pp. 61-79, 1997.
 R. T. Whitaker, A level-set approach to 3d reconstruction from range data. International Journal of Computer Vision, Volume 29, Issue 3, pp.203-231, 1998.