3 * @brief Sample code uses Sobel or Scharr OpenCV functions for edge detection
7 #include "opencv2/imgproc.hpp"
8 #include "opencv2/imgcodecs.hpp"
9 #include "opencv2/highgui.hpp"
19 int main( int argc, char** argv )
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}");
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 )";
33 Mat image,src, src_gray;
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");
43 String imageName = parser.get<String>("@input"); // by default
44 image = imread( imageName, IMREAD_COLOR ); // Load an image
55 GaussianBlur(image, src, Size(3, 3), 0, 0, BORDER_DEFAULT);
59 cvtColor(src, src_gray, COLOR_BGR2GRAY);
63 /// Generate grad_x and grad_y
65 Mat abs_grad_x, abs_grad_y;
68 Sobel(src_gray, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT);
71 Sobel(src_gray, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);
75 convertScaleAbs(grad_x, abs_grad_x);
76 convertScaleAbs(grad_y, abs_grad_y);
80 /// Total Gradient (approximate)
81 addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
85 imshow(window_name, grad);
86 char key = (char)waitKey(0);
94 if (key == 'k' || key == 'K')
96 ksize = ksize < 30 ? ksize+2 : -1;
99 if (key == 's' || key == 'S')
104 if (key == 'd' || key == 'D')
109 if (key == 'r' || key == 'R')