OpenCV Python 如何对给定的图像执行距离变换
我们可以使用cv2.distanceTransform()方法来执行距离变换。以下是该方法的语法。
语法
cv2.distanceTransform(src, distanceType, maskSize)
此方法接受以下参数 –
- src - 8位,单通道(二进制)源图像。
-
distanceType - 距离的类型。
-
maskSize - 距离变换掩模的大小。
步骤
要对图像执行距离变换,可以按照以下步骤进行 –
- 导入所需的库。在以下所有示例中,所需的Python库是 OpenCV 。请确保您已安装它。
-
使用 cv2.imread() 读取输入图像。使用该方法读取的RGB图像以BGR格式存储。可选择将读取的BGR图像分配给img。
-
现在使用 cv2.cvtColor() 函数将BGR图像转换为灰度图像,如下所示。可选择将转换后的灰度图像分配给gray。
-
现在对灰度图像应用阈值处理,将其转换为二进制图像。调整第二个参数(threshValue)以获得更好的二进制图像。
-
使用 cv2.distanceTransform() 对二进制图像进行距离变换。它返回一个距离变换后的图像。将此图像归一化到范围[0,1]。
-
显示距离变换后的图像。
让我们看看一些具体示例以获得更清楚的理解。
输入图像
我们将在下面的示例中使用此图像作为输入文件。
示例
在这个示例中,我们找到输入图像的距离变换。我们将 cv2.DIST_L2 作为distanceType,并使用3作为maskSize。
# 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()
输出
当您运行上述Python程序时,它将产生以下输出窗口:
示例
在此示例中,我们找到输入图像的距离变换。我们应用了五种不同类型的distanceType和maskSize为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()
输出
当您运行上述Python程序时,它将生成以下输出窗口,显示应用不同distanceType后得到的不同变换。
请注意这五种 distanceType 之间的差异。