opencv学习(8):opencv距离转换函数cv::distanceTransform()

对应头文件:

#include <opencv2/imgproc.hpp>

函数功能:

        计算源图像的每个像素到最近的零像素的距离。函数 cv::distanceTransform 计算从每个二进制图像像素到最近的零像素的近似或精确距离。 对于零图像像素,距离显然为零。

distanceTransform() [1/2]

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. 
                --标签数组的类型

distanceTransform() [2/2]

        这是一个重载的成员函数,是为了方便而提供的。 它与上述函数的不同之处仅在于它接受的参数。 

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。

DistanceTypes

        距离变换和 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|<c ? x^2/2 : c(|x|-c/2), c=1.345

DistanceTransformMasks

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

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: opencv中的mat::zeros函数是用来创建一个指定大小和类型的全零矩阵的函数。它的语法如下: cv::Mat cv::Mat::zeros(int rows, int cols, int type) 其中,rows和cols分别表示矩阵的行数和列数,type表示矩阵的类型,可以是CV_8UC1、CV_8UC3、CV_32FC1等等。函数返回一个指定大小和类型的全零矩阵。 ### 回答2: opencv中的mat::zeros函数是一个用于创建一个指定大小和类型的矩阵的函数。该函数的原型如下: Mat cv::Mat::zeros(int rows, int cols, int type) 其中,rows表示矩阵的行数,cols表示矩阵的列数,type表示矩阵的数据类型,常用的数据类型包括CV_8U、CV_8S、CV_16U、CV_16S、CV_32S、CV_32F、CV_64F等。该函数返回一个新创建的矩阵,初始化所有元素为0。 使用Mat::zeros函数可以快速创建一个指定大小和类型的矩阵,并且初始化为0,而不用手动遍历矩阵并设置每个元素的值。这在一些图像处理的操作中非常有用,例如在创建掩膜矩阵时,可以使用该函数创建一个大小和图像一样的全零矩阵,然后在需要的位置设置为1或其他值。 下面是一个示例代码,使用mat::zeros函数创建了一张大小为512x512,类型为CV_8UC1的全零图像: Mat img = Mat::zeros(512, 512, CV_8UC1); 需要注意的是,mat::zeros函数创建的矩阵所有元素的值都是0,而不是默认值。如果需要创建一个矩阵并且初始化为其它默认值,可以使用其重载函数,例如: Mat img = Mat::zeros(512, 512, CV_8UC3); Mat img2 = Mat::zeros(Size(512, 512), CV_8UC3); Mat img3 = Mat::zeros(img.size(), img.type()); 其中,img2使用了Size类型来指定矩阵的大小,img3则使用现有的矩阵来指定矩阵的大小和类型。 ### 回答3: opencv中的mat::zeros函数是用于创建一个具有指定行数、列数和类型的矩阵,并将所有像素的值设置为0的函数。这个函数可以创建单通道或多通道的矩阵,可以是8位、16位、32位或64位浮点型,也可以是8位、16位或32位整数型。该函数返回一个与所创建矩阵相同类型的空矩阵。 该函数有两种格式,第一种格式是: Mat dst = Mat::zeros(int rows, int cols, int type); 参数说明: rows:矩阵的行数; cols:矩阵的列数; type:矩阵的类型,包括如下几种: CV_8U:8位无符号整数型; CV_8S:8位有符号整数型; CV_16U:16位无符号整数型; CV_16S:16位有符号整数型; CV_32S:32位有符号整数型; CV_32F:32位浮点型; CV_64F:64位浮点型。 第二种格式是: void cv::Mat::zeros(InputArray _src, Scalar value=Scalar::all(0)) 参数说明: _src:矩阵,用于接收零矩阵; value:可选的参数,用于设置矩阵像素的值,默认值为Scalar::all(0),即所有像素设置为0。 总之,mat::zeros函数是一种非常方便的创建空矩阵并初始化像素值的方法。它可以用于图像处理、计算机视觉、机器学习等众多领域。在许多opencv应用中,需要先创建一个空矩阵,并将其作为输出参数传递给其他函数。因此,mat::zeros函数可以简化代码,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值