Distance transformation in image – Python OpenCV
Last Updated :
26 Apr, 2025
In this article, we will see how we can perform the distance transformation on a given image in OpenCV Python.
Distance Transformation
Distance transformation is a technique used to calculate the distance of each pixel in an image from the nearest non-zero pixel. It is often used in image segmentation and object recognition tasks, as it can help identify the boundaries of objects in an image.
The distance transform function in OpenCV, cv2.distanceTransform(), takes in a binary image and returns two arrays: the distance image and the label image. The distance image contains the distance values of each pixel from the nearest non-zero pixel, and the label image contains the labels of the nearest non-zero pixels.
The distance transform function also takes in two optional arguments: the distance type and the mask size. The distance type can be specified using constants such as cv2.DIST_L1 (for the Manhattan distance) or cv2.DIST_L2 (for the Euclidean distance). The mask size determines the size of the mask used to calculate the distances, with larger values resulting in more accurate but slower calculations.
In addition to the distance transform function, OpenCV also provides the cv2.normalize() function, which can be used to normalize the distance image so that the distance values are in the range of 0 to 255. This can be useful for visualizing the distance image.
Distance transformation is an image processing technique that allows us to obtain the distance of each pixel in an image from the nearest non-zero pixel. It is often used in image segmentation and object recognition tasks.
In OpenCV, we can perform distance transformation using the cv2.distanceTransform() function.
Steps to be followed:
Step 1: Load the image using the cv2.imread() function.
Step 2: Convert the image to grayscale using the cv2.cvtColor() function. This is because the distance transform function works only on single-channel images.
Step 3: Threshold the image to create a binary image, where the pixels with an intensity value greater than a certain threshold are set to 255 and the rest are set to 0. This can be done using the cv2.threshold() function.
Step 4: Use the cv2.distanceTransform() function to calculate the distance of each pixel from the nearest non-zero pixel. This function takes in three arguments: the binary image, the distance type (such as cv2.DIST_L2 for Euclidean distance), and the mask size (such as 3 for a 3×3 mask).
Step 5: The distance transform function returns two arrays: the distance image and the label image. The distance image contains the distance values of each pixel from the nearest non-zero pixel, and the label image contains the labels of the nearest non-zero pixels.
Here’s an example code for distance transformation in OpenCV:
Example 1:
Input Image:

Input Image Preview
Python3
import cv2
image = cv2.imread(r "Mandala.jpg" )
grayScaleImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(grayScaleImage, 123 , 255 , cv2.THRESH_BINARY)
distTransform = cv2.distanceTransform(threshold, cv2.DIST_L2, 3 )
cv2.imshow( 'Transformed Distance Image1' , distTransform)
cv2.waitKey( 0 )
cv2.destroyAllWindows()
cv2.imwrite( 'distTransformed.jpg' , distTransform)
|
Output image:
True

Distance transformed image with OpenCV

Saved Distance transformed image with OpenCV
The output of this code will be a window with the distance image, where each pixel contains the distance value from the nearest non-zero pixel. The distance values will be in the range of 0 to 255, with higher values indicating greater distance.
Example 2:
Here is the second example code for distance transformation in OpenCV, which demonstrates how to use the labels image to obtain the coordinates of the nearest non-zero pixels for each pixel in the image:
Input Image:

Input Image
Python3
import cv2
import numpy as np
image = cv2.imread(r "Visual_arts.jpg" )
grayScaleImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
_, threshold = cv2.threshold(grayScaleImage, 127 , 255 , cv2.THRESH_BINARY)
dist, labels, * other_vars = cv2.distanceTransform(threshold, cv2.DIST_L2, 3 )
coords = np.column_stack(np.where(labels > 0 ))
print (coords.squeeze())
|
Output:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
144 151 152 156 157 158 159 160 161 162 163 167 168 169 170 171 172 179
180 200 201 202 203 204 240 241 242 243 244 254 255 256 257 287 290 297]
Similar Reads
Image Transformations using OpenCV in Python
In this tutorial, we are going to learn Image Transformation using the OpenCV module in Python. What is Image Transformation? Image Transformation involves the transformation of image data in order to retrieve information from the image or preprocess the image for further usage. In this tutorial we
5 min read
Python OpenCV - Affine Transformation
OpenCV is the huge open-source library for computer vision, machine learning, and image processing and now it plays a major role in real-time operation which is very important in todayâs systems. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a
3 min read
Reading an image in OpenCV using Python
Prerequisite: Basics of OpenCV In this article, we'll try to open an image by using OpenCV (Open Source Computer Vision) library.  Following types of files are supported in OpenCV library: Windows bitmaps - *.bmp, *.dibJPEG files - *.jpeg, *.jpgPortable Network Graphics - *.png WebP - *.webp Sun ras
6 min read
Negative transformation of an image using Python and OpenCV
Image is also known as a set of pixels. When we store an image in computers or digitally, itâs corresponding pixel values are stored. So, when we read an image to a variable using OpenCV in Python, the variable stores the pixel values of the image. When we try to negatively transform an image, the b
4 min read
Animate image using OpenCV in Python
In this article, we will discuss how to animate an image using python's OpenCV module. Let's suppose we have one image. Using that single image we will animate it in such a way it will appear continuous array of the same image. This is useful in animating the background in certain games. For example
3 min read
Realtime Distance Estimation Using OpenCV - Python
Prerequisite: Introduction to OpenCV In this article, we are going to see how to calculate the distance with a webcam using OpenCV in Python. By using it, one can process images and videos to identify objects, faces, or even the handwriting of a human. This article focuses on detecting objects. We w
5 min read
Image Thresholding in Python OpenCV
Image Thresholding is an intensity transformation function in which the values of pixels below a particular threshold are reduced, and the values above that threshold are boosted. Â This generally results in a bilevel image at the end, where the image is composed of black and white pixels. Thresholdi
3 min read
Image Translation using OpenCV | Python
Translation refers to the rectilinear shift of an object i.e. an image from one location to another. If we know the amount of shift in horizontal and the vertical direction, say (tx, ty) then we can make a transformation matrix e.g. [Tex] \begin{bmatrix} 1 & 0 & tx \\ 0 & 1 & ty \end
2 min read
Perspective Transformation - Python OpenCV
In Perspective Transformation, we can change the perspective of a given image or video for getting better insights into the required information. In Perspective Transformation, we need to provide the points on the image from which want to gather information by changing the perspective. We also need
2 min read
Image Steganography using OpenCV in Python
Image Steganography is the process of hiding secret data in some image. In this post, we will hide one image inside another and convert it into another image and then extract back both the images from the previous image. The idea behind image-based Steganography is very simple. Images are composed o
3 min read