Segment image into foreground and background using active contour
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 state 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,method,smoothfactor) segments the image using the specified smoothfactor. smoothfactor controls the degree of smoothness or regularity of boundaries of the segmented regions. Higher values produce smoother region boundaries but can also smooth out finer details. Lower values allow more irregularities (less smoothing) in the region boundaries but allow finer details of the region boundaries to be captured.
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');
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 0],'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 0],'r'); legend('Initial Contour','Final Contour'); % Display segmented image. figure, imshow(bw) title('Segmented Image');
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');
Image to segmented, specified as a grayscale image. Must be non-sparse.
Data Types: single | double | int8 | int16 | int32 | uint8 | uint16 | uint32
Initial contour at which the evolution of the segmentation begins, specified as a binary image the same size as A.
Data Types: logical
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
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:
Data Types: double
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  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.
activecontour uses the Sparse-Field level-set method, similar to the method described in , 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.
 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.