OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Sobel Derivatives

Goal

In this tutorial you will learn how to:

Theory

Note
The explanation below belongs to the book Learning OpenCV by Bradski and Kaehler.
  1. In the last two tutorials we have seen applicative examples of convolutions. One of the most important convolutions is the computation of derivatives in an image (or an approximation to them).
  2. Why may be important the calculus of the derivatives in an image? Let's imagine we want to detect the edges present in the image. For instance:

    Sobel_Derivatives_Tutorial_Theory_0.jpg

    You can easily notice that in an edge, the pixel intensity changes in a notorious way. A good way to express changes is by using derivatives. A high change in gradient indicates a major change in the image.

  3. To be more graphical, let's assume we have a 1D-image. An edge is shown by the "jump" in intensity in the plot below:

    Sobel_Derivatives_Tutorial_Theory_Intensity_Function.jpg
  4. The edge "jump" can be seen more easily if we take the first derivative (actually, here appears as a maximum)

    Sobel_Derivatives_Tutorial_Theory_dIntensity_Function.jpg
  5. So, from the explanation above, we can deduce that a method to detect edges in an image can be performed by locating pixel locations where the gradient is higher than its neighbors (or to generalize, higher than a threshold).
  6. More detailed explanation, please refer to Learning OpenCV by Bradski and Kaehler

Sobel Operator

  1. The Sobel Operator is a discrete differentiation operator. It computes an approximation of the gradient of an image intensity function.
  2. The Sobel Operator combines Gaussian smoothing and differentiation.

Formulation

Assuming that the image to be operated is \(I\):

  1. We calculate two derivatives:

    1. Horizontal changes: This is computed by convolving \(I\) with a kernel \(G_{x}\) with odd size. For example for a kernel size of 3, \(G_{x}\) would be computed as:

    \[G_{x} = \begin{bmatrix} -1 & 0 & +1 \\ -2 & 0 & +2 \\ -1 & 0 & +1 \end{bmatrix} * I\]

    1. Vertical changes: This is computed by convolving \(I\) with a kernel \(G_{y}\) with odd size. For example for a kernel size of 3, \(G_{y}\) would be computed as:

    \[G_{y} = \begin{bmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ +1 & +2 & +1 \end{bmatrix} * I\]

  2. At each point of the image we calculate an approximation of the gradient in that point by combining both results above:

    \[G = \sqrt{ G_{x}^{2} + G_{y}^{2} }\]

    Although sometimes the following simpler equation is used:

    \[G = |G_{x}| + |G_{y}|\]

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 cv::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}\]

You can check out more information of this function in the OpenCV reference (cv::Scharr ). Also, in the sample code below, you will notice that above the code for cv::Sobel function there is also code for the cv::Scharr function commented. Uncommenting it (and obviously commenting the Sobel stuff) should give you an idea of how this function works.

Code

  1. What does this program do?
    • Applies the Sobel Operator and generates as output an image with the detected edges bright on a darker background.
  2. The tutorial code's is shown lines below. You can also download it from here
    1 
    8 #include "opencv2/imgcodecs.hpp"
    10 #include <stdlib.h>
    11 #include <stdio.h>
    12 
    13 using namespace cv;
    14 
    18 int main( int, char** argv )
    19 {
    20 
    21  Mat src, src_gray;
    22  Mat grad;
    23  const char* window_name = "Sobel Demo - Simple Edge Detector";
    24  int scale = 1;
    25  int delta = 0;
    26  int ddepth = CV_16S;
    27 
    29  src = imread( argv[1] );
    30 
    31  if( src.empty() )
    32  { return -1; }
    33 
    34  GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
    35 
    37  cvtColor( src, src_gray, COLOR_RGB2GRAY );
    38 
    40  namedWindow( window_name, WINDOW_AUTOSIZE );
    41 
    43  Mat grad_x, grad_y;
    44  Mat abs_grad_x, abs_grad_y;
    45 
    47  //Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
    48  Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
    49  convertScaleAbs( grad_x, abs_grad_x );
    50 
    52  //Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
    53  Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
    54  convertScaleAbs( grad_y, abs_grad_y );
    55 
    57  addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
    58 
    59  imshow( window_name, grad );
    60 
    61  waitKey(0);
    62 
    63  return 0;
    64 }
    bool empty() const
    Returns true if the array has no elements.
    #define CV_16S
    Definition: cvdef.h:104
    void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
    Converts an image from one color space to another.
    Mat imread(const String &filename, int flags=IMREAD_COLOR)
    Loads an image from a file.
    void imshow(const String &winname, InputArray mat)
    Displays an image in the specified window.
    void Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, int ksize=3, double scale=1, double delta=0, int borderType=BORDER_DEFAULT)
    Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator...
    Definition: imgproc.hpp:512
    void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
    Creates a window.
    void addWeighted(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1)
    Calculates the weighted sum of two arrays.
    Size2i Size
    Definition: types.hpp:308
    void convertScaleAbs(InputArray src, OutputArray dst, double alpha=1, double beta=0)
    Scales, calculates absolute values, and converts the result to 8-bit.
    void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT)
    Blurs an image using a Gaussian filter.
    Definition: highgui.hpp:138
    same as BORDER_REFLECT_101
    Definition: base.hpp:242
    int main(int argc, const char *argv[])
    Definition: facerec_demo.cpp:67
    n-dimensional dense array class
    Definition: mat.hpp:726
    int waitKey(int delay=0)
    Waits for a pressed key.

Explanation

  1. First we declare the variables we are going to use:
    Mat src, src_gray;
    Mat grad;
    char* window_name = "Sobel Demo - Simple Edge Detector";
    int scale = 1;
    int delta = 0;
    int ddepth = CV_16S;
  2. As usual we load our source image src:
    src = imread( argv[1] );
    if( !src.data )
    { return -1; }
  3. First, we apply a cv::GaussianBlur to our image to reduce the noise ( kernel size = 3 )
    GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
  4. Now we convert our filtered image to grayscale:
    cvtColor( src, src_gray, COLOR_RGB2GRAY );
  5. Second, we calculate the "*derivatives*" in x and y directions. For this, we use the function cv::Sobel as shown below:

    Mat grad_x, grad_y;
    Mat abs_grad_x, abs_grad_y;
    Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
    Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );

    The function takes the following arguments:

    • src_gray: In our example, the input image. Here it is CV_8U
    • grad_x/*grad_y*: The output image.
    • ddepth: The depth of the output image. We set it to CV_16S to avoid overflow.
    • x_order: The order of the derivative in x direction.
    • y_order: The order of the derivative in y direction.
    • scale, delta and BORDER_DEFAULT: We use default values.

    Notice that to calculate the gradient in x direction we use: \(x_{order}= 1\) and \(y_{order} = 0\). We do analogously for the y direction.

  6. We convert our partial results back to CV_8U:
    convertScaleAbs( grad_x, abs_grad_x );
    convertScaleAbs( grad_y, abs_grad_y );
  7. Finally, we try to approximate the gradient by adding both directional gradients (note that this is not an exact calculation at all! but it is good for our purposes).
    addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
  8. Finally, we show our result:
    imshow( window_name, grad );

Results

  1. Here is the output of applying our basic detector to lena.jpg:

    Sobel_Derivatives_Tutorial_Result.jpg