Skip to content

Watershed

Fact Value
Also called Watershed transform, Watershed segmentation
Status Implemented and tested
Family Segmentation and regions
Engine Science engine
Type Nonlinear, morphological, flooding from markers
Output Labels
Operator shown as algorithm (exact)
Support radius global
Code engine/src/pixelnexus_engine/filters/segmentation.py, watershed_filter
Operator code engine/src/pixelnexus_engine/filters/segmentation.py, watershed_representation
Packages scikit-image, SciPy

Overview

The watershed treats the image as a height map. Here the height map is the gradient of the image, so edges are ridges and flat areas are valleys. Water rises from the valleys, and where floods from two valleys meet, a boundary forms. Every noise dip would be a valley of its own, so only valleys at least h deep start a flood. The regions go to the Features panel with their area, centroid and mean brightness, and are drawn over the image in their label colors. Click a row number in the panel to make that region the selection, or select all regions at once. The filter runs in the PixelNexus engine, with scikit-image.

Four panels. The Disks test image with grainy noise. The elevation is black with thin bright rings on the edges of the six disks. With the depth h at 2, the regions show each disk in its own color, but the ground is dotted with many small specks of other colors. With h at 20, the image has exactly seven regions, the six disks and the ground. Four panels. The Disks test image with grainy noise. The elevation is black with thin bright rings on the edges of the six disks. With the depth h at 2, the regions show each disk in its own color, but the ground is dotted with many small specks of other colors. With h at 20, the image has exactly seven regions, the six disks and the ground.
The Disks test image with noise of sigma 8. Then the elevation, the gradient after a blur of sigma 1, and the regions in their label colors with the depth h at 2 and at 20.

How it works

  1. Blur the luma and take its Sobel gradient as the elevation.
  2. Keep the minima of the elevation that are at least h deep, as markers.
  3. Flood the elevation from the markers, in order of height.
  4. Each pixel joins the flood that reaches it first.

Mathematical foundation

\[ E = \lvert \nabla (G_\sigma * Y) \rvert, \qquad M = \operatorname{HMIN}_h(E), \qquad h = 10 \]

The formula shows the default parameter values.

where

  • \(E\): The elevation, the gradient magnitude
  • \(G_\sigma * Y\): The luma after a Gaussian blur
  • \(\operatorname{HMIN}_h\): The minima at least h deep

The elevation E is the gradient magnitude of the blurred luma Y, so edges are ridges and flat areas are valleys. Every regional minimum would start its own region, and noise makes very many of them. The h-minima transform keeps only the minima at least h deep: a dip whose floor is less than h below the lowest pass out of it does not count. Each kept minimum, as a connected set of pixels, becomes a marker. The h-minima transform would count every minimum that ties exactly with the lowest elevation, however shallow, so this filter first breaks exact ties by adding at most 1e-6, our choice. Then water rises from all markers at once, in order of elevation. Each pixel joins the flood that reaches it first, and floods from different markers meet along the ridges, which become the boundaries.

Flooding by immersion, as Vincent and Soille describe it, sorts the pixels by elevation and processes them from the lowest. A pixel next to exactly one flooded basin joins it. A pixel where two basins meet stays on the boundary or joins one of them, depending on the implementation. Here scikit-image gives every pixel a region, so there are no boundary lines.

The markers decide how many regions there are. The h-minima transform raises the elevation by h and rebuilds it from below by reconstruction. Valleys shallower than h fill up completely, and only deeper ones keep a floor that becomes a marker. A valley's depth is how far it is below the lowest pass to a deeper valley.

The compact watershed of Neubert and Protzel adds the distance from the marker to the elevation, weighted by the compactness. The regions then become rounder and more equal in size, like superpixels.

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 Regions Regions, Elevation (gradient), Boundaries over the image, Mean color of each region The regions as a label map, drawn over the image and listed in the Features panel. Or the elevation that is flooded, the Sobel gradient, stretched. Or the boundaries in yellow over the image, or each region filled with its mean color.
Smoothing sigma 1 0 to 5 px The sigma of a Gaussian blur of the luma before the Sobel gradient. 0 turns the blur off.
Depth h h 10 0.5 to 200 A valley of the gradient starts a region only when it is at least this deep, in the units of the gradient. A ramp of 1 level per pixel has a gradient of about 1.4.
Compactness compactness 0 0 to 10 Above 0, the compact watershed of Neubert and Protzel, which also weighs the distance from each marker. scikit-image uses 0, the classic watershed.

Implementation

watershed_filter in engine/src/pixelnexus_engine/filters/segmentation.py blurs the luma in pixel levels with scipy.ndimage.gaussian_filter, repeating the edge pixels, and takes skimage.filters.sobel as the elevation. A ramp of 1 level per pixel then has an elevation of about 1.4. The markers are skimage.morphology.h_minima of the elevation, labeled with 8-connectivity. Then skimage.segmentation.watershed floods from them, with 4-connected neighbors.

The h-minima transform counts every minimum that ties exactly with the lowest elevation, however shallow its valley. A gradient can have such exact ties, such as several pixels at 0. So the filter first adds a ramp of at most 1e-6, growing in raster order, which breaks the ties. That choice is ours. The elevation output shows the gradient, stretched so its largest value is white.

The regions output is a label map. The engine sends it with a table of the regions, and the studio draws it over the image. See the science engine for how label maps travel.

Worked example

Depth h Smoothing sigma Regions
2 0 3357
2 1 58
5 0 189
5 1 11
10 0 12
10 1 11
20 0 11
20 1 7
40 0 7
40 1 7

The image of the figure, with six disks on a ground, so 7 regions would be ideal. Its elevation reaches 93 on the disk edges after the default blur.

The blur matters most. Without it, noise makes thousands of shallow valleys at small h, and even h of 20 leaves 11 regions. With the default blur of sigma 1, a depth of 20 gives exactly the 7 regions of the image. The disk edges reach an elevation of about 93, far above the noise, so any depth between 20 and 40 works.

Complexity

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

N is the number of pixels. The flooding keeps a priority queue of pixels.

Artifacts and limitations

Limitations

  • Without a good depth h, noise gives very many small regions.
  • Weak edges between two valleys can be crossed when one of the valleys is too shallow.

Artifacts

  • Small regions along strong edges, where the gradient has its own small dips.

Tests

The elevation must equal scikit-image's Sobel of the blurred luma. The labels must equal scikit-image's watershed from the h-minima markers, as a partition. Two minima that tie at the lowest level, with a shallow pass between them, must give one marker, while a deep basin behind a ridge still gives its own. On the noisy Disks image with h of 20, the filter must find 7 regions. The representation must have its three stages.

References

  • Vincent, L., and Soille, P. Watersheds in digital spaces, an efficient algorithm based on immersion simulations. IEEE Transactions on Pattern Analysis and Machine Intelligence 13(6), 583 to 598, 1991. DOI 10.1109/34.87344.
  • Neubert, P., and Protzel, P. Compact watershed and preemptive SLIC, on improving trade-offs of superpixel segmentation algorithms. International Conference on Pattern Recognition (ICPR), 996 to 1001, 2014. DOI 10.1109/ICPR.2014.181.
  • Soille, P. Morphological Image Analysis, Principles and Applications. Second edition, Springer, 2004. DOI 10.1007/978-3-662-05088-0.
  • scikit-image documentation, skimage.segmentation.watershed and skimage.morphology.h_minima.