In this article, we are going to learn about the distanceTransform() function of the OpenCV library. To use this function, you will need to have the OpenCV library installed in your Python environment.
What is distanceTransform() Function?
The OpenCV distanceTransform() function is an image processing function that calculates the Euclidean distance between each non-zero pixel in an image and the nearest zero pixel. It calculates the distance map for segmentation purposes. This method returns a grayscale picture with each pixel value representing the distance from the nearest non-zero pixel in the supplied image.
Syntax of distanceTransform() Function
Syntax: cv2.distanceTransform(src, distanceType, maskSize[, dst[, labels[, distanceMask]]]) -> dst
Parameter:
This function accepts the following parameters:
- src: The input picture, which should be an 8-bit or 32-bit floating point image with a single channel.
- distanceType: The distance type that was utilised to calculate the distance. One of the following constants could be used: mask cv2.DIST L1, mask cv2.DIST L2, mask cv2.DIST C, or mask cv2.DIST L12.
- maskSize: The distance transform mask’s size. One of the following constants could be used: cv2.DIST MASK 3, cv2.DIST MASK 5, cv2.DIST MASK PRECISE, and cv2.DIST MASK PRECISE.
Example 1
We will use the following image in our code.
You can then import the cv2 module and call the distanceTransform() function, passing it to the appropriate arguments. This function to transform an image after it has been loaded and thresholded to produce a binary image. A neighborhood of 5×5 pixels and the L2 (Euclidean) distance are used to determine the distance transform. Next, OpenCV is used to normalize and display the distance transform image.
Python3
import cv2 import numpy as np # Load the input image and make it grayscale. image = cv2.imread( 'cards.jpeg' ) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Create a binary image by throttling the image. ret, thresh = cv2.threshold(gray, 127 , 255 , cv2.THRESH_BINARY) #Determine the distance transform. dist = cv2.distanceTransform(thresh, cv2.DIST_L2, 5 ) # Make the distance transform normal. dist_output = cv2.normalize(dist, None , 0 , 1.0 , cv2.NORM_MINMAX) # Display the distance transform cv2.imshow( 'Distance Transform' , dist_output) cv2.waitKey( 0 ) |
Output:
Example 2
We will use the following image in our code.
The photos load, are thresholded and are passed to the distance in this case. Utilizing a 3×3 neighborhood and the distance measure DIST LABEL PIXEL, the transform() function. Additionally, OpenCV is used to normalize and display the output.
Python3
import cv2 import numpy as np # Load appropriate image img = cv2.imread( 'randomqr.png' , 0 ) # Threshold the image to convert a binary image ret, thresh = cv2.threshold(img, 127 , 255 , cv2.THRESH_BINARY) # Perform the distance transform dist = cv2.distanceTransform(thresh, cv2.DIST_LABEL_PIXEL, 3 ) # Standardize the distance transform normalized = cv2.normalize(dist, None , 0 , 255 , cv2.NORM_MINMAX, cv2.CV_8U) # Display the distance transform image cv2.imshow( 'Distance Transform' , normalized) cv2.waitKey( 0 ) cv2.destroyAllWindows() |
Output: