Besides edge feature extraction Sobel operator __ image processing

Source: Internet
Author: User

This article mainly describes the function of the following functions:

(1) Cvsobel

(2) Cvconvertscaleabs

(3) Cvcvtcolor


First, let's take a look at how the computer detects edges. Take the gray image as an example, its theoretical basis is this, if there is an edge, then the image Gray will have a certain change, in order to facilitate the assumption that the black gradient to white represents a boundary, then to its gray-scale analysis, the edge of the gray function is a function y=kx, the first derivative is its slope k, This means that the first derivative of the edge is a constant, and because the first derivative of the non-edge is zero, the first derivative can be used to determine the edge of the image. is usually the derivative of the x direction and the y direction, i.e. the gradient. In theory the computer is the way to get the edge of the image.

However, the specific application to the image you will find that the derivative is not, because there is no accurate function to let you take the derivative, and the computer in the solution to solve the solution than to find the numerical solution more trouble, so think of an alternative way to derivation number. is to use a 3x3 window to approximate the derivative of the image. Take the derivation of the x direction as an example, the derivative of a point is the sum of the elements of the third line minus the sum of the first line of elements, thus the approximate derivative of a certain point is obtained. In fact, it is also very good to understand why it is approximate derivative, the derivative represents a change rate, from the first row into the third row, gray value subtraction, of course, is a change rate. This is called the Prewitt operator. So the approximate X-square wizard count is out. The number of Y-square wizards is similar to the X-square method, except that the sum of the first column elements is subtracted from the sum of the third column elements. The x direction and the Y-square Wizard count, then the gradient comes out. So you can find the edge of a picture.

There is also a problem, because of the 3x3 center point of the derivative, so the second column added a weight, its weight is 2, the first column and the third column of the weight of 1, OK, this is the Sobel operator. Compared with the Prewitt operator, Sobel has stronger anti-noise ability. As shown in the figure:


In this way, the center point of the X-square number of wizards to find out. Give me an example.


, the x points are sobel in the form of δx=1x50+2x30+1x50-(1x50+2x30+1x50) = 0. This way you can see that this point is not a boundary. Well, after understanding the basic theory, let's look at the Sobel function under OpenCV.


(1) void Cvsobel (const cvarr* SRC, cvarr* dst, int xorder, intyorder, int aperture_size=3);

SRC: input image;

DST: Output image;

The difference order in the direction of xorder:x;

The difference order in the direction of Yorder:y;

Aperture_size expands the size of the Sobel nucleus (both the window order) and must be 1 (note that this is a 3x1 or 1x3 vector rather than a phalanx), 3, 5, or 7.

Then, because the derivative is Sobel, there will be a negative value and more than 255. And the first built Sobel image is 8 bits: ipl_depth_8u, that is, 8-bit unsigned number, so Sobel set up a number of images are not enough, to 16-bit signed, that is, ipl_depth_16s. Therefore, it is necessary to establish a 16-bit image, the need for this sentence sobel= Cvcreateimage (cvgetsize (frame), ipl_depth_16s,1), this way to run, will not complain, but the Sobel image does not appear, this is what the reason. The original image display is 8-bit unsigned display, now is 16-bit signed, of course, the display will be problematic. So also to convert Sobel to 8-bit unsigned. The OPENCV provides a function that is cvconvertscaleabs.


(2) cvconvertscaleabs (const cvarr* SRC, cvarr* DST, double scale=1, double shift=0);

SRC: source image;

DST: Target images;

Scale: Coefficient of transformation before multiplication;

Shift converts the coefficient of the preceding addition. This makes it possible to create a new unsigned image and then convert it.

Iplimage*sobel8u=cvcreateimage (Cvgetsize (Sobel), ipl_depth_8u,1);

Add Cvconvertscaleabs (sobel,sobel8u,1,0) to the image before displaying it, so you can see the effect of Cvsobel. You can see the effect of the derivation in the x direction or the y direction.


Finally, talk about Cvcvtcolor This function, is the OPENCV color space conversion function, you can achieve RGB color to HSV,HSI and other color space conversion, can also be converted to grayscale images. Parameter cv_rgb2gray is RGB to gray, parameter Cv_gray2rgb is gray to RGB. The processing result is colored.

(3) void Cvcvtcolor (const cvarr* SRC, cvarr* dst, int code);

Src
The input 8-bit,16-bit or 32-bit single-precision floating-point number image.
Dst
Output 8-bit, 16-bit or 32-bit single-precision floating-point image.
Code
Color space conversion mode, the code to achieve different types of color space conversion. Like what:

The Cv_bgr2gray representation is converted to a grayscale graph,

CV_BGR2HSV converts a picture from an RGB space to a HSV space.

When code chooses Cv_bgr2gray, DST needs to be a single channel picture.

When code selects CV_BGR2HSV, for 8-bit graph, the RGB value should be normalized to 0-1.
So the H range in the HSV graph is the range of 0-360,s and V is 0-1.


Now, for the convenience of everyone, I have put the complete program on the table.

Code:

#include <cv.h>
#include using namespace Std;
using namespace CV;
void Main ()
{
Iplimage *frame,*gray,*sobel;
Frame=cvloadimage ("car1.jpg");//Load image
Gray=cvcreateimage (Cvgetsize (frame), frame->depth,1);/note 8-bit
Cvsobel requires the target image to be ipl_depth_16s
Sobel=cvcreateimage (Cvgetsize (frame), ipl_depth_16s,1);/note 16-bit
Cvnamedwindow ("frame");
Cvnamedwindow ("Gray");
Cvnamedwindow ("Sobel");
Cvcvtcolor (Frame,gray,cv_bgr2gray);//Convert to Grayscale
Calculate the first-order X-direction difference, or you can calculate the first-order Y-direction
Cvsobel (gray,sobel,1,0,3);//x-1 Step Guide, y-0 Order
Iplimage *sobel8u=cvcreateimage (Cvgetsize (Sobel), ipl_depth_8u,1);/note is 8-bit
Then turn the format back in to show
Cvconvertscaleabs (sobel,sobel8u,1,0)//convert back to 8-bit to display
Cvshowimage ("Frame", frame);//Display image
Cvshowimage ("Gray", gray);
Cvshowimage ("Sobel", sobel8u);

Cvwaitkey (0);/waiting
Cvreleaseimage (&frame);/free space (important for video processing, no release will cause memory leaks)
Cvreleaseimage (&gray);
Cvreleaseimage (&sobel);
Cvdestroywindow ("frame");
Cvdestroywindow ("Gray");
Cvdestroywindow ("Sobel");
}

Results:



Finish


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.