opencv3.1 距离变换

本文详细介绍了OpenCV中distanceTransform函数的应用,包括两种调用方式及其参数解释,并提供了转换为0-255 uchar类型的显示建议,同时提及了伪彩色和维诺图的显示方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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  //!<

};

用法



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值