The operator uses two 3×3 kernels which are
convolved with the original image to calculate approximations of the
derivatives – one for horizontal changes, and one for vertical. If we define
A as the source image, and
Gx and
Gy are two images which at each point contain the horizontal and vertical derivative approximations respectively, the computations are as follows: : \begin{aligned} \mathbf{G}_x &= \begin{bmatrix} -1 & 0 & +1 \\ -2 & 0 & +2 \\ -1 & 0 & +1 \end{bmatrix} * \mathbf{A} \\ \mathbf{G}_y &= \begin{bmatrix} -1 & -2 & -1\\ 0 & 0 & 0 \\ +1 & +2 & +1 \end{bmatrix} * \mathbf{A} \end{aligned} where * here denotes the 2-dimensional
signal processing convolution operation. In his text describing the origin of the operator, Sobel shows different signs for these kernels. He defined the operators as neighborhood masks (i.e. correlation kernels), and therefore are mirrored from what described here as convolution kernels. He also assumed the vertical axis increasing upwards instead of downwards as is common in image processing nowadays, and hence the vertical kernel is flipped. 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, \mathbf{G}_x and \mathbf{G}_y can be written as : \begin{aligned} \mathbf{G}_x &= \begin{bmatrix} 1 \\ 2 \\ 1 \end{bmatrix} * \left ( \begin{bmatrix} 1 & 0 & -1 \end{bmatrix} * \mathbf{A} \right ) \\ \mathbf{G}_y &= \begin{bmatrix} +1 \\ 0 \\ -1 \end{bmatrix} * \left ( \begin{bmatrix} 1 & 2 & 1 \end{bmatrix} * \mathbf{A} \right ) \end{aligned} 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
Pythagorean addition: :\mathbf{G} = \sqrt{ {\mathbf{G}_x}^2 + {\mathbf{G}_y}^2 } Using this information, we can also calculate the gradient's direction: :\mathbf{\Theta} = \operatorname{atan2}( \mathbf{G}_y , \mathbf{G}_x ) where, for example, \mathbf{\Theta} is 0 for a vertical edge which is lighter on the right side (for \operatorname{atan2} see
atan2). == More formally ==