OpenCV4系列之图像梯度和边缘检测

在图像处理中,求解图像梯度是常用操作。

Sobel算子

Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator.

Sobel 算子是一种离散性差分算子,用来计算图像像素值的一阶、二阶、三阶或混合梯度。在图像的任何一点使用此算子,将会产生对应的灰度矢量或是其法矢量。

C++: void Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, int ksize=3, double scale=1, double delta=0, int borderType=BORDER_DEFAULT )
C: void cvSobel(const CvArr* src, CvArr* dst, int xorder, int yorder, int aperture_size=3 )

参数含义

src – 输入图像

dst – 输出结果,与输入图像具有相同的尺寸和通道数

ddepth – 输出图像的数据类型。支持以下数据类型组合

  • src.depth() = CV_8U, ddepth = -1/CV_16S/CV_32F/CV_64F
  • src.depth() = CV_16U/CV_16S, ddepth = -1/CV_32F/CV_64F
  • src.depth() = CV_32F, ddepth = -1/CV_32F/CV_64F
  • src.depth() = CV_64F, ddepth = -1/CV_64F

当ddepth=-1时,输出与输入具有相同的数据类型。当输入是8比特图像时,输出结果将是截断的导数值(in the case of 8-bit input images it will result in truncated derivatives)。

xorder – x方向求导阶数

yorder – y方向求导阶数

ksize – 卷积核的大小,只能是1/3/5/7之一(it must be 1, 3, 5, or 7)。

scale – 缩放尺度因子,默认无缩放

delta – 存储之前加到上述结果上的偏移量。

borderType – 边界插值方法,详见附录A-1。

Scharr算子

Calculates the first x- or y- image derivative using Scharr operator.

该算子参数和 Sobel 算子一致,与 Sobel 区别在于,Scharr 仅作用于大小为3的内核。具有和sobel算子一样的速度,但结果更为精确。

C++: void Scharr(InputArray src, OutputArray dst, int ddepth, int dx, int dy, double scale=1, double delta=0, int borderType=BORDER_DEFAULT )

参数含义

src – 输入图像

dst – 输出结果,与输入图像具有相同的尺寸和通道数

ddepth – 输出图像的数据类型,即矩阵中元素的一个通道的数据类型,这个值和 type 是相关的。例如 type 为 CV_16SC2,一个2通道的16位的有符号整数,depth是CV_16S

dx – dx=1 表示求 x 方向的一阶梯度,dx=0 表示不求 x 方向

dy – 与上类似,dy=1 表示求 y 方向的一阶梯度,dy=0 表示不求 y 方向

scale – 求导得到的值的缩放尺度因子,默认无缩放

delta – 在存储之前加到求导值上的数值,可以用于将0以下的值调整到0以上。delta=0 时表示梯度为0处结果保存为0;delta=m 时表示梯度为0处结果保存为m

borderType – 表示图像四周像素外插值方法,默认是 BORDER_DEFAULT,该参数解释见附录A-1。

附录A-1

borderType 决定在图像发生几何变换或者滤波操作(卷积)时边沿像素的处理方式

/*
 Various border types, image boundaries are denoted with '|'

 * BORDER_REPLICATE:     aaaaaa|abcdefgh|hhhhhhh
 * BORDER_REFLECT:       fedcba|abcdefgh|hgfedcb
 * BORDER_REFLECT_101:   gfedcb|abcdefgh|gfedcba
 * BORDER_WRAP:          cdefgh|abcdefgh|abcdefg
 * BORDER_CONSTANT:      iiiiii|abcdefgh|iiiiiii  with some specified 'i'
 */
  • BORDER_CONSTANT 边沿像素用 i 扩展,需要设置borderValue 指定 ' i ' 值,const cv::Scalar& borderValue = cv::Scalar(0);
  • BORDER_REPLICATE,复制边界像素
  • BORDER_REFLECT,对边界对称扩展,包含对称轴处的元素
  • BORDER_REFLECT_101,以边界为对称轴对称扩展复制像素,不包含对称轴处的元素

参考资料

[1] Image Filtering

[2] OpenCV2:Mat属性type,depth,step

[3] Sobel Derivatives

[4] opencv边缘检测sobel算子

[5] python opencv学习(六)图像梯度计算

猜你喜欢

转载自www.cnblogs.com/phillee/p/12374573.html