clahe.cpp 2.8 KB
Newer Older
yao's avatar
yao committed
1 2
#include <iostream>
#include "opencv2/core/core.hpp"
3
#include "opencv2/core/utility.hpp"
yao's avatar
yao committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/ocl/ocl.hpp"
using namespace cv;
using namespace std;

Ptr<CLAHE> pFilter;
int tilesize;
int cliplimit;
string outfile;

static void TSize_Callback(int pos)
{
    if(pos==0)
    {
        pFilter->setTilesGridSize(Size(1,1));
    }
    pFilter->setTilesGridSize(Size(tilesize,tilesize));
}

static void Clip_Callback(int)
{
    pFilter->setClipLimit(cliplimit);
}

int main(int argc, char** argv)
{
    const char* keys =
32 33 34 35
        "{ i input   |                    | specify input image }"
        "{ c camera  |    0               | specify camera id   }"
        "{ s use_cpu |    false           | use cpu algorithm   }"
        "{ o output  | clahe_output.jpg   | specify output save path}";
yao's avatar
yao committed
36 37 38 39 40 41

    CommandLineParser cmd(argc, argv, keys);
    string infile = cmd.get<string>("i");
    outfile = cmd.get<string>("o");
    int camid = cmd.get<int>("c");
    bool use_cpu = cmd.get<bool>("s");
42
    VideoCapture capture;
yao's avatar
yao committed
43 44 45 46 47 48 49
    bool running = true;

    namedWindow("CLAHE");
    createTrackbar("Tile Size", "CLAHE", &tilesize, 32, (TrackbarCallback)TSize_Callback);
    createTrackbar("Clip Limit", "CLAHE", &cliplimit, 20, (TrackbarCallback)Clip_Callback);
    Mat frame, outframe;
    ocl::oclMat d_outframe;
50

yao's avatar
yao committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    int cur_clip;
    Size cur_tilesize;
    if(use_cpu)
    {
        pFilter = createCLAHE();
    }
    else
    {
        pFilter = ocl::createCLAHE();
    }
    cur_clip = (int)pFilter->getClipLimit();
    cur_tilesize = pFilter->getTilesGridSize();
    setTrackbarPos("Tile Size", "CLAHE", cur_tilesize.width);
    setTrackbarPos("Clip Limit", "CLAHE", cur_clip);
    if(infile != "")
    {
        frame = imread(infile);
        if(frame.empty())
        {
            cout << "error read image: " << infile << endl;
            return -1;
        }
    }
    else
    {
76
        capture.open(camid);
yao's avatar
yao committed
77 78 79 80 81 82
    }
    cout << "\nControls:\n"
         << "\to - save output image\n"
         << "\tESC - exit\n";
    while(running)
    {
83 84
        if(capture.isOpened())
            capture.read(frame);
yao's avatar
yao committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        else
            frame = imread(infile);
        if(frame.empty())
        {
            continue;
        }
        if(use_cpu)
        {
            cvtColor(frame, frame, COLOR_BGR2GRAY);
            pFilter->apply(frame, outframe);
        }
        else
        {
            ocl::oclMat d_frame(frame);
            ocl::cvtColor(d_frame, d_outframe, COLOR_BGR2GRAY);
            pFilter->apply(d_outframe, d_outframe);
            d_outframe.download(outframe);
        }
        imshow("CLAHE", outframe);
104
        char key = (char)waitKey(3);
yao's avatar
yao committed
105 106 107 108 109
        if(key == 'o') imwrite(outfile, outframe);
        else if(key == 27) running = false;
    }
    return 0;
}