# Sobel operator / 索贝尔算子/ Sobel derivatives / Sobel 导数

#

## Formulation

$${G _ x} = {{\begin{bmatrix} -1 & 0 & +1 \\ -2 & 0 & +2 \\ -1 & 0 & +1 \end{bmatrix}} \ast {\text{A}}}$$

and
$${G _ y} = {{\begin{bmatrix} +1 & +2 & +1 \\ 0 & 0 & 0 \\ -1 & -2 & -1 \end{bmatrix}} \ast {\text{A}}}$$

where $$\ast$$ here denotes the 2-dimensional signal processing
convolution operation.

Since the Sobel kernels can be decomposed as the products of an averaging and
a differentiation kernel,
they compute the gradient(梯度) with smoothing.
For example, $$G _ x$$ can be written as:
$${\begin{bmatrix} +1 & +2 & +1 \\ 0 & 0 & 0 \\ -1 & -2 & -1 \end{bmatrix}} = {{\begin{bmatrix}1 \\ 2 \\ 1\end{bmatrix}} {\begin{bmatrix}+1 & 0 & -1\end{bmatrix}}}$$

The x-coordinate is defined here as increasing in the “right”-direction,
and the y-coordinate is defined as increasing in the “down”-direction.
At each point in the image, the resulting gradient approximations can be
combined to give the gradient magnitude(梯度强度), using:
$${G} = {\sqrt{{{G _ x}^{2}} + {{G _ y}^{2}}}}$$

Although sometimes the following simpler equation is used:
$${G} = {{|{G _ x}|} + {|{G _ y}|}}$$

$${\theta} = {\arctan{\left(\frac{{G _ y}}{G _ x}\right)}}$$
where, for example, $$\theta$$ is 0 for a vertical edge
(图像该处拥有纵向边缘) which is lighter on the right side.

Sobel 算子 根据像素点上下、左右邻点灰度加权差,

## Alternative operators

The Sobel–Feldman operator(费尔德曼算子),
while reducing artifacts associated with a pure central differences operator,
does not have perfect rotational symmetry.
Scharr looked into optimizing this property.4
5
Filter kernels up to size 5 x 5 have been presented there,
but the most frequently used one is:
$$\begin{bmatrix}+3&0&-3\\+10&0&-10\\+3&0&-3\end{bmatrix}$$
$$\begin{bmatrix}+3&+10&+3\\0&0&0\\-3&-10&-3\end{bmatrix}$$

### Scharr

• Use the OpenCV function Scharr() to calculate a more accurate derivative
for a kernel of size 3 × 3

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 standar Sobel function.
It implements the following kernels:
$${G _ x} = {\begin{bmatrix}-3&0&+3\\-10&0&+10\\-3&0&+3\end{bmatrix}}$$
$${G _ y} = {\begin{bmatrix}-3&-10&-3\\0&0&0\\+3&+10&+3\end{bmatrix}}$$