machinevisiontoolbox.Image.distance_transform
- Image.distance_transform(invert=False, norm='L2', h=1)
Distance transform
- Parameters:
invert (bool, optional) – consider inverted image, defaults to False
norm (str, optional) – distance metric: ‘L1’ or ‘L2’ [default]
h (int, optional) – half width of window, defaults to 1
- Returns:
distance transform of image
- Return type:
Image
Compute the distance transform. For each zero input pixel, compute its distance to the nearest non-zero input pixel.
Example:
>>> from machinevisiontoolbox import Image >>> import numpy as np >>> pixels = np.zeros((5,5)) >>> pixels[2, 1:3] = 1 >>> img = Image(pixels) >>> img.distance_transform().print(precision=3) 2.324 1.910 1.910 2.324 2.739 1.369 0.955 0.955 1.369 2.324 0.955 0.000 0.000 0.955 1.910 1.369 0.955 0.955 1.369 2.324 2.324 1.910 1.910 2.324 2.739 >>> img.distance_transform(norm="L1").print() 3.00 2.00 2.00 3.00 4.00 2.00 1.00 1.00 2.00 3.00 1.00 0.00 0.00 1.00 2.00 2.00 1.00 1.00 2.00 3.00 3.00 2.00 2.00 3.00 4.00
- Note:
The output image is the same size as the input image.
Distance is computed using a sliding window and is an approximation of true distance.
For non-zero input pixels the corresponding output pixels are set to zero.
The signed-distance function is
image.distance_transform() - image.distance_transform(invert=True)
- References:
Robotics, Vision & Control for Python, Section 11.6.4, P. Corke, Springer 2023.
- Seealso: