clahe.cpp 2.9 KB

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