opencv-图像梯度(gradient)

本文介绍了如何使用OpenCV进行图像梯度处理,重点讲解了Sobel和Laplacian导数的用法。通过cv2.Sobel()和cv2.Laplacian()函数,可以计算图像的边缘和梯度。Sobel导数支持不同大小的滤波器,Laplacian导数则对应于图像的二阶偏导数。对于8位图像,处理梯度时需要注意正负值的问题,建议使用更高精度的数据类型来存储结果。
摘要由CSDN通过智能技术生成

前言:
目标:

  • 寻找图像的梯度,边缘等
  • 学习cv2.Sobel(),cv2.Scharr(),cv2.Laplacian()

Opencv有三种梯度滤波器或者说是高通滤波器,它们分别是:Sobel,Scharr,Laplacian。

Sobel Derivatives(导数)

cv2.Sobel(src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]]) → \rightarrow dst

  • src – input image.

  • dst – output image of the same size and the same number of channels as src .

  • ddepth – output image depth; the following combinations of src.depth() and ddepth are supported:
    – 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
    when ddepth=-1, the destination image will have the same depth as the source; in the case of 8-bit input images it will result in truncated derivatives.

  • xorder – order of the derivative x.

  • yorder – order of the derivative y.

  • ksize – size of the extended Sobel kernel; it must be 1, 3, 5, or 7.

  • scale – optional scale factor for the computed derivative values; by default, no scaling is applied (see getDerivKernels() for details).

  • delta – optional delta value that is added to the results prior to storing them in dst.

  • borderType – pixel extrapolation method (see borderInterpolate() for details).

  • ddepth的取值,主要是图像的深度。
    注意一点的是如果我们的像素数据类型为cv2.CV_8U或者np.uint8,这种数据类型是由黑色向白色变换的是正斜率,而白色向黑丝变换的是负斜率。所以在得到的梯度图像时,会有正数和负数之分。
    如果你想保存这两种变化得到的梯度,最好使用更高级的形式,比如cv2.CV_16S,cv2.CV_64F等等,取其绝对值然后将其转换为cv2.CV_8U就可以了。见下面的例子:
    在这里插入图片描述

img = cv2.imread('box.png',0)
'单边灰度变换的边缘检测'
sobelx8u = cv2.Sobel(img,cv2.CV_8U,1,0,ksize=
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值