Cvsobel can be used for edge detection, that is, a simple gradient algorithm (Edge Extraction Algorithm ).
(The following two paragraphs are taken from the Network)
First, let's start with how the computer detects the edge. Taking a grayscale image as an example, its theoretical basis is as follows. If an edge appears, the gray scale of the image will change to a certain extent. To facilitate the assumption that the black gradient is white, it represents a boundary, for its gray scale analysis, the gray scale function at the edge is a Gini Function Y = kx, and the first derivative is its slope k, that is, the first derivative of the edge is a constant, the first derivative of the non-edge is zero, so that the first derivative can be used to preliminarily determine the edge of the image. It is usually the derivative of the X and Y directions, that is, the gradient. In theory, computers obtain the edge of an image in this way.
However, when applied to images, you will find that this derivative cannot be obtained, because there is no accurate function that allows you to evaluate the derivative, in addition, it is much more difficult for a computer to solve the analytical solution than to obtain the numerical solution, so it comes to an alternative method to obtain the derivative. It is to use a 3 × 3 window to approximate the image. Take the X-direction derivation as an example. The derivative of a certain point is the sum of the elements in the third row minus the sum of the elements in the first row. In this way, the approximate derivative of a certain point is obtained. In fact, it is also very easy to understand why it almost represents the derivative, the derivative represents a change rate, from the first line to the third line, the gray value subtraction, of course, is a change rate. This is the so-called Prewitt operator. In this way, the derivative of the X direction is obtained. The Y-direction derivative is similar to the X-direction derivative, except that the sum of the elements in the third column is used to subtract the sum of the elements in the first column. The derivative of the X direction and the Y direction comes out, so the gradient will come out. In this way, you can find the edge in a graph. Another problem is that, because the derivative of the 3x3 center is obtained, a weight is added to the second column. Its weight is 2 and the weight of the first and third columns is 1, okay, this is the Sobel operator. Compared with the Prewitt operator, Sobel has better anti-noise capability. : In this way, the X-direction derivative of the center is obtained.
For example ., X points are obtained by using the Sobel method. Delta X = 1x50 + 2x30 + 1x50-(1x50 + 2x30 + 1x50) = 0. This shows that this point is not a boundary.
Bytes --------------------------------------------------------------------------------------------------
This function is as follows:
Sobel
Use the extended Sobel operator to calculate the first, second, third, or hybrid image difference
void cvSobel( const CvArr* src, CvArr* dst, int xorder, int yorder, int aperture_size=3 );
-
SRC
-
Input image.
-
DST
-
Output image.
-
Xorder
-
Order of difference in the X direction
-
Yorder
-
Differential order in the Y direction
-
Aperture_size
-
The size of the extended Sobel core must be 1, 3, 5, or 7. In addition to the size of 1, in other cases, aperture_size × aperture _ size can be used to calculate the difference. For aperture_size = 1, use the 3x1 or 1x3 kernel (Gaussian smoothing is not performed ). Here is a special variable cv_scharr (=-1), which corresponds to the 3x3 Scharr filter and can give a ratio.
3x3 Sobel filters more accurate results. The filter coefficients of Scharr are:
-
Transpose X-direction or matrix to Y-direction.
The cvsobel function computes the image difference by performing convolution operations on the image using the corresponding kernel:
Because the Sobel operator combines Gaussian smoothing and differentiation, the result is more or less robust to noise. Generally, the function call uses the following parameters (xorder = 1, yorder = 0, aperture_size = 3) or (xorder = 0, yorder = 1, aperture_size = 3) to calculate the first-order X-or Y-direction image difference. The first case corresponds:
Core.
Second correspondence:
Or
The selection of the core depends on the definition of the image origin (origin comes from the definition of the iplimage structure ). Because this function does not perform image scale conversion, elements in the output image (array) usually have larger absolute values than those in the input image (array: that is, the depth of pixels ). To prevent overflow, when the input image is 8 bits, the output image must be 16 bits. Of course, you can use the cvconvertscale or cvconvertscaleabs function to convert the calculation result (DST)
8-bit. In addition to 8-bit images, the function also accepts 32-bit floating-point images. All input and output images must be single-channel and have the same image size or ROI size.
Bytes --------------------------------------------------------------------------------------------------
/* Code */
# Include
Bytes --------------------------------------------------------------------------------------------------
/* Result */
After Sobel
Aperture_size = 7.