Skip to content

digitopo.watershed

Topological watershed of grey-level images (2D and 3D).

The functions, parameters and results are those of pink26.watershed.

Think of a grey-level image as a relief: dark points are low, bright points are high. The lower section at level k is the set of the points below k; its connected components are the lakes at level k. The topological watershed lowers the image as much as possible without ever merging two lakes, creating one or making one disappear, at any level. The minima then fill their basins, and what is left between the basins are thin crest lines, at the level of the passes between the minima.

connectivity is the adjacency of the lakes, the basins and the minima: 4 or 8 in 2D, 6, 18 or 26 in 3D. The image has no frame: a minimum that touches the side of the image is a minimum like any other.

References

M. Couprie, G. Bertrand, "Topological gray-scale watershed transformation", Proc. SPIE Vision Geometry VI, vol. 3168, 1997, pp. 136-146.

G. Bertrand, "On topological watersheds", Journal of Mathematical Imaging and Vision 22(2-3), 2005, pp. 217-230.

M. Couprie, L. Najman, G. Bertrand, "Quasi-linear algorithms for the topological watershed", Journal of Mathematical Imaging and Vision 22(2-3), 2005, pp. 231-249.

topological_watershed

topological_watershed(
    image: ArrayLike,
    connectivity: int,
    *,
    convert: bool = False,
) -> NDArray[np.uint8]

Lower a grey-level image to its topological watershed.

Lowers W-destructible points until none is left. The result is a new uint8 array, at most image at every point, with the same number of lakes at every level, the same minima and the same level of passes. On a plateau, the place of a crest line depends on the order in which the points are visited (Pink's order).

Parameters:

Name Type Description Default
image array_like

2D or 3D uint8 array (any other dtype raises TypeError).

required
connectivity int

Connectivity of the minima and basins: 4 or 8 in 2D, 6, 18 or 26 in 3D.

required
convert bool

If True, image is first converted by digitopo.interop.as_uint8 (floats in [0, 1] as in scikit-image, bool, integers from 0 to 255). The result stays uint8. With False, as in pink26, only uint8 is accepted.

False

Returns:

Type Description
ndarray

New uint8 array with the shape of image.

Raises:

Type Description
TypeError

If image is not uint8 (with convert=False), or connectivity is not an integer.

ValueError

If image is not 2D or 3D or has an empty axis, connectivity is not 4 or 8 (2D) or 6, 18 or 26 (3D), or, with convert=True, the values of image are out of range (see digitopo.interop.as_uint8).

DigitopoError

If the image is too large for the core.

Examples:

>>> import numpy as np, digitopo
>>> image = np.array([[0, 50, 100, 200, 100, 50, 0],
...                   [0, 50, 100, 200, 100, 50, 0]], dtype=np.uint8)
>>> digitopo.watershed.topological_watershed(image, 4)
array([[  0,   0,   0, 200,   0,   0,   0],
       [  0,   0,   0, 200,   0,   0,   0]], dtype=uint8)

watershed_lines

watershed_lines(
    image: ArrayLike,
    markers: ArrayLike,
    connectivity: int,
    *,
    convert: bool = False,
) -> NDArray[np.bool_]

Watershed lines of a grey-level image, from markers.

Each marker region becomes the only minimum of its basin; the result is True on the lines and False in the basins. The markers are imposed at level 0, so two markers joined through points at 0 form one region, with no line between them.

Parameters:

Name Type Description Default
image array_like

2D or 3D uint8 array.

required
markers array_like

bool or uint8 array of the shape of image; every nonzero point is a marker. At least one marker is required.

required
connectivity int required
convert bool

If True, image is first converted by digitopo.interop.as_uint8 (floats in [0, 1] as in scikit-image, bool, integers from 0 to 255); the lines are computed on that uint8 image. With False, as in pink26, only uint8 is accepted.

False

Returns:

Type Description
ndarray

New bool array: True on the lines, False in the basins.

Raises:

Type Description
TypeError

If image is not uint8 (with convert=False), markers is not bool or uint8, or connectivity is not an integer.

ValueError

If image is not 2D or 3D or has an empty axis, connectivity is not 4 or 8 (2D) or 6, 18 or 26 (3D), markers does not have the shape of image or has no marker, or, with convert=True, the values of image are out of range.

DigitopoError

If the image is too large for the core.

Examples:

>>> import numpy as np, digitopo
>>> image = np.array([[0, 50, 100, 200, 100, 50, 0],
...                   [0, 50, 100, 200, 100, 50, 0]], dtype=np.uint8)
>>> markers = np.zeros(image.shape, dtype=bool)
>>> markers[0, 0] = markers[0, 6] = True
>>> digitopo.watershed.watershed_lines(image, markers, 4).astype(int)
array([[0, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0]])

watershed_labels

watershed_labels(
    image: ArrayLike, markers: ArrayLike, connectivity: int
) -> NDArray[np.integer[Any]]

Label the basins of the watershed from labelled markers.

The counterpart of skimage.segmentation.watershed(image, markers, connectivity=..., watershed_line=True), with the lines of the topological watershed: every point of a basin gets the label of the markers it contains, and the lines get 0. Unlike scikit-image, the lines are the crest lines of the topological watershed, which keeps the passes between the basins at their level.

Every connected region of markers has its own basin. As in scikit-image, the basins of the same label are then joined: a line point whose labelled neighbours all have one label gets it, unless two labels would touch. The lines left separate different labels only.

Three differences with watershed_lines, which follows pink26:

  • the markers are the only points at level 0: the other points at 0 are raised to 1 first, so that two markers joined through points at 0 stay two basins, separated by a line (in watershed_lines they form one region);
  • the points of a marker that touch a marker of another label (for connectivity) are no longer markers: two touching markers would form one minimum, hence one basin. The line between the two basins then runs through their contact;
  • image may be a float image in [0, 1], as in scikit-image: it is converted by digitopo.interop.as_uint8, like bool and integer images from 0 to 255.

Parameters:

Name Type Description Default
image array_like

2D or 3D grey-level image: uint8, bool, integers from 0 to 255, or floats in [0, 1].

required
markers array_like

Integer array of the shape of image: 0 where there is no marker, a positive label on each marker (for example the result of skimage.measure.label). Several regions may share a label. At least one marker is required.

required
connectivity int

Connectivity of the basins and the markers: 4 or 8 in 2D, 6, 18 or 26 in 3D (digitopo.connectivity converts that of scikit-image).

required

Returns:

Type Description
ndarray

Array of the shape and dtype of markers: 0 on the lines, the label of its markers on every basin and on the lines between basins of that label. Every marker point keeps its label, except the points of contact between two labels.

Raises:

Type Description
TypeError

If markers is not an integer array (a bool mask included: label it first, for example with skimage.measure.label).

DigitopoError

If markers has a negative value or no marker, or if a label has only points of contact with other labels (it would get no basin).

Examples:

>>> import numpy as np, digitopo
>>> image = np.array([[0.0, 0.2, 0.4, 0.8, 0.4, 0.2, 0.0],
...                   [0.0, 0.2, 0.4, 0.8, 0.4, 0.2, 0.0]])
>>> markers = np.zeros(image.shape, dtype=np.int32)
>>> markers[0, 0], markers[0, 6] = 1, 2
>>> digitopo.watershed.watershed_labels(image, markers, 4)
array([[1, 1, 1, 0, 2, 2, 2],
       [1, 1, 1, 0, 2, 2, 2]], dtype=int32)