对应头文件:
#include
函数功能:
计算源图像的每个像素到最近的零像素的距离。函数 cv::distanceTransform 计算从每个二进制图像像素到最近的零像素的近似或精确距离。 对于零图像像素,距离显然为零。
c++格式
void cv::distanceTransform ( InputArray src,
OutputArray dst,
OutputArray labels,
int distanceType,
int maskSize,
int labelType = DIST_LABEL_CCOMP
)
python格式:
cv.distanceTransform( src, distanceType, maskSize[, dst[, dstType]] ) -> dst
cv.distanceTransformWithLabels( src, distanceType, maskSize[, dst[, labels[, labelType]]] ) -> dst, labels
参数定义:
src 8-bit, single-channel (binary) source image.
----8位单通道二值图像(0-255)
dst Output image with calculated distances. It is a 8-bit or 32-bit floating-point, single-channel image of the same size as src.
----计算距离后的输出图像,8位或32位浮点型的单通道图像
labels Output 2D array of labels (the discrete Voronoi diagram). It has the type CV_32SC1 and the same size as src.
----标签的二维输出数组
distanceType Type of distance, see DistanceTypes
maskSize Size of the distance transform mask, see DistanceTransformMasks. DIST_MASK_PRECISE is not supported by this variant. In case of the DIST_L1 or DIST_C distance type, the parameter is forced to 3 because a 3×3 mask gives the same result as 5×5 or any larger aperture.
----距离变换蒙版的大小,请参阅 DistanceTransformMasks。 此变体不支持 DIST_MASK_PRECISE。 在 DIST_L1 或 DIST_C 距离类型的情况下,该参数被强制为 3,因为 3×3 掩膜给出与 5×5 或任何更大孔径相同的结果。
labelType Type of the label array to build, see DistanceTransformLabelTypes.
--标签数组的类型
这是一个重载的成员函数,是为了方便而提供的。 它与上述函数的不同之处仅在于它接受的参数。
c++格式:
void cv::distanceTransform ( InputArray src,
OutputArray dst,
int distanceType,
int maskSize,
int dstType = CV_32F
)
python格式:
cv.distanceTransform( src, distanceType, maskSize[, dst[, dstType]] ) -> dst
cv.distanceTransformWithLabels( src, distanceType, maskSize[, dst[, labels[, labelType]]] ) -> dst, labels
参数:
src 8-bit, single-channel (binary) source image.
dst Output image with calculated distances. It is a 8-bit or 32-bit floating-point, single-channel image of the same size as src .
distanceType Type of distance, see DistanceTypes
maskSize Size of the distance transform mask, see DistanceTransformMasks. In case of the DIST_L1 or DIST_C distance type, the parameter is forced to 3 because a 3×3 mask gives the same result as 5×5 or any larger aperture.
----距离变换蒙版的大小,请参阅 DistanceTransformMasks。 在 DIST_L1 或 DIST_C 距离类型的情况下,该参数被强制为 3,因为 3×3 掩膜给出与 5×5 或任何更大孔径相同的结果。
dstType Type of output image. It can be CV_8U or CV_32F. Type CV_8U can be used only for the first variant of the function and distanceType == DIST_L1.
----输出图像的类型。 它可以是 CV_8U 或 CV_32F。 类型 CV_8U 只能用于函数的第一个变体和 distanceType == DIST_L1。
距离变换和 M 估计器的距离类型
DIST_USER Python: cv.DIST_USER |
User defined distance. 自定义类型 |
DIST_L1 Python: cv.DIST_L1 |
distance = |x1-x2| + |y1-y2| 坐标绝对值 |
DIST_L2 Python: cv.DIST_L2 |
the simple euclidean distance 简单欧拉距离,应该是坐标差的平方求和再开方 |
DIST_C Python: cv.DIST_C |
distance = max(|x1-x2|,|y1-y2|) |
DIST_L12 Python: cv.DIST_L12 |
L1-L2 metric: distance = 2(sqrt(1+x*x/2) - 1)) 强调x方向距离 |
DIST_FAIR Python: cv.DIST_FAIR |
distance = c^2(|x|/c-log(1+|x|/c)), c = 1.3998 |
DIST_WELSCH Python: cv.DIST_WELSCH |
distance = c^2/2(1-exp(-(x/c)^2)), c = 2.9846 |
DIST_HUBER Python: cv.DIST_HUBER |
distance = |x| |
DIST_MASK_3 Python: cv.DIST_MASK_3 |
mask=3 3*3掩膜 |
DIST_MASK_5 Python: cv.DIST_MASK_5 |
mask=5 5*5掩膜 |
DIST_MASK_PRECISE Python: cv.DIST_MASK_PRECISE |
@meng