1 /**
2  * @file Sobel_Demo.cpp
3  * @brief Sample code using Sobel and/orScharr OpenCV functions to make a simple Edge Detector
4  * @author OpenCV team
5  */
6 
7 #include "opencv2/imgproc/imgproc.hpp"
8 #include "opencv2/imgcodecs.hpp"
9 #include "opencv2/highgui/highgui.hpp"
10 #include <stdlib.h>
11 #include <stdio.h>
12 
13 using namespace cv;
14 
15 /**
16  * @function main
17  */
main(int,char ** argv)18 int main( int, char** argv )
19 {
20 
21   Mat src, src_gray;
22   Mat grad;
23   const char* window_name = "Sobel Demo - Simple Edge Detector";
24   int scale = 1;
25   int delta = 0;
26   int ddepth = CV_16S;
27 
28   /// Load an image
29   src = imread( argv[1] );
30 
31   if( src.empty() )
32     { return -1; }
33 
34   GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
35 
36   /// Convert it to gray
37   cvtColor( src, src_gray, COLOR_RGB2GRAY );
38 
39   /// Create window
40   namedWindow( window_name, WINDOW_AUTOSIZE );
41 
42   /// Generate grad_x and grad_y
43   Mat grad_x, grad_y;
44   Mat abs_grad_x, abs_grad_y;
45 
46   /// Gradient X
47   //Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
48   Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
49   convertScaleAbs( grad_x, abs_grad_x );
50 
51   /// Gradient Y
52   //Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
53   Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
54   convertScaleAbs( grad_y, abs_grad_y );
55 
56   /// Total Gradient (approximate)
57   addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
58 
59   imshow( window_name, grad );
60 
61   waitKey(0);
62 
63   return 0;
64 }
65