Skip to content

Active contours

Fact Value
Also called Snakes, Snake
Status Implemented and tested
Family Segmentation and regions
Engine Science engine
Type Nonlinear, iterative, energy minimization
Output Labels
Operator shown as algorithm (exact)
Support radius global
Code engine/src/pixelnexus_engine/filters/contours.py, active_contour_filter
Operator code engine/src/pixelnexus_engine/filters/contours.py, active_contour_representation
Packages scikit-image, SciPy

Overview

An active contour, or snake, is a closed chain of points. Kass, Witkin and Terzopoulos gave it an energy, like an elastic band that resists bending, and let it slide onto edges. This filter puts the points on the outline of your selection and lets them move. The snake stays one smooth closed curve. Its inside goes to the Features panel and is drawn over the image. Click its row number to make it the selection. The filter runs in the PixelNexus engine, with scikit-image.

Four panels of a disk and a square on a dark ground with light noise. From a blue box around both objects, the yellow snake wraps the disk and the square in one curve, with a narrow bridge between them. From a blue box around the disk, the yellow snake sits on the edge of the disk. With a rigidity of 10, the yellow snake follows the disk on the left but bulges out toward the square on the right. Four panels of a disk and a square on a dark ground with light noise. From a blue box around both objects, the yellow snake wraps the disk and the square in one curve, with a narrow bridge between them. From a blue box around the disk, the yellow snake sits on the edge of the disk. With a rigidity of 10, the yellow snake follows the disk on the left but bulges out toward the square on the right.
A disk and a square with noise of sigma 10. Then the snake from a box around both, from a box around the disk, and from the same box with a rigidity of 10. The start is blue and the end yellow.

How it works

  1. Take the outline of the largest part of the selection, and space the points evenly on it.
  2. Blur the luma and find its edges with the Sobel filter.
  3. Move every point a little toward lower energy, keeping the chain tight and smooth.
  4. Stop when the snake settles, or after the most steps.

Mathematical foundation

\[ E = \sum_i \left( \alpha \lvert v_{i+1} - v_i \rvert^2 + \beta \lvert v_{i+1} - 2 v_i + v_{i-1} \rvert^2 \right) + \sum_i P(v_i), \qquad P = -\left( w_{line} \, Y_\sigma + w_{edge} \, \lvert \nabla Y_\sigma \rvert \right) \]

The formula shows the default parameter values.

where

  • \(v_i\): Point i of the snake
  • \(\alpha\): The elasticity
  • \(\beta\): The rigidity
  • \(P\): The image energy
  • \(Y_\sigma\): The luma after a Gaussian blur of sigma

The snake is a closed chain of points, called v. Its energy E has an internal part and an image part. The alpha term is small when the points are close, so it pulls the chain tight. The beta term is small when the chain is straight, so it resists bending. With a positive line weight, the image energy P is low on bright pixels. With a positive edge weight, it is low on edges. Y sigma is the blurred luma, and its gradient comes from the Sobel filter. Each step solves for the internal forces with a fixed matrix, as Kass, Witkin and Terzopoulos did, and moves down the image energy. The scikit-image code limits each move to the largest move. It stops when the snake is within the settled distance of one of its 10 saved positions.

The internal energy only cares about the shape of the chain. The image energy only cares about where the points are. A step balances the two. Kass, Witkin and Terzopoulos solved for the internal forces with one fixed matrix, so each step is a matrix product and the chain stays stable.

The snake only feels edges close to it. In a flat area, only the elasticity acts, and the snake shrinks. That is why it should start a little outside the object.

Kernel or operator

There is no kernel. The Inspector asks the engine for the stages with the current settings.

Parameters

Parameter Default Values Meaning
Output output Inside Inside, Start and end over the image The inside of the final snake as a label map, drawn over the image and listed in the Features panel. Or the starting snake in blue and the final snake in yellow over the image.
Elasticity alpha 0.05 0 to 1 The weight alpha of the length of the snake. scikit-image uses 0.01. We use 0.05, since at 0.01 a snake far from any edge barely shrank before it counted as settled in our tests.
Rigidity beta 0.1 0 to 50 The weight beta of the bending of the snake. scikit-image uses 0.1.
Edge weight wEdge 1 -5 to 5 How strongly edges attract the snake. scikit-image uses 1.
Brightness weight wLine 0 -5 to 5 How strongly bright pixels attract the snake. Negative values attract it to dark pixels. scikit-image uses 0.
Smoothing sigma 2 0 to 10 px The sigma of a Gaussian blur of the luma first, our choice. 0 turns it off.
Time step gamma 0.01 0.0001 to 1 The step size gamma. scikit-image uses 0.01.
Largest move maxMove 1 0.1 to 5 px The most a point moves in one step. scikit-image uses 1.
Points points 200 20 to 500 The number of points on the snake, evenly spaced along the selection outline. Our choice.
Iterations iterations 2500 1 to 10000 The most steps. scikit-image uses 2500.
Settled at convergence 0.1 0 to 1 px Every 11 steps, scikit-image compares the snake with its 10 saved positions. It stops when one of them is this close, measured as the largest move of any point in x plus y. scikit-image uses 0.1.

Implementation

active_contour_filter in engine/src/pixelnexus_engine/filters/contours.py takes the largest 8-connected part of the selection and fills its holes. The part is padded by one pixel. Then skimage.measure.find_contours at 0.5 gives its outline, half a pixel outside the selected pixels. The outline is resampled to evenly spaced points, our choice. Then skimage.segmentation.active_contour moves the points on the blurred luma, with a periodic boundary, so the snake stays closed.

The inside of the final snake comes from skimage.draw.polygon, as a label map with one label. The filter stops with a message when the snake shrinks to a line or a point. The selection is only an input. Its catalog entry sets selectionRole: contour, so the studio runs the filter on the whole image.

Worked example

Elasticity alpha Rigidity beta Mean radius (px) Spread (px) Overlap
0.01 0.1 28.8 3.62 0.970
0.01 10 30.7 5.80 0.889
0.05 0.1 27.7 0.09 0.983
0.05 10 30.4 5.50 0.879
0.1 0.1 27.6 0.09 0.978
0.1 10 27.6 0.08 0.976

The image of the figure, from the box around the disk, with the other defaults. The disk has a radius of 28 pixels. The mean radius and its spread, the standard deviation, are the distances of the final points from the center of the disk. The overlap is the area of the inside and the disk together divided by the area of either.

With scikit-image's elasticity of 0.01, the snake still sits a few pixels off the disk in places, with a spread of 3.62 pixels. In our first tests, from a box farther away, it barely moved before it counted as settled. So the default is 0.05, which lands within 0.3 pixels of the true radius with a spread under 0.1 pixels. A high rigidity keeps the snake from bending into the corner between the disk and the square, unless a strong elasticity pulls it in.

The figure also shows the main limit. From a box around both objects, the snake cannot split, so it wraps both in one curve.

Complexity

  • Time: \(O(N + n^3 + K n)\)
  • Memory: \(O(N + n^2)\)

N is the number of pixels, n the number of points and K the number of steps. The n^3 is one matrix inversion.

Artifacts and limitations

Limitations

  • It finds only nearby edges, so the selection must be close.
  • It cannot split to outline two objects.
  • Many settings that interact.

Artifacts

  • Corners cut off when the rigidity is high.
  • A snake that shrinks to a point when no edge stops it.

Tests

Evenly spaced points on a square must land on its corners and midpoints. The start must be the outline of the largest part of the selection, with its hole filled, half a pixel outside. The end must be scikit-image's on the blurred luma. From a box around a noisy disk, the snake must settle within 1 pixel of its radius. The contour output must draw the start in blue and the end in yellow. A snake on a flat image must stop with a message. The representation must have its four stages.

References

  • Kass, M., Witkin, A., and Terzopoulos, D. Snakes, active contour models. International Journal of Computer Vision 1(4), 321 to 331, 1988. DOI 10.1007/BF00133570.
  • scikit-image documentation, skimage.segmentation.active_contour.