What is the active contour model?

An active contour model, also known as a deformable or snakes model, is a computer vision technique used for object segmentation and boundary detection in images. It is particularly useful in applications such as image processing, computer graphics, and medical imaging.

Contour representation

The active contour model operates by iteratively deforming an initial contour to match the desired object boundary. The contour is typically represented as a parametric curve or a set of connected points.

Energy functional

The active contour model minimizes an energy functional that measures the contour’s fit to the desired object boundary and its smoothness.

The energy functional consists of two main terms:

  1. Internal energy
  2. External energy

Internal energy

The internal energy term encourages smoothness and regularity of the contour shape. It penalizes high curvatures, long contour segments, and other deformations that violate the desired properties of the contour.

External energy

The external energy term drives the contour towards the desired object boundary. It is computed based on the image characteristics, such as gradient magnitude, texture, or color. The external energy attracts the contour to edges or regions of interest in the image, making it conform to the object boundaries.

Optimization process

To find the optimal contour, an iterative process is employed. The contour is initialized near the object boundary, and then it evolves by minimizing the total energy functional. This evolution is achieved by updating the position of contour points or adjusting the parameters of the parametric curve. The contour is iteratively deformed until it reaches a stable configuration that best fits the desired object boundary.

Syntax

Here is the syntax to perform active contour segmentation:

snake = active_contour(image, init_contour, alpha=0.01, beta=0.004, gamma=0.005)

Parameters

  • image: Input image

  • init_contour: The initial contour is the starting point for the snake to evolve and converge to the desired contour.

  • alpha: It controls the elasticity of the snake. Higher alpha values make the snake more elastic, allowing it to deform more freely. Lower values make the snake less elastic, causing it to resist deformation and adhere more closely to the image edges. We can adjust the alpha value based on the snake’s desired flexibility.

  • beta: It represents the stiffness of the snake. Higher beta values make the snake stiff, causing it to contract and maintain a shorter length. Lower beta values make the snake less stiff, allowing it to expand and adapt to longer contours. You can adjust the beta value based on the snake’s desired stiffness.

  • gamma: It controls the influence of the image forces on the snake. The image forces attract the snake toward the edges or features in the image. Higher gamma values make the snake more influenced by the image forces, allowing it to conform tightly to the edges. Lower gamma values make the snake less influenced by the image forces, causing it to be less affected by the image features. You can adjust the value of gamma based on the desired influence of image features on the snake.

Let’s consider the following example of how active contour model works:

import numpy as np
import matplotlib.pyplot as plt
from skimage import data
from skimage.segmentation import active_contour
from skimage.filters import threshold_otsu, gaussian
import skimage.io as skio
image = skio.imread("starfish.png", as_gray=True)
# Create an initial contour
# The contour coordinates can be manually defined or obtained using other techniques
# For simplicity, let's create a circular contour in the center of the image
y, x = np.indices(image.shape)
center_y, center_x = image.shape[0] // 2, image.shape[1] // 2
radius = min(center_x, center_y) +100
init_contour = np.array([150 + radius * np.cos(np.linspace(0, 2*np.pi, 250)),
100 + radius * np.sin(np.linspace(0, 2*np.pi, 250))]).T
# Perform active contour segmentation
snake = active_contour(image, init_contour, alpha=0.01, beta=0.004, gamma=0.005)
# Plot the results
fig, ax = plt.subplots(figsize=(5, 5))
ax.imshow(image, cmap='gray')
ax.plot(init_contour[:, 0], init_contour[:, 1], '--r', lw=2)
ax.plot(snake[:, 0], snake[:, 1], '-b', lw=5)
ax.axis('off')
plt.show()

Code explanation

  • Lines 1–6: These lines import the required modules to perm active contour model on the image.

  • Line 8: It reads the image "starfish.png" and loads it into the image variable. The as_gray=True argument converts the image to grayscale, ensuring that the image variable contains a single-channel grayscale image.

  • Lines 13–17: These lines define an initial contour for the active contour segmentation. The y, x = np.indices(image.shape) line creates two arrays (y and x) that represent the indices of each pixel in the image. The center_y and center_x variables store the coordinates of the center of the image. The radius variable is set to the minimum of center_x and center_y, plus 100 pixels. The init_contour array is created by generating 250 points on a circular contour using np.cos and np.sin functions, centered at (150, 100) with the calculated radius.

  • Line 20: This line performs the active contour segmentation using the active_contour function. It takes the image as input, along with the init_contour array and the specified values of alpha, beta, and gamma. The resulting snake contour is stored in the snake variable.

  • Lines 23–28: These lines create a figure and axes object using plt.subplots(). The grayscale image is displayed using ax.imshow(). The initial contour is plotted as a red dashed line using ax.plot() with --r specifying the line style. The snake contour is plotted as a blue solid line. ax.axis('off') removes the axis ticks and labels. Finally, plt.show() displays the plot with the image and contours.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved