首页 > 代码库 > OpenCV Tutorials —— Sobel Derivatives

OpenCV Tutorials —— Sobel Derivatives

图像边缘 —— 像素灰度值变换剧烈的点

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

 

  1. 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:

    Intensity Plot for an edge

 

  1. The edge “jump” can be seen more easily if we take the first derivative (actually, here appears as a maximum)

    First derivative of Intensity - Plot for an edge

 

Sobel Operator

  1. 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

    2. 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}|

 

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}

 

 

calculate the “derivatives” in x and y directions.

Mat grad_x, grad_y;Mat abs_grad_x, abs_grad_y;/// Gradient XSobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );/// Gradient YSobel( 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.

 

 

convert our partial results back to CV_8U:

convertScaleAbs( grad_x, abs_grad_x );convertScaleAbs( grad_y, abs_grad_y );

 

 

Code

#include "stdafx.h"#include "opencv2/imgproc/imgproc.hpp"#include "opencv2/highgui/highgui.hpp"#include <stdlib.h>#include <stdio.h>using namespace cv;/** @function main */int main( int argc, char** argv ){	Mat src, src_gray;	Mat grad;	char* window_name = "Sobel Demo - Simple Edge Detector";	int scale = 1;	int delta = 0;	int ddepth = CV_16S;	int c;	/// Load an image	src = http://www.mamicode.com/imread( argv[1] );>

OpenCV Tutorials —— Sobel Derivatives