1 #include <iostream>
2 #include "opencv2/core.hpp"
3 #include "opencv2/core/ocl.hpp"
4 #include "opencv2/core/utility.hpp"
5 #include "opencv2/imgproc.hpp"
6 #include "opencv2/imgcodecs.hpp"
7 #include "opencv2/videoio.hpp"
8 #include "opencv2/highgui.hpp"
9
10 using namespace cv;
11 using namespace std;
12
13 Ptr<CLAHE> pFilter;
14 int tilesize;
15 int cliplimit;
16
TSize_Callback(int pos,void *)17 static void TSize_Callback(int pos, void* /*data*/)
18 {
19 if(pos==0)
20 pFilter->setTilesGridSize(Size(1,1));
21 else
22 pFilter->setTilesGridSize(Size(tilesize,tilesize));
23 }
24
Clip_Callback(int,void *)25 static void Clip_Callback(int, void* /*data*/)
26 {
27 pFilter->setClipLimit(cliplimit);
28 }
29
main(int argc,char ** argv)30 int main(int argc, char** argv)
31 {
32 const char* keys =
33 "{ i input | | specify input image }"
34 "{ c camera | 0 | specify camera id }"
35 "{ o output | clahe_output.jpg | specify output save path}"
36 "{ h help | | print help message }";
37
38 cv::CommandLineParser cmd(argc, argv, keys);
39 if (cmd.has("help"))
40 {
41 cout << "Usage : clahe [options]" << endl;
42 cout << "Available options:" << endl;
43 cmd.printMessage();
44 return EXIT_SUCCESS;
45 }
46
47 string infile = cmd.get<string>("i"), outfile = cmd.get<string>("o");
48 int camid = cmd.get<int>("c");
49 VideoCapture capture;
50
51 namedWindow("CLAHE");
52 createTrackbar("Tile Size", "CLAHE", &tilesize, 32, (TrackbarCallback)TSize_Callback);
53 createTrackbar("Clip Limit", "CLAHE", &cliplimit, 20, (TrackbarCallback)Clip_Callback);
54
55 UMat frame, outframe;
56
57 int cur_clip;
58 Size cur_tilesize;
59 pFilter = createCLAHE();
60
61 cur_clip = (int)pFilter->getClipLimit();
62 cur_tilesize = pFilter->getTilesGridSize();
63 setTrackbarPos("Tile Size", "CLAHE", cur_tilesize.width);
64 setTrackbarPos("Clip Limit", "CLAHE", cur_clip);
65
66 if(!infile.empty())
67 {
68 infile = samples::findFile(infile);
69 imread(infile).copyTo(frame);
70 if(frame.empty())
71 {
72 cout << "error read image: " << infile << endl;
73 return EXIT_FAILURE;
74 }
75 }
76 else
77 capture.open(camid);
78
79 cout << "\nControls:\n"
80 << "\to - save output image\n"
81 << "\tm - switch OpenCL <-> CPU mode"
82 << "\tESC - exit\n";
83
84 for (;;)
85 {
86 if(capture.isOpened())
87 capture.read(frame);
88 else
89 imread(infile).copyTo(frame);
90 if(frame.empty())
91 {
92 waitKey();
93 break;
94 }
95
96 cvtColor(frame, frame, COLOR_BGR2GRAY);
97 pFilter->apply(frame, outframe);
98
99 imshow("CLAHE", outframe);
100
101 char key = (char)waitKey(3);
102 if(key == 'o')
103 imwrite(outfile, outframe);
104 else if(key == 27)
105 break;
106 else if(key == 'm')
107 {
108 ocl::setUseOpenCL(!cv::ocl::useOpenCL());
109 cout << "Switched to " << (ocl::useOpenCL() ? "OpenCL enabled" : "CPU") << " mode\n";
110 }
111 }
112 return EXIT_SUCCESS;
113 }
114