OPENCV Official Document Learning record (13)

Source: Internet
Author: User

Sobel Edge Detection (derivative operation)

Main function Sobel ()

1#include <opencv2/opencv.hpp>2#include <iostream>3#include <string>4 5 using namespacestd;6 using namespaceCV;7 8 voidShowimg (Const string&win_name,ConstMat &img)9 {Ten Namedwindow (win_name,cv_window_autosize); One imshow (win_name,img); A } -  - intMainintargcChar**argv) the { -     if((argc-2)!=0) -         return-1; -Mat Src=imread (argv[1]); +     if(Src.empty ()) -         return-2; +Gaussianblur (Src,src,size (3,3),0,0, Border_default); A  atShowimg ("SRC", SRC); -  - Mat Src_gray; - Cvtcolor (src,src_gray,cv_rgb2gray); -  - Mat grad; in Mat grad_x,grad_y; - Mat abs_grad_x,abs_grad_y; to     intDdepth=cv_16s; +     intDelta=0; -     intscale=1; the  *Sobel (Src_gray,grad_x,ddepth,1,0,3, Scale,delta,border_default); $ Convertscaleabs (grad_x,abs_grad_x);Panax Notoginseng  -Sobel (Src_gray,grad_y,ddepth,0,1,3, Scale,delta,border_default);  the Convertscaleabs (grad_y,abs_grad_y); +  AAddweighted (abs_grad_x,0.5, Abs_grad_y,0.5,0, grad); the  +Showimg ("Reault", grad); -  $ Waitkey (); $     return 0; -}

The Gaussian transform is applied to reduce the image noise.

The Soblel function is used to calculate the horizontal and vertical gradients,

Ddepth is the image depth, where cv_16s is used to prevent overflow (the image here is cv_8u);

1,0 or 0, 1 means the gradient direction is chosen;

3 is the size of the matrix for calculating gradients;

The remaining parameters are all used as default values.

In order to convert the image back to cv_8u, Convertscaleabs (grad_y,abs_grad_y) is required;

To merge two images, using the addweighted () function, if I remember correctly, parameter 0 is the delta value, which is the value attached to the superimposed pixel:

Abs_grad_x,abs_grad_y,grad three images are as follows:

If you do not see the real:

At the same time, we can use the ScHARR function for the third-order kernel operation, but the effect is worse than the strongman:

The code is similar:

1#include <opencv2/opencv.hpp>2#include <iostream>3#include <string>4 5 using namespacestd;6 using namespaceCV;7 8 voidShowimg (Const string&win_name,ConstMat &img)9 {Ten Namedwindow (win_name,cv_window_autosize); One imshow (win_name,img); A } -  - intMainintargcChar**argv) the { -     if((argc-2)!=0) -         return-1; -Mat Src=imread (argv[1]); +     if(Src.empty ()) -         return-2; +Gaussianblur (Src,src,size (3,3),0,0, Border_default); A  atShowimg ("SRC", SRC); -  - Mat Src_gray; - Cvtcolor (src,src_gray,cv_rgb2gray); -  - Mat grad; in Mat grad_x,grad_y; - Mat abs_grad_x,abs_grad_y; to     intDdepth=cv_16s; +     intDelta=0; -     intscale=1; the  *ScHARR (Src_gray,grad_x,ddepth,1,0, Scale,delta,border_default); $ Convertscaleabs (grad_x,abs_grad_x);Panax NotoginsengShowimg ("X", abs_grad_x); -  theScHARR (Src_gray,grad_y,ddepth,0,1, Scale,delta,border_default);  + Convertscaleabs (grad_y,abs_grad_y); AShowimg ("Y", abs_grad_y); the  +Addweighted (abs_grad_x,0.5, Abs_grad_y,0.5,0, grad); -  $Showimg ("Reault", grad); $  - Waitkey (); -     return 0; the}

Above.

Append: The matrix used in the code:

OPENCV Official Document Learning record (13)

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.