摘自https://docs.opencv.org/4.2.0/d2/d2c/tutorial_sobel_derivatives.html
摘自https://docs.opencv.org/4.2.0/d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d
In an edge, the pixel intensity changes in a notorious way. A good way to express changes is by using derivatives.
Sobel Operator
- The Sobel Operator is a discrete differentiation operator. It computes an approximation of the gradient of an image intensity function.
- The Sobel Operator combines Gaussian smoothing and differentiation.
Formulation
Assuming that the image to be operated is I. We calculate two derivatives:
Horizontal changes: This is computed by convolving I with a kernel with odd size. For example, a kernel size of 3:
Vertical changes:
At each point of the image we calculate an approximation of the gradient in that point by combining both results above:
Although sometimes the following simpler equation is used:
G=|Gx|+|Gy|
Note: When the size of the kernel is 3
, the Sobel kernel shown above may produce noticeable inaccuracies (after all, Sobel is only an approximation of the derivative). OpenCV addresses this inaccuracy for kernels of size 3 by using the Scharr() function. This is as fast but more accurate than the standard Sobel function. It implements the following kernels:
dst=cv.Sobel(src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]])
src | input image. |
dst | output image of the same size and the same number of channels as src . |
ddepth | output image depth, see combinations; in the case of 8-bit input images it will result in truncated derivatives. |
dx | order of the derivative x. |
dy | 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. |
delta | optional delta value that is added to the results prior to storing them in dst. |
borderType | pixel extrapolation method, see BorderTypes |
Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator.
The function calculates an image derivative by convolving the image with the appropriate kernel:
The Sobel operators combine Gaussian smoothing and differentiation, so the result is more or less resistant to the noise. Most often, the function is called with ( xorder = 1, yorder = 0, ksize = 3) or ( xorder = 0, yorder = 1, ksize = 3) to calculate the first x- or y- image derivative.
In all cases except 1, the ksize×ksize separable kernel is used to calculate the derivative. When ksize = 1, the 3×1 or 1×3 kernel is used (that is, no Gaussian smoothing is done). ksize = 1 can only be used for the first or the second x- or y- derivatives.
There is also the special value ksize = -1that corresponds to the 3×3 Scharr filter that may give more accurate results than the 3×3 Sobel.