Edge detection Sobel operator and Cvsobel

Source: Internet
Author: User
When edge extraction preserves the region of the image with drastic changes in grayscale, mathematically, the most intuitive method is differential, for the digital image is differential, from the point of view of signal processing, is to use high-pass filter, to retain HF signal. The following program uses Sobel operator, Laplace operator, canny operator to realize the edge detection of the image.
Note: Cvsobel is only used for single-channel image transformation, such as color image cvsobel processing, each channel image can be Cvsobel, and then converted to color image.
===============================================
#include "Cv.h"
#include "cxcore.h"
#include "highgui.h"
void Main ()
{
Iplimage * PIMAGE=NULL;
Iplimage * PIMAGE8UGRAY=NULL;
Iplimage * PIMAGE8USMOOTH=NULL;
Iplimage * PIMAGE16UGRAYSOBEL=NULL;
Iplimage * PIMAGE8UGRAYSOBELSHOW=NULL;
//--------------

Iplimage * Pimageplanes[3]={null,null,null};

Iplimage * PIMAGE16UCOLORSOBEL=NULL; Plimage * PIMAGE8UCOLORSOBELSHOW=NULL;
//--------------
Iplimage * PIMAGE16UGRAYLAPLACE=NULL;
Iplimage * PIMAGE8UGRAYLAPLACESHOW=NULL;
//--------------
Iplimage * PIMAGE8UGRAYCANNY=NULL;
================== grayscale Image Sobel transform =====================
Pimage=cvloadimage ("Lena.jpg",-1);
Pimage8ugray=cvcreateimage (Cvgetsize (pimage), ipl_depth_8u,1);
Pimage8usmooth=cvcreateimage (Cvgetsize (pimage), ipl_depth_8u,1);
Pimage8ugraysobelshow=cvcreateimage (Cvgetsize (pimage), ipl_depth_8u,1);
Turn grayscale
Cvcvtcolor (Pimage,pimage8ugray,cv_bgr2gray);
Gaussian filter
Cvsmooth (pimage8ugray,pimage8usmooth,cv_gaussian,3,0,0);
Cvsobel requires the target image to be ipl_depth_16s
Pimage16ugraysobel=cvcreateimage (Cvgetsize (pimage), ipl_depth_16s,1);
/Calculates the first-order X-direction difference, or calculates the first-order Y-direction
Cvsobel (pimage8usmooth,pimage16ugraysobel,0,1,3);
Then turn the format back to show
Cvconvertscaleabs (pimage16ugraysobel,pimage8ugraysobelshow,1,0);
Cvnamedwindow ("Grayscale image Sobel transform", cv_window_autosize);
Cvshowimage ("Grayscale image Sobel transform", pimage8ugraysobelshow);

================== color Image Sobel Transform =====================
int i;
for (i=0;i<3;i++)
{
Pimageplanes[i]=cvcreateimage (Cvgetsize (pimage), ipl_depth_8u,1);
}
Pimage16ucolorsobel=cvcreateimage (Cvgetsize (pimage), ipl_depth_16s,1);
Pimage8ucolorsobelshow=cvcreateimage (Cvgetsize (pimage), ipl_depth_8u,3);
Divided into 3 single channels
Cvcvtpixtoplane (Pimage,pimageplanes[0],pimageplanes[1],pimageplanes[2],null);
for (i=0;i<3;i++)
{
Cvsobel (pimageplanes[i],pimage16ucolorsobel,0,1,3);
Cvconvertscaleabs (pimage16ucolorsobel,pimageplanes[i],1,0);
}
Cvcvtplanetopix (pimageplanes[0],pimageplanes[1],pimageplanes[2],null,pimage8ucolorsobelshow);
Cvnamedwindow ("Color image Sobel Transform", cv_window_autosize);
Cvshowimage ("Color image Sobel Transform", pimage8ucolorsobelshow);

================== grayscale image Laplace transform =====================
Pimage16ugraylaplace=cvcreateimage (Cvgetsize (pimage), ipl_depth_16s,1);
Pimage8ugraylaplaceshow=cvcreateimage (Cvgetsize (pimage), ipl_depth_8u,1);
Cvlaplace (pimage8usmooth,pimage16ugraylaplace,3);
Cvconvertscaleabs (pimage16ugraylaplace,pimage8ugraylaplaceshow,1,0);
Cvnamedwindow ("Grayscale image Laplace transform", cv_window_autosize);
Cvshowimage ("Grayscale image Laplace transform", pimage8ugraylaplaceshow);

================== grayscale Image Canny transform =====================
Pimage8ugraycanny=cvcreateimage (Cvgetsize (pimage), ipl_depth_8u,1);
Cvcanny (pimage8usmooth,pimage8ugraycanny,100,200,3);
Cvnamedwindow ("Grayscale image canny transform", cv_window_autosize);
Cvshowimage ("Grayscale image canny transform", pimage8ugraycanny);

Cvwaitkey (0);
Cvdestroywindow ("Grayscale image Sobel transform");
Cvdestroywindow ("Color image Sobel transform");
Cvdestroywindow ("Grayscale image Laplace transform");
Cvreleaseimage (&pimage);
Cvreleaseimage (&pimage8ugray);
Cvreleaseimage (&pimage8usmooth);
Cvreleaseimage (&pimage16ugraysobel);
Cvreleaseimage (&pimage8ugraysobelshow);
Cvreleaseimage (&pimage16ucolorsobel);
Cvreleaseimage (&pimage8ucolorsobelshow);
Cvreleaseimage (&pimageplanes[0]);
Cvreleaseimage (&pimageplanes[1]);
Cvreleaseimage (&pimageplanes[2]);
Cvreleaseimage (&pimage16ugraylaplace);
Cvreleaseimage (&pimage8ugraylaplaceshow);
}===============================================

