Home | History | Annotate | Download | only in tapi
      1 #include <iostream>
      2 #include "opencv2/core/core.hpp"
      3 #include "opencv2/core/ocl.hpp"
      4 #include "opencv2/core/utility.hpp"
      5 #include "opencv2/imgproc/imgproc.hpp"
      6 #include "opencv2/imgcodecs.hpp"
      7 #include "opencv2/videoio.hpp"
      8 #include "opencv2/highgui/highgui.hpp"
      9 
     10 using namespace cv;
     11 using namespace std;
     12 
     13 Ptr<CLAHE> pFilter;
     14 int tilesize;
     15 int cliplimit;
     16 
     17 static void TSize_Callback(int pos)
     18 {
     19     if(pos==0)
     20         pFilter->setTilesGridSize(Size(1,1));
     21     else
     22         pFilter->setTilesGridSize(Size(tilesize,tilesize));
     23 }
     24 
     25 static void Clip_Callback(int)
     26 {
     27     pFilter->setClipLimit(cliplimit);
     28 }
     29 
     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     | false              | 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 != "")
     67     {
     68         imread(infile).copyTo(frame);
     69         if(frame.empty())
     70         {
     71             cout << "error read image: " << infile << endl;
     72             return EXIT_FAILURE;
     73         }
     74     }
     75     else
     76         capture.open(camid);
     77 
     78     cout << "\nControls:\n"
     79          << "\to - save output image\n"
     80          << "\tm - switch OpenCL <-> CPU mode"
     81          << "\tESC - exit\n";
     82 
     83     for (;;)
     84     {
     85         if(capture.isOpened())
     86             capture.read(frame);
     87         else
     88             imread(infile).copyTo(frame);
     89         if(frame.empty())
     90             continue;
     91 
     92         cvtColor(frame, frame, COLOR_BGR2GRAY);
     93         pFilter->apply(frame, outframe);
     94 
     95         imshow("CLAHE", outframe);
     96 
     97         char key = (char)waitKey(3);
     98         if(key == 'o')
     99             imwrite(outfile, outframe);
    100         else if(key == 27)
    101             break;
    102         else if(key == 'm')
    103         {
    104             ocl::setUseOpenCL(!cv::ocl::useOpenCL());
    105             cout << "Switched to " << (ocl::useOpenCL() ? "OpenCL enabled" : "CPU") << " mode\n";
    106         }
    107     }
    108     return EXIT_SUCCESS;
    109 }
    110