opencv/samples/ocl/clahe.cpp

112 lines
2.9 KiB
C++
Raw Normal View History

2013-06-28 15:08:39 +08:00
#include <iostream>
#include "opencv2/core/core.hpp"
#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;
static void TSize_Callback(int pos)
{
if(pos==0)
pFilter->setTilesGridSize(Size(1,1));
2013-10-22 00:47:55 +08:00
else
pFilter->setTilesGridSize(Size(tilesize,tilesize));
2013-06-28 15:08:39 +08:00
}
static void Clip_Callback(int)
{
pFilter->setClipLimit(cliplimit);
}
int main(int argc, char** argv)
{
const char* keys =
"{ i | input | | specify input image }"
"{ c | camera | 0 | specify camera id }"
"{ s | use_cpu | false | use cpu algorithm }"
2013-10-22 00:47:55 +08:00
"{ o | output | clahe_output.jpg | specify output save path}"
"{ h | help | false | print help message }";
cv::CommandLineParser cmd(argc, argv, keys);
if (cmd.get<bool>("help"))
{
cout << "Usage : clahe [options]" << endl;
cout << "Available options:" << endl;
cmd.printParams();
return EXIT_SUCCESS;
}
2013-06-28 15:08:39 +08:00
2013-10-22 00:47:55 +08:00
string infile = cmd.get<string>("i"), outfile = cmd.get<string>("o");
2013-06-28 15:08:39 +08:00
int camid = cmd.get<int>("c");
bool use_cpu = cmd.get<bool>("s");
CvCapture* capture = 0;
namedWindow("CLAHE");
createTrackbar("Tile Size", "CLAHE", &tilesize, 32, (TrackbarCallback)TSize_Callback);
createTrackbar("Clip Limit", "CLAHE", &cliplimit, 20, (TrackbarCallback)Clip_Callback);
2013-09-03 15:02:18 +08:00
2013-06-28 15:08:39 +08:00
Mat frame, outframe;
2013-10-22 00:47:55 +08:00
ocl::oclMat d_outframe, d_frame;
2013-08-21 20:44:09 +08:00
2013-06-28 15:08:39 +08:00
int cur_clip;
Size cur_tilesize;
2013-10-22 00:47:55 +08:00
pFilter = use_cpu ? createCLAHE() : ocl::createCLAHE();
2013-06-28 15:08:39 +08:00
cur_clip = (int)pFilter->getClipLimit();
cur_tilesize = pFilter->getTilesGridSize();
setTrackbarPos("Tile Size", "CLAHE", cur_tilesize.width);
setTrackbarPos("Clip Limit", "CLAHE", cur_clip);
2013-10-22 00:47:55 +08:00
2013-06-28 15:08:39 +08:00
if(infile != "")
{
frame = imread(infile);
if(frame.empty())
{
cout << "error read image: " << infile << endl;
2013-10-22 00:47:55 +08:00
return EXIT_FAILURE;
2013-06-28 15:08:39 +08:00
}
}
else
capture = cvCaptureFromCAM(camid);
2013-10-22 00:47:55 +08:00
2013-06-28 15:08:39 +08:00
cout << "\nControls:\n"
<< "\to - save output image\n"
<< "\tESC - exit\n";
2013-10-22 00:47:55 +08:00
for (;;)
2013-06-28 15:08:39 +08:00
{
if(capture)
frame = cvQueryFrame(capture);
else
frame = imread(infile);
if(frame.empty())
continue;
2013-10-22 00:47:55 +08:00
2013-06-28 15:08:39 +08:00
if(use_cpu)
{
cvtColor(frame, frame, COLOR_BGR2GRAY);
pFilter->apply(frame, outframe);
}
else
{
2013-10-22 00:47:55 +08:00
ocl::cvtColor(d_frame = frame, d_outframe, COLOR_BGR2GRAY);
2013-06-28 15:08:39 +08:00
pFilter->apply(d_outframe, d_outframe);
d_outframe.download(outframe);
}
2013-10-22 00:47:55 +08:00
2013-06-28 15:08:39 +08:00
imshow("CLAHE", outframe);
2013-10-22 00:47:55 +08:00
2013-06-28 15:08:39 +08:00
char key = (char)cvWaitKey(3);
2013-10-22 00:47:55 +08:00
if(key == 'o')
imwrite(outfile, outframe);
else if(key == 27)
break;
2013-06-28 15:08:39 +08:00
}
2013-10-22 00:47:55 +08:00
return EXIT_SUCCESS;
2013-06-28 15:08:39 +08:00
}