Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator.
cv2.Sobel(src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]]) → dst
cv2.CV_*
): output image depth. Recommended value is -1 to use src1.depth()
, and this is used in this app.dx=1
or dx=2
in this app.dy=1
or dy=2
in this app.src
. scale=1
.dst
. Default is 0.cv2.BORDER_*
): Pixel extrapolation method. Default is BORDER_DEFAULT. One of the following:
This function calculates the first, second, third, or mixed image derivatives using an extended Sobel operator. In most cases, the ksize
\(\times\) ksize
separable kernel is used to calculate the derivative. The exception occurs when ksize=1
, and the \(3 \times 1\) or \(1 \times 3\) kernel is used and no Gaussian smoothing is performed.
The function calculates an image derivative by convolving the image with the appropriate kernel: $$\texttt{dst}=\frac{\partial^{\texttt{dx}+\texttt{dy}} \texttt{src}} { {\partial x}^{\texttt{dx}}{\partial y}^{\texttt{dy}}}$$ The Sobel operators combine Gaussian smoothing and differentiation, so the result is more or less resistant to the noise.
ksize=1
is only valid for the first or the second x- or y- derivatives.ksize=3
, the Sobel kernel may produce noticeable inaccuracies. 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.ksize
, when ksize
is larger than 7, it simply pads the kernel with 0's and does not produce different results (Source).borderType
.copyMakeBorder
, otherwise the default value is set to 0.ddepth=-1
, the destination image will have the same depth as the source; in the case of 8-bit unsigned input images it will result in truncated derivatives. That is, the Sobel function will have a negative value after the derivative is obtained, and a value greater than 255. The original image is uint8
, which is an 8-bit unsigned number, so the number of bits created by Sobel is not enough and so there will be truncation (source). It is better to use 64 bit float input images and convert to 8 bit unsigned later. Please see this link for more information. dx=1, dy=0, ksize=3
) or (dx=0, dy=1, ksize=3
) to calculate the first \(x\)- or \(y\)- image derivative. The first case corresponds to a kernel of:
$$\left[\begin{matrix}-1&0&1\\-2&0&2\\-1&0&1\\\end{matrix}\right]$$
The second case corresponds to a kernel of:
$$\left[\begin{matrix}-1&-2&-1\\0&0&0\\1&2&1\\\end{matrix}\right]$$