52093804145ae5efc42dec83063fe96807efccdd
[platform/upstream/opencv.git] / samples / cpp / tutorial_code / ImgTrans / Sobel_Demo.cpp
1 /**
2  * @file Sobel_Demo.cpp
3  * @brief Sample code uses Sobel or Scharr OpenCV functions for edge detection
4  * @author OpenCV team
5  */
6
7 #include "opencv2/imgproc.hpp"
8 #include "opencv2/imgcodecs.hpp"
9 #include "opencv2/highgui.hpp"
10
11 #include <iostream>
12
13 using namespace cv;
14 using namespace std;
15
16 /**
17  * @function main
18  */
19 int main( int argc, char** argv )
20 {
21   cv::CommandLineParser parser(argc, argv,
22                                "{@input   |../data/lena.jpg|input image}"
23                                "{ksize   k|1|ksize (hit 'K' to increase its value)}"
24                                "{scale   s|1|scale (hit 'S' to increase its value)}"
25                                "{delta   d|0|delta (hit 'D' to increase its value)}"
26                                "{help    h|false|show help message}");
27
28   cout << "The sample uses Sobel or Scharr OpenCV functions for edge detection\n\n";
29   parser.printMessage();
30   cout << "\nPress 'ESC' to exit program.\nPress 'R' to reset values ( ksize will be -1 equal to Scharr function )";
31
32   //![variables]
33   Mat image,src, src_gray;
34   Mat grad;
35   const String window_name = "Sobel Demo - Simple Edge Detector";
36   int ksize = parser.get<int>("ksize");
37   int scale = parser.get<int>("scale");
38   int delta = parser.get<int>("delta");
39   int ddepth = CV_16S;
40   //![variables]
41
42   //![load]
43   String imageName = parser.get<String>("@input"); // by default
44   image = imread( imageName, IMREAD_COLOR ); // Load an image
45
46   if( image.empty() )
47   {
48     return 1;
49   }
50   //![load]
51
52   for (;;)
53   {
54     //![reduce_noise]
55     GaussianBlur(image, src, Size(3, 3), 0, 0, BORDER_DEFAULT);
56     //![reduce_noise]
57
58     //![convert_to_gray]
59     cvtColor(src, src_gray, COLOR_BGR2GRAY);
60     //![convert_to_gray]
61
62     //![sobel]
63     /// Generate grad_x and grad_y
64     Mat grad_x, grad_y;
65     Mat abs_grad_x, abs_grad_y;
66
67     /// Gradient X
68     Sobel(src_gray, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT);
69
70     /// Gradient Y
71     Sobel(src_gray, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);
72     //![sobel]
73
74     //![convert]
75     convertScaleAbs(grad_x, abs_grad_x);
76     convertScaleAbs(grad_y, abs_grad_y);
77     //![convert]
78
79     //![blend]
80     /// Total Gradient (approximate)
81     addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
82     //![blend]
83
84     //![display]
85     imshow(window_name, grad);
86     char key = (char)waitKey(0);
87     //![display]
88
89     if(key == 27)
90     {
91       return 0;
92     }
93
94     if (key == 'k' || key == 'K')
95     {
96       ksize = ksize < 30 ? ksize+2 : -1;
97     }
98
99     if (key == 's' || key == 'S')
100     {
101       scale++;
102     }
103
104     if (key == 'd' || key == 'D')
105     {
106       delta++;
107     }
108
109     if (key == 'r' || key == 'R')
110     {
111       scale =  1;
112       ksize = -1;
113       delta =  0;
114     }
115   }
116   return 0;
117 }