Image Processing: Model based segmentation
Model-Based Segmentation: Active Contours
Model-Based Segmentation is a top-down approach where a geometric model or a mathematical energy function evolves to fit the objects in an image. Unlike data-based methods (like thresholding) which look at pixels individually, model-based methods consider the connectivity and smoothness of the object's boundary.
This article explores the Active Contours (or "Snakes") algorithm, specifically using the Chan-Vese method to segment objects like coins.
Theoretical Background
Active Contours (Snakes)
An Active Contour is a curve that moves through an image to minimize an energy function. The curve is influenced by two forces:
- Internal Forces: These keep the curve smooth and continuous (preventing it from becoming jagged or breaking).
- External Forces: These pull the curve toward image features like edges or region boundaries.
The Chan-Vese Method
The standard Active Contour relies on gradients (edges). However, the Chan-Vese model is a region-based active contour.
- It does not look for edges directly.
- Instead, it tries to separate the image into two regions (inside the curve and outside the curve) such that the pixel intensities inside are homogeneous and the pixel intensities outside are homogeneous.
- This makes it robust to noise and able to detect objects whose boundaries are not clearly defined by sharp gradients.
MATLAB Implementation
The following MATLAB script demonstrates the evolution of an active contour over several iterations.
Code Logic
% Model-Based Segmentation
% Algorithm: Active Contours
clc; clear; close all;
% 1. Load Image
% 'coins.png' is a standard MATLAB demo image
I = imread('coins.png');
% 2. Define Initial Mask
% The algorithm needs a starting point. We create a rectangle slightly
% smaller than the image border. The curve will shrink/expand from here.
mask = zeros(size(I));
mask(10:end-10, 10:end-10) = 1;
% 3. Run Active Contours
% We run the algorithm for different numbers of iterations to visualize the process.
% 100 Iterations: The curve starts shrinking but hasn't separated objects yet.
bw_100 = activecontour(I, mask, 100, 'Chan-Vese');
% 300 Iterations: The curve begins to snap around individual objects.
bw_300 = activecontour(I, mask, 300, 'Chan-Vese');
% 500 Iterations: The curve has fully tightened around the coins.
bw_500 = activecontour(I, mask, 500, 'Chan-Vese');
% 4. Visualization
figure('Name', 'Task 5: Active Contour Evolution (0-500)', ...
'NumberTitle', 'off', ...
'Position', [50, 100, 1600, 400]);
% --- Original + Initial Mask ---
subplot(1, 4, 1);
imshow(I); hold on;
visboundaries(mask, 'Color', 'b', 'LineWidth', 2);
title({'1. Start', '(Initial Blue Mask)'}, 'FontSize', 11);
% --- 100 Iterations ---
subplot(1, 4, 2);
imshow(I); hold on;
visboundaries(bw_100, 'Color', 'y', 'LineWidth', 2);
title({'2. 100 Iterations', '(Under-segmented)'}, 'FontSize', 11);
% --- 300 Iterations ---
subplot(1, 4, 3);
imshow(I); hold on;
visboundaries(bw_300, 'Color', 'r', 'LineWidth', 2);
title({'3. 300 Iterations', '(Converged)'}, 'FontSize', 11, 'FontWeight', 'bold');
% --- 500 Iterations ---
subplot(1, 4, 4);
imshow(I); hold on;
visboundaries(bw_500, 'Color', 'g', 'LineWidth', 2);
title({'4. 500 Iterations', '(Stable / Tight Fit)'}, 'FontSize', 11);
Analysis of Results
The active contour is an iterative process.
- Initialization: We start with a generic box (blue). This is the initial guess.
- Evolution: As iterations proceed, the box shrinks.
- Topology Change: A key feature of the level-set implementation (used in Chan-Vese) is that the curve can split. Notice how the single blue box eventually splits into multiple separate circles (red/green) to wrap around each coin individually.
- Convergence: At 500 iterations, the contours stop moving because the energy function has reached a minimum—the regions inside the circles are bright (coins) and the region outside is dark (background).

Key MATLAB Functions
activecontour(I, mask, n, method): Evolves the segmentation.n: Number of iterations.method: 'Chan-Vese' (region-based) or 'edge' (gradient-based).
visboundaries: A helper function to overlay binary mask boundaries onto the original image for visualization.