Skip to main content

Module watershed

Module watershed 

Source
Expand description

Watershed segmentation (marker-controlled flooding).

Think of a grayscale image as a landscape in which brightness is altitude. Dropping a marker into each valley and slowly raising the water level makes each marker’s lake grow; where lakes from different markers would merge, a dam (the watershed line) is built. Applied to a gradient magnitude image, valleys are homogeneous regions and ridges are object boundaries, so the flooded basins are the segments.

The implementation is Meyer’s priority-flood algorithm: a priority queue always releases the lowest unprocessed pixel next, and each pixel takes the label of the neighbour that reached it. Ties (equal elevation) are broken first-in-first-out so flooding spreads evenly across plateaus. The result labels every pixel reachable from a marker; use label_boundaries to extract the lines between regions.

See the book entry Segmentation.

Functions§

label_boundaries
Marks pixels that have a right or lower neighbour with a different (nonzero) label: a one-pixel-wide boundary between regions.
watershed
Floods elevation from markers (positive integers label the seeds, 0 means unlabelled) and returns the label of every reachable pixel. Pixels connected to no marker stay 0.
watershed_masked
Like watershed but flooding only where mask is true (pixels outside the mask stay 0).