clahe.cpp 2.93 KB
Newer Older
wester committed
1
#include <iostream>
a  
Kai Westerkamp committed
2 3 4
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
wester committed
5
#include "opencv2/ocl/ocl.hpp"
wester committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
using namespace cv;
using namespace std;

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

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

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

int main(int argc, char** argv)
{
    const char* keys =
wester committed
29 30 31 32 33
        "{ 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}"
        "{ h | help    | false              | print help message }";
wester committed
34 35

    cv::CommandLineParser cmd(argc, argv, keys);
wester committed
36
    if (cmd.get<bool>("help"))
wester committed
37 38 39
    {
        cout << "Usage : clahe [options]" << endl;
        cout << "Available options:" << endl;
wester committed
40
        cmd.printParams();
wester committed
41 42 43 44 45
        return EXIT_SUCCESS;
    }

    string infile = cmd.get<string>("i"), outfile = cmd.get<string>("o");
    int camid = cmd.get<int>("c");
wester committed
46 47
    bool use_cpu = cmd.get<bool>("s");
    CvCapture* capture = 0;
wester committed
48 49 50 51 52

    namedWindow("CLAHE");
    createTrackbar("Tile Size", "CLAHE", &tilesize, 32, (TrackbarCallback)TSize_Callback);
    createTrackbar("Clip Limit", "CLAHE", &cliplimit, 20, (TrackbarCallback)Clip_Callback);

wester committed
53 54
    Mat frame, outframe;
    ocl::oclMat d_outframe, d_frame;
wester committed
55 56 57

    int cur_clip;
    Size cur_tilesize;
wester committed
58
    pFilter = use_cpu ? createCLAHE() : ocl::createCLAHE();
wester committed
59 60 61 62 63 64 65 66

    cur_clip = (int)pFilter->getClipLimit();
    cur_tilesize = pFilter->getTilesGridSize();
    setTrackbarPos("Tile Size", "CLAHE", cur_tilesize.width);
    setTrackbarPos("Clip Limit", "CLAHE", cur_clip);

    if(infile != "")
    {
wester committed
67
        frame = imread(infile);
wester committed
68 69 70 71 72 73 74
        if(frame.empty())
        {
            cout << "error read image: " << infile << endl;
            return EXIT_FAILURE;
        }
    }
    else
wester committed
75
        capture = cvCaptureFromCAM(camid);
wester committed
76 77 78 79 80 81 82

    cout << "\nControls:\n"
         << "\to - save output image\n"
         << "\tESC - exit\n";

    for (;;)
    {
wester committed
83 84
        if(capture)
            frame = cvQueryFrame(capture);
wester committed
85
        else
wester committed
86
            frame = imread(infile);
wester committed
87 88 89
        if(frame.empty())
            continue;

wester committed
90 91 92 93 94 95 96 97 98 99 100
        if(use_cpu)
        {
            cvtColor(frame, frame, COLOR_BGR2GRAY);
            pFilter->apply(frame, outframe);
        }
        else
        {
            ocl::cvtColor(d_frame = frame, d_outframe, COLOR_BGR2GRAY);
            pFilter->apply(d_outframe, d_outframe);
            d_outframe.download(outframe);
        }
wester committed
101 102 103

        imshow("CLAHE", outframe);

wester committed
104
        char key = (char)cvWaitKey(3);
wester committed
105 106 107 108 109 110 111
        if(key == 'o')
            imwrite(outfile, outframe);
        else if(key == 27)
            break;
    }
    return EXIT_SUCCESS;
}