美文网首页
OpenCV-Python距离变换函数

OpenCV-Python距离变换函数

作者: 音符纸飞机 | 来源:发表于2018-07-17 16:12 被阅读183次

距离变换函数用于计算每个像素离最近0值像素的距离,输入是一个二值图像。
图像上越亮的点,代表了离零点的距离越远。
应用于图像细化,图像分割等。

dist_img = cv2.distanceTransform(img_bin, distance_type, mask_size)
# distance_type 计算距离的公式
#     参看 cv2.DIST_* ,常用cv2.DIST_L1
# mask_size 
#     参看 cv2.DIST_MASK_*
distance_type
import cv2
from cvutils import utils
from matplotlib import pyplot as plt

img = cv2.imread('laugh.jpg', 0)
img_filtered = cv2.bilateralFilter(img, 21, 75, 75)
img_bin = utils.convert2binary(img_filtered, ksize=91)

dist_img = cv2.distanceTransform(img_bin, cv2.DIST_L1, cv2.DIST_MASK_3)

plt.subplot(1, 2, 1), plt.imshow(dist_img)
plt.title("jet dist"), plt.xticks([]), plt.yticks([])
plt.subplot(1, 2, 2), plt.imshow(cv2.equalizeHist(cv2.convertScaleAbs(dist_img)), cmap='gray')
plt.title("gray_dist"), plt.xticks([]), plt.yticks([])
plt.show()
距离变换结果

相关文章

网友评论

      本文标题:OpenCV-Python距离变换函数

      本文链接:https://www.haomeiwen.com/subject/vocgpftx.html