===============================================
Sobel

Computes the first, second, third-order, or mixed-image differential void Cvsobel using the extended Sobel operator (const cvarr* SRC, cvarr* dst, int xorder, int yorder, int aperture_size=3); src input image. DST output image. Xorder the difference order number in the X direction yorder the difference order in the y direction aperture_size the size of the Sobel core, which must be 1, 3, 5, or 7. In addition to the size of 1, in other cases, the aperture_sizexaperture_size separable kernel will be used to calculate the difference. In the case of aperture_size=1, use the 3x1 or 1x3 kernel (without Gaussian smoothing). Here is a special variable Cv_scharr (=-1), corresponding to the 3x3 ScHARR filter, which gives a more accurate result than a 3x3 Sobel filter. The ScHARR filter coefficients are:

To the X-direction as well as transpose the matrix to the Y-direction.

The function Cvsobel calculates the image difference by convolution operations on the image with the corresponding kernel:

Because the Sobel operator combines Gaussian smoothing and differential, the result is more or less robust to noise. Typically, a function call takes 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 to:

Nuclear.

Second correspondence:

Or

The kernel selection relies on the definition of the origin of the image (origin comes from the definition of the iplimage structure). Since the function does not perform image scale transformations, the elements of the output image (array) usually have a greater absolute value (the translator's note: The depth of the pixel) than the input image (array). To prevent overflow, when the input image is 8 bits, the output image is required to be 16 bits. Of course, you can use the function Cvconvertscale or cvconvertscaleabs to convert to 8-bit. In addition to the 8-bit image, the function also accepts 32-bit floating-point image. All input and output images must be single-channel and have the same image size or ROI size. ===============================================

Laplace

Calculates the Laplacian transform of the image void Cvlaplace (const cvarr* SRC, cvarr* dst, int aperture_size=3); src input image. DST output image. Aperture_size kernel size (as defined in Cvsobel).

The function Cvlaplace computes the Laplacian transformation of the input image by first calculating the second-order X-and Y-differential with the Sobel operator, and then summing: DST (x, y) = d2src/dx2 + d2src/dy2

For aperture_size=1, the fastest calculation results, equivalent to the image using the following core to do the convolution:

The

is similar to the Cvsobel function, which also does not make image scale transformations, and the combination of supported input and output image types is consistent with Cvsobel. ===============================================
Canny

void Cvcanny (const cvarr* image, cvarr* edges, Doubl E threshold1,
double threshold2, int aperture_size=3);
image 

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.