
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Perform Distance Transformation on an Image in OpenCV Python
We can perform the distance transform using the method cv2.distanceTransform(). Following is the syntax of this method.
Syntax
cv2.distanceTransform(src, distanceType, maskSize)
This method accepts the following parameters ?
src ? 8-bit, single-channel (binary) source image.
distanceType ? Type of the distance.
maskSize ? Size of the distance transform mask.
Steps
To perform distance transform on the image, we could follow the below steps-
Import the required library. In all the following examples, the required Python library is OpenCV. Make sure you have already installed it.
Read an input image using cv2.imread(). The RGB image read using this method is in BGR format. Optionally assign the read BGR image to img.
Now convert this BGR image to grayscale image as below using cv2.cvtColor() function. Optionally assign the converted grayscale image to gray.
Now apply thresholding on the grayscale image to convert it to a binary image. Adjust the second parameter (threshValue) for better binary image.
Apply distance transform on the binary image using cv2.distanceTransform(). It returns a distance transformed image. Normalize this image for range [0,1].
Display the distance transformed image.
Let's have a look at some examples for more clear understanding.
Input Image
We will use this image as the input file in the examples below.
Example
In this example, we find the distance transform of the input image. We apply cv2.DIST_L2 as distanceType and the maskSize of 3.
# Load image in grayscale import cv2 import numpy as np import matplotlib.pyplot as plt # Read the input image img = cv2.imread('sketch.jpg') # convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # apply thresholding to convert the grayscale image to a binary image ret,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY) # Apply distance transform on the binary image dist = cv2.distanceTransform(thresh, cv2.DIST_L2, 3) # Normalize the distance image for range = {0.0, 1.0} # so we can visualize and threshold it cv2.normalize(dist, dist, 0, 1.0, cv2.NORM_MINMAX) cv2.imshow('Distance Transform Image', dist) cv2.waitKey(0) cv2.destroyAllWindows()
Output
When you run the above Python program, it will produce the following output window ?
Example
In this example, we find the distance transform of the input image. We apply five different types of distanceType and the maskSize of 3.
# import required libraries import cv2 import matplotlib.pyplot as plt # Read the input image img = cv2.imread('sketch.jpg') # convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply thresholding to convert the grayscale image to a binary image ret,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY) # Apply distance transform on the binary image dist_C = cv2.distanceTransform(thresh, cv2.DIST_C, 3) dist_L1 = cv2.distanceTransform(thresh, cv2.DIST_L1, 3) dist_L2 = cv2.distanceTransform(thresh, cv2.DIST_L2, 3) dist_LP = cv2.distanceTransform(thresh, cv2.DIST_LABEL_PIXEL, 3) dist_M = cv2.distanceTransform(thresh, cv2.DIST_MASK_3, 3) # Normalize the distance image for range = {0.0, 1.0} # so we can visualize and threshold it cv2.normalize(dist_C, dist_C, 0, 1.0, cv2.NORM_MINMAX) cv2.normalize(dist_L1, dist_L1, 0, 1.0, cv2.NORM_MINMAX) cv2.normalize(dist_L2, dist_L2, 0, 1.0, cv2.NORM_MINMAX) cv2.normalize(dist_LP, dist_LP, 0, 1.0, cv2.NORM_MINMAX) cv2.normalize(dist_M, dist_M, 0, 1.0, cv2.NORM_MINMAX) # visualize the distance images plt.subplot(231),plt.imshow(dist_C, cmap = 'gray') plt.title('DIST_C'), plt.xticks([]), plt.yticks([]) plt.subplot(232),plt.imshow(dist_L1, cmap = 'gray') plt.title('DIST_L1'), plt.xticks([]), plt.yticks([]) plt.subplot(233),plt.imshow(dist_L2, cmap = 'gray') plt.title('DIST_L2'), plt.xticks([]), plt.yticks([]) plt.subplot(234),plt.imshow(dist_LP, cmap = 'gray') plt.title('DIST_LABEL_PIXEL'), plt.xticks([]), plt.yticks([]) plt.subplot(235),plt.imshow(dist_M, cmap = 'gray') plt.title('DIST_MASK_3'), plt.xticks([]), plt.yticks([]) plt.show()
Output
When you run the above Python program, it will produce the following output window showing different transforms obtained after applying different distanceType.
Notice the difference among these five distanceTypes.