Skip to content

Level sets

Fact Value
Also called Morphological geodesic active contour, MorphGAC, Geodesic active contour
Status Implemented and tested
Family Segmentation and regions
Engine Science engine
Type Nonlinear, morphological, level set evolution
Output Labels
Operator shown as algorithm (exact)
Support radius global
Code engine/src/pixelnexus_engine/filters/contours.py, level_set
Operator code engine/src/pixelnexus_engine/filters/contours.py, level_set_representation
Packages scikit-image

Overview

This filter fits a loose selection to the edges of the image. It is the morphological geodesic active contour of Marquez-Neila, Baumela and Alvarez. A balloon force shrinks or grows the outline of the selection, one pixel at a time, and edges stop it. The outline can split and merge, so one box around several objects can end on each of them. The 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. A disk and a square on a dark ground with light noise, and a white box around both. The edge stopping image is white with a mottled texture, and dark rings along the edges of the disk and the square. Shrunk from the box, yellow outlines sit on the disk and the square, with a few tiny yellow specks left in the ground. Grown from small spots, the disk is outlined well, while the outline of the square has a dent on its left side. Four panels. A disk and a square on a dark ground with light noise, and a white box around both. The edge stopping image is white with a mottled texture, and dark rings along the edges of the disk and the square. Shrunk from the box, yellow outlines sit on the disk and the square, with a few tiny yellow specks left in the ground. Grown from small spots, the disk is outlined well, while the outline of the square has a dent on its left side.
A disk and a square with noise of sigma 10, and a box selected around both. Then the edge stopping image, the contour shrunk from the box, and the contour grown from a small square inside each object.

How it works

  1. Make the edge stopping image g from the gradient of the blurred luma.
  2. Start the contour from the selection.
  3. Dilate or erode it by one pixel where g is high, with the balloon force.
  4. Move pixels on the contour toward the edges.
  5. Smooth the contour, and repeat.

Mathematical foundation

\[ g = \frac{1}{\sqrt{1 + \alpha \, \lvert \nabla (G_\sigma * Y) \rvert}}, \qquad \frac{\partial u}{\partial t} = g \, \lvert \nabla u \rvert \left(\operatorname{div}\frac{\nabla u}{\lvert \nabla u \rvert} + \nu\right) + \nabla g \cdot \nabla u \]

The formula shows the default parameter values.

where

  • \(u\): The inside of the contour, 1 or 0
  • \(g\): The edge stopping image
  • \(\nu\): The balloon force
  • \(\alpha\): The steepness of g
  • \(G_\sigma\): A Gaussian of sigma

u is 1 inside the contour and 0 outside, and starts as the selection. The image g is near 1 in flat areas and near 0 on edges. The equation is the geodesic active contour of Caselles, Kimmel and Sapiro. Marquez-Neila, Baumela and Alvarez replaced each of its terms with a morphological step. The balloon force nu becomes a dilation or erosion by one pixel. It acts only where g is above a threshold divided by the size of nu. The scikit-image code sets the threshold to the 40th percentile of g. The term grad g dot grad u pulls the contour into the valleys of g, the edges. The curvature term becomes a smoothing with the operators SI and IS, run the given number of times. Every step only switches pixels between 0 and 1, so there is no step size to choose.

The geodesic active contour of Caselles, Kimmel and Sapiro moves a curve so that it becomes the shortest path in a space where edges are cheap. Written as a level set, it is a partial differential equation, which needs a careful step size.

Marquez-Neila, Baumela and Alvarez showed that each term has a morphological twin. The balloon term is a dilation or an erosion. The curvature term is a pair of operators, SI and IS, that remove small bumps and dents. So each step only switches pixels between inside and outside, and there is no step size to choose.

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, Boundaries over the image, Edge stopping image The inside of the final contour as a label map, drawn over the image and listed in the Features panel. Or its boundary in yellow over the image. Or the edge stopping image g as gray, white in flat areas and dark on edges.
Balloon force balloon -1 -5 to 5 Below 0 the contour shrinks, above 0 it grows, by one pixel per step where the image is flat enough. A larger size lets it push past weaker edges. scikit-image uses 0.
Iterations iterations 100 1 to 1000 The number of steps. Each step moves the contour by at most about one pixel.
Smoothing smoothing 1 0 to 4 How many times the curvature smoothing runs in each step. scikit-image uses 1.
Edge smoothing sigma 2 0.5 to 10 px The sigma of the Gaussian in the gradient of the edge stopping image. scikit-image uses 5. We use 2, which kept the contour closer to a disk with a sharp edge in our tests.
Edge contrast alpha 100 1 to 1000 The steepness alpha of the edge stopping image. scikit-image uses 100.

Implementation

level_set in engine/src/pixelnexus_engine/filters/contours.py makes the edge stopping image with skimage.segmentation.inverse_gaussian_gradient on the luma from 0 to 1. Then skimage.segmentation.morphological_geodesic_active_contour starts from the selected pixels, those above 127. The threshold is scikit-image's automatic one, the 40th percentile of g. The filter stops with a message when the selection is empty.

The selection is only an input here. Its catalog entry sets selectionRole: contour, so the studio runs the filter on the whole image. The inside is a label map with one label. See the science engine for how label maps travel.

Worked example

Start Iterations Smoothing Parts Overlap
Box, shrink 10 1 5 0.370
Spots, grow 10 1 2 0.334
Box, shrink 25 1 17 0.638
Spots, grow 25 1 2 0.906
Box, shrink 50 1 17 0.938
Spots, grow 50 1 2 0.954
Box, shrink 100 1 18 0.975
Spots, grow 100 1 2 0.954
Box, shrink 200 1 18 0.975
Spots, grow 200 1 2 0.954
Box, shrink 100 2 2 0.990
Spots, grow 100 2 2 0.961

The image and selections of the figure, with the other defaults. The disk and the square are 2 parts. The overlap is the area of the inside and both objects together divided by the area of either.

From the box, the contour reaches the objects after about 100 steps. With the default smoothing, noise in the ground leaves 16 small specks. There, g is low enough that the balloon cannot erode them. A smoothing of 2 removes them and gives an overlap of 0.990.

Grown from small spots, the contour stops early on the left side of the square, where the noise stopped it. The figure shows the dent. More steps do not help, since the contour no longer moves.

Complexity

  • Time: \(O(N K)\)
  • Memory: \(O(N)\)

N is the number of pixels and K the number of steps.

Artifacts and limitations

Limitations

  • Needs edges. A soft border lets the balloon force push through.
  • It needs a selection near the object.

Artifacts

  • Contours that leak through gaps in an edge.
  • Corners rounded off by the smoothing.

Tests

The result must be scikit-image's from the selection. On a noisy disk, the contour must shrink from a box and grow from a small disk to an overlap above 0.98. The edge stopping output must be g times 255. An empty selection must stop with a message. The representation must have its four stages.

References

  • Marquez-Neila, P., Baumela, L., and Alvarez, L. A morphological approach to curvature-based evolution of curves and surfaces. IEEE Transactions on Pattern Analysis and Machine Intelligence 36(1), 2 to 17, 2014. DOI 10.1109/TPAMI.2013.106.
  • Caselles, V., Kimmel, R., and Sapiro, G. Geodesic active contours. International Journal of Computer Vision 22(1), 61 to 79, 1997. DOI 10.1023/A:1007979827043.
  • scikit-image documentation, skimage.segmentation.morphological_geodesic_active_contour.