Image.distance_transform#
- Image.distance_transform(invert: bool = False, norm: str = 'L2', h: int = 1) Any[source]#
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:
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), dtype=np.uint8) >>> pixels[2, 1:3] = 1 >>> img = Image(pixels) >>> img.distance_transform().print(precision=3) 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 340282346638528859811704183484516925440.000 >>> img.distance_transform(norm="L1").print() 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.00 340282346638528859811704183484516925440.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:
P. Corke, Robotics, Vision & Control for Python, Springer, 2023, Section 11.6.4.
Important
Uses OpenCV function
cv2.distanceTransformwhich accepts single-channel, CV_8U images.- Seealso: