|
1 | 1 | /**
|
2 | 2 | * @file Sobel_Demo.cpp
|
3 |
| - * @brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector |
| 3 | + * @brief Sample code uses Sobel or Scharr OpenCV functions for edge detection |
4 | 4 | * @author OpenCV team
|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | #include "opencv2/imgproc.hpp"
|
8 | 8 | #include "opencv2/imgcodecs.hpp"
|
9 | 9 | #include "opencv2/highgui.hpp"
|
10 | 10 |
|
| 11 | +#include <iostream> |
| 12 | + |
11 | 13 | using namespace cv;
|
| 14 | +using namespace std; |
12 | 15 |
|
13 | 16 | /**
|
14 | 17 | * @function main
|
15 | 18 | */
|
16 | 19 | int main( int argc, char** argv )
|
17 | 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 | + |
18 | 32 | //![variables]
|
19 |
| - Mat src, src_gray; |
| 33 | + Mat image,src, src_gray; |
20 | 34 | Mat grad;
|
21 |
| - const char* window_name = "Sobel Demo - Simple Edge Detector"; |
22 |
| - int scale = 1; |
23 |
| - int delta = 0; |
| 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"); |
24 | 39 | int ddepth = CV_16S;
|
25 | 40 | //![variables]
|
26 | 41 |
|
27 | 42 | //![load]
|
28 |
| - String imageName("../data/lena.jpg"); // by default |
29 |
| - if (argc > 1) |
| 43 | + String imageName = parser.get<String>("@input"); // by default |
| 44 | + image = imread( imageName, IMREAD_COLOR ); // Load an image |
| 45 | + |
| 46 | + if( image.empty() ) |
30 | 47 | {
|
31 |
| - imageName = argv[1]; |
| 48 | + return 1; |
32 | 49 | }
|
33 |
| - src = imread( imageName, IMREAD_COLOR ); // Load an image |
34 |
| - |
35 |
| - if( src.empty() ) |
36 |
| - { return -1; } |
37 | 50 | //![load]
|
38 | 51 |
|
39 |
| - //![reduce_noise] |
40 |
| - GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT ); |
41 |
| - //![reduce_noise] |
| 52 | + for (;;) |
| 53 | + { |
| 54 | + //![reduce_noise] |
| 55 | + GaussianBlur(image, src, Size(3, 3), 0, 0, BORDER_DEFAULT); |
| 56 | + //![reduce_noise] |
42 | 57 |
|
43 |
| - //![convert_to_gray] |
44 |
| - cvtColor( src, src_gray, COLOR_BGR2GRAY ); |
45 |
| - //![convert_to_gray] |
| 58 | + //![convert_to_gray] |
| 59 | + cvtColor(src, src_gray, COLOR_BGR2GRAY); |
| 60 | + //![convert_to_gray] |
46 | 61 |
|
47 |
| - //![sobel] |
48 |
| - /// Generate grad_x and grad_y |
49 |
| - Mat grad_x, grad_y; |
50 |
| - Mat abs_grad_x, abs_grad_y; |
| 62 | + //![sobel] |
| 63 | + /// Generate grad_x and grad_y |
| 64 | + Mat grad_x, grad_y; |
| 65 | + Mat abs_grad_x, abs_grad_y; |
51 | 66 |
|
52 |
| - /// Gradient X |
53 |
| - //Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT ); |
54 |
| - Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT ); |
| 67 | + /// Gradient X |
| 68 | + Sobel(src_gray, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT); |
55 | 69 |
|
56 |
| - /// Gradient Y |
57 |
| - //Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT ); |
58 |
| - Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT ); |
59 |
| - //![sobel] |
| 70 | + /// Gradient Y |
| 71 | + Sobel(src_gray, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT); |
| 72 | + //![sobel] |
60 | 73 |
|
61 |
| - //![convert] |
62 |
| - convertScaleAbs( grad_x, abs_grad_x ); |
63 |
| - convertScaleAbs( grad_y, abs_grad_y ); |
64 |
| - //![convert] |
| 74 | + //![convert] |
| 75 | + convertScaleAbs(grad_x, abs_grad_x); |
| 76 | + convertScaleAbs(grad_y, abs_grad_y); |
| 77 | + //![convert] |
65 | 78 |
|
66 |
| - //![blend] |
67 |
| - /// Total Gradient (approximate) |
68 |
| - addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad ); |
69 |
| - //![blend] |
| 79 | + //![blend] |
| 80 | + /// Total Gradient (approximate) |
| 81 | + addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad); |
| 82 | + //![blend] |
70 | 83 |
|
71 |
| - //![display] |
72 |
| - imshow( window_name, grad ); |
73 |
| - waitKey(0); |
74 |
| - //![display] |
| 84 | + //![display] |
| 85 | + imshow(window_name, grad); |
| 86 | + char key = (char)waitKey(0); |
| 87 | + //![display] |
75 | 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 | + } |
76 | 116 | return 0;
|
77 | 117 | }
|
0 commit comments