activecontour

Segment image into foreground and background using active contour

Syntax

  • bw = activecontour(A,mask) example
  • bw = activecontour(A,mask,n)
  • bw = activecontour(A,mask,method) example
  • bw = activecontour(A,mask,n,method) example
  • bw = activecontour(___,Name,Value)

Description

example

bw = activecontour(A,mask) segments the 2-D grayscale image A into foreground (object) and background regions using active contour based segmentation. The output image bw is a binary image where the foreground is white (logical true) and the background is black (logical false). mask is a binary image that specifies the initial position of the active contour. The boundaries of the object region(s) (white) in mask define the initial contour position used for contour evolution to segment the image. To obtain faster and more accurate segmentation results, specify an initial contour position that is close to the desired object boundaries.

bw = activecontour(A,mask,n) segments the image by evolving the contour for a maximum of n iterations. If you do not specify n, activecontour uses the default value 100. You might need to specify higher values of n to achieve the desired segmentation result if the initial contour position (specified by the region boundaries in mask) is far from the object boundaries.

example

bw = activecontour(A,mask,method) specifies the active contour method used for segmentation, either 'Chan-Vese' or ‘edge'.

example

bw = activecontour(A,mask,n,method) segments the image by evolving the contour for a maximum of n iterations using the specified method.

bw = activecontour(___,Name,Value) specifies parameters that control various aspects of the segmentation. Parameter names can be abbreviated, and case does not matter.

Examples

expand all

Segment an Image Specifying the Mask

Read image and display it.

I = imread('coins.png');
imshow(I)
title('Original Image');

Specify 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);
  
figure, imshow(bw);
title('Segmented Image');

Segment Image Overlaying Mask and Contour on Original Image

This example shows how to segment an image, overlaying the mask and contour on the original 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.

contour(mask,[0.5 0.5],'b');

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.

contour(bw,[0.5 0.5],'r');
legend('Initial Contour','Final Contour');

Display segmented image.

figure, imshow(bw)
title('Segmented Image');

Segment an Image Specifying a Polygonal Mask Created Interactively

Read image and display it, along with 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');

Input Arguments

expand all

A — Image to be segmentedgrayscale image

Image to segmented, specified as a grayscale image. Must be non-sparse.

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

mask — Initial contour at which the evolution of the segmentation beginsbinary image

Initial contour at which the evolution of the segmentation begins, specified as a binary image the same size as A.

Data Types: logical

n — Maximum number of iterations to perform in evolution of the segmentation100 (default) | numeric scalar.

Maximum number of iterations to perform in evolution of the segmentation, specified as a numeric scalar. activecontour stops the evolution of the active contour if the contour position in the current iteration is the same as one of the contour positions from the most recent five iterations, or if the maximum number of iterations is reached.

You might need to specify higher values of n to achieve desired segmentation results if the initial contour position (specified by the region boundaries in mask) is far from the desired object boundaries.

Data Types: double

method — Active contour method used for segmentation‘Chan-Vese' (default) | 'edge'

Active contour method used for segmentation, specified as the character string 'Chan-Vese' or 'edge'. The Chan and Vese's region-based energy model is described in [1]. The edge-based model, similar to Geodesic Active Contour, is described in [2].

Data Types: char

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: bw = activecontour(I, mask, 200, 'edge','SmoothFactor',1.5);

'SmoothFactor' — Degree of smoothness or regularity of the boundaries of the segmented regions0, for ‘Chan-Vese'; 1 for 'edge' (default) | positive numeric scalar

Degree of smoothness or regularity of the boundaries of the segmented regions, specified as 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.

Example: bw = activecontour(I, mask, 200, 'edge','SmoothFactor',1.5);

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

'ContractionBias' — Tendency of the contour to grow outwards or shrink inwards0, for ‘Chan-Vese'; 0.3 for 'edge' (default) | scalar

Tendency of the contour to grow outwards or shrink inwards, specified as a scalar. Positive values bias the contour to shrink inwards (contract). Negative values bias the contour to grow outwards (expand). Note that this parameter does not guarantee that the contour will contract (or expand) strictly. It is possible that even with a positive value for this parameter, the contour could actually expand (or vice-versa). However, since you specified a bias, the expansion will be slower than an unbiased contour.

Example: bw = activecontour(I, mask, 200, 'edge','ContractionBias',0.4);

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

Output Arguments

expand all

bw — Segmented imagebinary image the same size as the input image A.

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

More About

expand all

Tips

  • activecontour uses the boundaries of the regions in mask as the initial state of the contour from where the evolution starts. If mask has regions with holes, unpredictable results may be seen. Use imfill to fill any holes in the regions in mask.

  • The regions in mask should not touch the image borders. If a region touches the image border(s), activecontour removes a single-pixel layer from the region so that the region does not touch the image border before further processing.

  • To get faster and more accurate results, specify an initial contour position that is close to the desired object boundaries. This is especially true for the 'edge' method.

  • For the 'edge' method, the active contour is naturally biased towards shrinking inwards (collapsing) by default, i.e. in absence of any image gradient, the active contour shrinks on its own. This is unlike the 'Chan-Vese' method where, by default, the contour is unbiased, i.e. free to either shrink or expand based on the image features.

  • To achieve an accurate segmentation result with the 'edge' method, the initial contour (specified by region boundaries in mask) should lie outside the boundaries of the object to be segmented, because the active contour is biased to shrink by default.

  • The 'Chan-Vese' method [1] may not segment all objects in the image, if the various object regions are of significantly different grayscale intensities. For example, if the image has some objects that are brighter than the background and some that are darker, the 'Chan-Vese' method typically segments out either the dark or the bright objects only.

Algorithms

activecontour uses the Sparse-Field level-set method, similar to the method described in [3], for implementing active contour evolution.

activecontour stops the evolution of the active contour if the contour position in the current iteration is the same as one of the contour positions from the most recent five iterations, or if the maximum number of iterations has been reached.

References

[1] T. F. Chan, L. A. Vese, Active contours without edges. IEEE Transactions on Image Processing, Volume 10, Issue 2, pp. 266-277, 2001

[2] V. Caselles, R. Kimmel, G. Sapiro, Geodesic active contours. International Journal of Computer Vision, Volume 22, Issue 1, pp. 61-79, 1997.

[3] 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.

Was this topic helpful?