distanceTransform函数:到边缘点的距离(黑色的(0)都认为是边缘)
两种调用方式:distanceTransform(edge, dist, distType, maskSize);
distanceTransform(edge, dist, labels, distType, maskSize, labelType);
显示时候最好convert成0-255的uchar类型
如
Mat dis;
dist.convertTo(dis , CV_8U, 1, 0);
imshow("dist",dis);
另外,opencv\sources\samples\cpp\distrans.cpp有伪彩色和维诺图的显示方式。
各参数定义
edge:边缘图像
dist 32s的距离变换图
distType 距离类型
maskSize:距离变换的mask,越大越费时间,精度会提高一些。
label :label图,生成的是维诺图,还能表示连通域When labelType==DIST_LABEL_CCOMP, the function scans through the input image and
marks all the zero pixels with distinct labels.
labelType:可取 0 1两个值 DIST_LABEL_CCOMP时,每个黑色连通域的图像以及距离变换离他最小的地方会被标成同一个label each connected component of zeros in src (as well as all the non-zero pixels closest to the
connected component) will be assigned the same label
DIST_LABEL_PIXEL = 1 /** each zero pixel (and all the non-zero pixels closest to it) gets its own label. */ 每个区域得到自己的label
enum DistanceTypes {
DIST_USER = -1, //!< User defined distance //用户自定义距离DIST_L1 = 1, //!< distance = |x1-x2| + |y1-y2| //L1模距离
DIST_L2 = 2, //!< the simple euclidean distance //L2模距离 欧氏距离
DIST_C = 3, //!< distance = max(|x1-x2|,|y1-y2|)
DIST_L12 = 4, //!< L1-L2 metric: distance = 2(sqrt(1+x*x/2) - 1))
DIST_FAIR = 5, //!< distance = c^2(|x|/c-log(1+|x|/c)), c = 1.3998
DIST_WELSCH = 6, //!< distance = c^2/2(1-exp(-(x/c)^2)), c = 2.9846
DIST_HUBER = 7 //!< distance = |x|<c ? x^2/2 : c(|x|-c/2), c=1.345
};
//! Mask size for distance transform
enum DistanceTransformMasks {
DIST_MASK_3 = 3, //!< mask=3
DIST_MASK_5 = 5, //!< mask=5
DIST_MASK_PRECISE = 0 //!<
};
用法