Skip to content

digitopo.distance

Distance maps of binary images.

The function, its parameters and its errors are those of pink26.distance, and the results are pink26's point by point.

Vocabulary

Think of a binary image as a set of pixels, the object; everything else is the background. The distance map gives each object pixel its distance to the nearest background pixel, and 0 on the background. The image is surrounded by an infinite background, so the pixels on the border of the array are at distance 1 from the outside, like any other pixel next to the background.

Several distances are available, which differ in how a step is counted:

  • "squared_euclidean": the straight-line distance, squared so that the values stay integers (dx**2 + dy**2, plus dz**2 in 3D). 2D or 3D.
  • "chamfer": an integer approximation of the straight-line distance: a step to a side neighbor costs 5 and a step to a diagonal neighbor costs 7, so the values are about 5 times the Euclidean distance. 2D only.
  • "d4": the number of steps between side neighbors (|dx| + |dy|). 2D.
  • "d8": the number of steps when diagonal steps are allowed (max(|dx|, |dy|)). 2D.
  • "d6": the number of steps between neighbors that share a face (|dx| + |dy| + |dz|). 3D.
  • "d18": the number of steps when moving along a face or an edge. 3D.
  • "d26": the number of steps when moving along a face, an edge or a corner (max(|dx|, |dy|, |dz|)). 3D.

The squared Euclidean distance is computed with the algorithm of Meijster et al.; the other distances are shortest paths with the neighbours and weights of Pink's ldist and lchamfrein.

References

A. Meijster, J. B. T. M. Roerdink, W. H. Hesselink, "A general algorithm for computing distance transforms in linear time", in Mathematical Morphology and its Applications to Image and Signal Processing, Springer, 2000, pp. 331-340.

distance_map

distance_map(
    image: ArrayLike, kind: str = "squared_euclidean"
) -> NDArray[np.int32]

Compute the distance of every object pixel to the background.

Parameters:

Name Type Description Default
image array_like

2D array (rows, columns) or 3D array (depth, rows, columns) of bool or integers. Any nonzero value is an object point.

required
kind str

The distance: "squared_euclidean" (2D or 3D), "chamfer", "d4" or "d8" (2D only), "d6", "d18" or "d26" (3D only). See the module documentation.

"squared_euclidean"

Returns:

Type Description
ndarray

New int32 array with the shape of image: 0 on the background, the distance (at least 1) on the object. The outside of the image counts as background. The values are those of pink26.distance.distance_map.

Raises:

Type Description
TypeError

If image has a floating-point (or other non-integer) dtype.

ValueError

If image is not 2D or 3D, has an axis of length zero, kind is not one of the names above, or kind does not match the dimension of image (for example "d6" for a 2D image).

DigitopoError

If a distance does not fit in 32 bits (a squared Euclidean distance of 2^31 or more: images of tens of thousands of pixels per side).

See Also

digitopo.skeleton.ultimate_skeleton

References

A. Meijster, J. B. T. M. Roerdink, W. H. Hesselink, "A general algorithm for computing distance transforms in linear time", 2000.

Examples:

A 3x3 square of object pixels inside a 5x5 image. The pixels of the square touch the background, so their squared distance is 1, except the centre: the nearest background pixel is 2 pixels away, so 2 squared is 4.

>>> import numpy as np, digitopo
>>> image = np.zeros((5, 5), dtype=np.uint8)
>>> image[1:4, 1:4] = 1
>>> digitopo.distance.distance_map(image)
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 4, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]], dtype=int32)
>>> digitopo.distance.distance_map(image, "d4")[2, 2]
np.int32(2)
>>> digitopo.distance.distance_map(image, "chamfer")[2, 2]
np.int32(10)