retinaDemo.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //============================================================================
  2. // Name : retinademo.cpp
  3. // Author : Alexandre Benoit, benoit.alexandre.vision@gmail.com
  4. // Version : 0.1
  5. // Copyright : LISTIC/GIPSA French Labs, july 2011
  6. // Description : Gipsa/LISTIC Labs retina demo in C++, Ansi-style
  7. //============================================================================
  8. #include <iostream>
  9. #include <cstring>
  10. #include "opencv2/bioinspired.hpp"
  11. #include "opencv2/imgcodecs.hpp"
  12. #include "opencv2/videoio.hpp"
  13. #include "opencv2/highgui.hpp"
  14. #include "opencv2/core/ocl.hpp"
  15. const std::string keys =
  16. "{image | | Input from image file }"
  17. "{video | | Input from video file }"
  18. "{camera | 0 | Index of input camera. If image or video is not specified, camera 0 will be used }"
  19. "{log | | Activate retina log sampling }"
  20. "{ocl | | Use OpenCL acceleration if possible }"
  21. "{help | | Print help}";
  22. int main(int argc, char* argv[])
  23. {
  24. // welcome message
  25. std::cout<<"****************************************************"<<std::endl
  26. <<"* Retina demonstration : demonstrates the use of is a wrapper class of the Gipsa/Listic Labs retina model."<<std::endl
  27. <<"* This retina model allows spatio-temporal image processing (applied on still images, video sequences)."<<std::endl
  28. <<"* As a summary, these are the retina model properties:"<<std::endl
  29. <<"* => It applies a spectral whithening (mid-frequency details enhancement)"<<std::endl
  30. <<"* => high frequency spatio-temporal noise reduction"<<std::endl
  31. <<"* => low frequency luminance to be reduced (luminance range compression)"<<std::endl
  32. <<"* => local logarithmic luminance compression allows details to be enhanced in low light conditions\n"<<std::endl
  33. <<"* for more information, reer to the following papers :"<<std::endl
  34. <<"* Benoit A., Caplier A., Durette B., Herault, J., \"USING HUMAN VISUAL SYSTEM MODELING FOR BIO-INSPIRED LOW LEVEL IMAGE PROCESSING\", Elsevier, Computer Vision and Image Understanding 114 (2010), pp. 758-773, DOI: http://dx.doi.org/10.1016/j.cviu.2010.01.011"<<std::endl
  35. <<"* Vision: Images, Signals and Neural Networks: Models of Neural Processing in Visual Perception (Progress in Neural Processing),By: Jeanny Herault, ISBN: 9814273686. WAPI (Tower ID): 113266891."<<std::endl
  36. <<"* => reports comments/remarks at benoit.alexandre.vision@gmail.com"<<std::endl
  37. <<"* => more informations and papers at : http://sites.google.com/site/benoitalexandrevision/"<<std::endl
  38. <<"****************************************************"<<std::endl
  39. <<" NOTE : this program generates the default retina parameters file 'RetinaDefaultParameters.xml'"<<std::endl
  40. <<" => you can use this to fine tune parameters and load them if you save to file 'RetinaSpecificParameters.xml'"<<std::endl;
  41. cv::CommandLineParser parser(argc, argv, keys);
  42. if(!parser.check() || parser.has("help")) {
  43. parser.printMessage();
  44. return 0;
  45. }
  46. bool useLogSampling = parser.has("log"); // check if user wants retina log sampling processing
  47. bool useOCL = parser.has("ocl");
  48. cv::ocl::setUseOpenCL(useOCL);
  49. if(useOCL && !cv::ocl::useOpenCL())
  50. {
  51. std::cout << "Failed to enable OpenCL\n";
  52. }
  53. // declare the retina input buffer... that will be fed differently in regard of the input media
  54. cv::Mat inputFrame;
  55. cv::VideoCapture videoCapture; // in case a video media is used, its manager is declared here
  56. if(parser.has("video"))
  57. videoCapture.open(parser.get<cv::String>("video"));
  58. else if(parser.has("image"))
  59. inputFrame = cv::imread(parser.get<cv::String>("image"));
  60. else
  61. videoCapture.open(parser.get<int>("camera"));
  62. if (videoCapture.isOpened())
  63. {
  64. videoCapture >> inputFrame;
  65. }
  66. if(inputFrame.empty())
  67. {
  68. std::cout << "Failed to open media source\n";
  69. return 0;
  70. }
  71. //////////////////////////////////////////////////////////////////////////////
  72. // Program start in a try/catch safety context (Retina may throw errors)
  73. try
  74. {
  75. // create a retina instance with default parameters setup, uncomment the initialisation you wanna test
  76. cv::Ptr<cv::bioinspired::Retina> myRetina;
  77. // if the last parameter is 'log', then activate log sampling (favour foveal vision and subsamples peripheral vision)
  78. if (useLogSampling)
  79. {
  80. myRetina = cv::bioinspired::Retina::create(inputFrame.size(),
  81. true, cv::bioinspired::RETINA_COLOR_BAYER, true, 2.0, 10.0);
  82. }
  83. else// -> else allocate "classical" retina :
  84. myRetina = cv::bioinspired::Retina::create(inputFrame.size());
  85. // save default retina parameters file in order to let you see this and maybe modify it and reload using method "setup"
  86. myRetina->write("RetinaDefaultParameters.xml");
  87. // load parameters if file exists
  88. myRetina->setup("RetinaSpecificParameters.xml");
  89. myRetina->clearBuffers();
  90. // declare retina output buffers
  91. cv::UMat retinaOutput_parvo;
  92. cv::UMat retinaOutput_magno;
  93. // processing loop with stop condition
  94. int64 totalTime = 0;
  95. int64 totalFrames = 0;
  96. while(true)
  97. {
  98. // if using video stream, then, grabbing a new frame, else, input remains the same
  99. if (videoCapture.isOpened())
  100. videoCapture>>inputFrame;
  101. if(inputFrame.empty())
  102. break;
  103. // run retina filter
  104. int64 frameTime = cv::getTickCount();
  105. myRetina->run(inputFrame);
  106. // Retrieve and display retina output
  107. frameTime = cv::getTickCount() - frameTime;
  108. totalTime += frameTime;
  109. totalFrames++;
  110. myRetina->getParvo(retinaOutput_parvo);
  111. myRetina->getMagno(retinaOutput_magno);
  112. cv::imshow("retina input", inputFrame);
  113. cv::imshow("Retina Parvo", retinaOutput_parvo);
  114. cv::imshow("Retina Magno", retinaOutput_magno);
  115. int key = cv::waitKey(5);
  116. if(key == 'q')
  117. break;
  118. }
  119. std::cout << "\nMean frame processing time: " << (totalTime / cv::getTickFrequency()) / totalFrames << " s" << std::endl;
  120. std::cout << "Retina demo end" << std::endl;
  121. }
  122. catch(const cv::Exception& e)
  123. {
  124. std::cerr<<"Error using Retina : "<<e.what()<<std::endl;
  125. }
  126. return 0;
  127. }