1 #include "opencv2/core/utility.hpp"
2 #include "opencv2/imgproc.hpp"
3 #include "opencv2/imgcodecs.hpp"
4 #include "opencv2/highgui.hpp"
5
6 #include <stdio.h>
7
8 using namespace cv;
9 using namespace std;
10
11 int edgeThresh = 1;
12 Mat image, gray, edge, cedge;
13
14 // define a trackbar callback
onTrackbar(int,void *)15 static void onTrackbar(int, void*)
16 {
17 blur(gray, edge, Size(3,3));
18
19 // Run the edge detector on grayscale
20 Canny(edge, edge, edgeThresh, edgeThresh*3, 3);
21 cedge = Scalar::all(0);
22
23 image.copyTo(cedge, edge);
24 imshow("Edge map", cedge);
25 }
26
help()27 static void help()
28 {
29 printf("\nThis sample demonstrates Canny edge detection\n"
30 "Call:\n"
31 " /.edge [image_name -- Default is ../data/fruits.jpg]\n\n");
32 }
33
34 const char* keys =
35 {
36 "{@image |../data/fruits.jpg|input image name}"
37 };
38
main(int argc,const char ** argv)39 int main( int argc, const char** argv )
40 {
41 help();
42
43 CommandLineParser parser(argc, argv, keys);
44 string filename = parser.get<string>(0);
45
46 image = imread(filename, 1);
47 if(image.empty())
48 {
49 printf("Cannot read image file: %s\n", filename.c_str());
50 help();
51 return -1;
52 }
53 cedge.create(image.size(), image.type());
54 cvtColor(image, gray, COLOR_BGR2GRAY);
55
56 // Create a window
57 namedWindow("Edge map", 1);
58
59 // create a toolbar
60 createTrackbar("Canny threshold", "Edge map", &edgeThresh, 100, onTrackbar);
61
62 // Show the image
63 onTrackbar(0, 0);
64
65 // Wait for a key stroke; the same function arranges events processing
66 waitKey(0);
67
68 return 0;
69 }
70