Open In App

Distance transformation in image – Python OpenCV

Last Updated : 02 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

 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
  
# Load the image
# you can give the path to the image
image = cv2.imread(r"Mandala.jpg")
  
# Convert the image to grayscale
grayScaleImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
# Threshold the image to create a binary image
_, threshold = cv2.threshold(grayScaleImage, 123, 255, cv2.THRESH_BINARY)
  
# Calculate the distance transform
# distTransform = cv2.distanceTransform(threshold, cv2.DIST_C, 3)
# distTransform= cv2.distanceTransform(threshold, cv2.DIST_L1, 3)
distTransform= cv2.distanceTransform(threshold, cv2.DIST_L2, 3)
  
# Display the distance image
cv2.imshow('Transformed Distance Image1', distTransform)
  
# End the process after pressing any key
cv2.waitKey(0)
cv2.destroyAllWindows()
  
# Save the transformed image
cv2.imwrite('distTransformed.jpg', distTransform)


Output image: 

True

Distance transformed image-Geeksforgeeks

Distance transformed image with OpenCV

Saved Distance transformed image - Geeksforgeeks

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 - Geeksforgeeks

Input Image

 

Python3




# importing necessary libraries
import cv2
import numpy as np
  
# Load the image
# you can specify the path to image
image = cv2.imread(r"Visual_arts.jpg")
  
# Convert the image to grayscale
grayScaleImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
# Threshold the image to create a binary image
_, threshold = cv2.threshold(grayScaleImage, 127, 255, cv2.THRESH_BINARY)
  
# Calculate the distance transform
dist, labels, *other_vars = cv2.distanceTransform(threshold, cv2.DIST_L2, 3)
  
# Find the coordinates of the nearest non-zero pixels for each pixel in the image
coords = np.column_stack(np.where(labels > 0))
  
# Print the coordinates
# squeeze is used to remove the undesired dimension
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]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads