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:
- Internal energy
- 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 npimport matplotlib.pyplot as pltfrom skimage import datafrom skimage.segmentation import active_contourfrom skimage.filters import threshold_otsu, gaussianimport skimage.io as skioimage = 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 imagey, x = np.indices(image.shape)center_y, center_x = image.shape[0] // 2, image.shape[1] // 2radius = min(center_x, center_y) +100init_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 segmentationsnake = active_contour(image, init_contour, alpha=0.01, beta=0.004, gamma=0.005)# Plot the resultsfig, 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. Theas_gray=Trueargument 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 (yandx) that represent the indices of each pixel in the image. Thecenter_yandcenter_xvariables store the coordinates of the center of the image. Theradiusvariable is set to the minimum ofcenter_xandcenter_y, plus100pixels. Theinit_contourarray is created by generating250points on a circular contour usingnp.cosandnp.sinfunctions, centered at (150,100) with the calculated radius. -
Line 20: This line performs the active contour segmentation using the
active_contourfunction. It takes theimageas input, along with theinit_contourarray and the specified values ofalpha,beta, andgamma. The resulting snake contour is stored in thesnakevariable. -
Lines 23–28: These lines create a figure and axes object using
plt.subplots(). The grayscale image is displayed usingax.imshow(). The initial contour is plotted as a red dashed line usingax.plot()with--rspecifying the line style. The snake contour is plotted as abluesolid line.ax.axis('off')removes the axis ticks and labels. Finally,plt.show() displays the plot with the image and contours.
Free Resources