前言:
目标:
- 寻找图像的梯度,边缘等
- 学习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=