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)