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, plusdz**2in 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
¶
Compute the distance of every object pixel to the background.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
array_like
|
2D array |
required |
kind
|
str
|
The distance: |
"squared_euclidean"
|
Returns:
| Type | Description |
|---|---|
ndarray
|
New |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
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)