retinaDemo.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/highgui.hpp"
  12. static void help(std::string errorMessage)
  13. {
  14. std::cout<<"Program init error : "<<errorMessage<<std::endl;
  15. std::cout<<"\nProgram call procedure : retinaDemo [processing mode] [Optional : media target] [Optional LAST parameter: \"log\" to activate retina log sampling]"<<std::endl;
  16. std::cout<<"\t[processing mode] :"<<std::endl;
  17. std::cout<<"\t -image : for still image processing"<<std::endl;
  18. std::cout<<"\t -video : for video stream processing"<<std::endl;
  19. std::cout<<"\t[Optional : media target] :"<<std::endl;
  20. std::cout<<"\t if processing an image or video file, then, specify the path and filename of the target to process"<<std::endl;
  21. std::cout<<"\t leave empty if processing video stream coming from a connected video device"<<std::endl;
  22. std::cout<<"\t[Optional : activate retina log sampling] : an optional last parameter can be specified for retina spatial log sampling"<<std::endl;
  23. std::cout<<"\t set \"log\" without quotes to activate this sampling, output frame size will be divided by 4"<<std::endl;
  24. std::cout<<"\nExamples:"<<std::endl;
  25. std::cout<<"\t-Image processing : ./retinaDemo -image lena.jpg"<<std::endl;
  26. std::cout<<"\t-Image processing with log sampling : ./retinaDemo -image lena.jpg log"<<std::endl;
  27. std::cout<<"\t-Video processing : ./retinaDemo -video myMovie.mp4"<<std::endl;
  28. std::cout<<"\t-Live video processing : ./retinaDemo -video"<<std::endl;
  29. std::cout<<"\nPlease start again with new parameters"<<std::endl;
  30. }
  31. int main(int argc, char* argv[]) {
  32. // welcome message
  33. std::cout<<"****************************************************"<<std::endl;
  34. std::cout<<"* Retina demonstration : demonstrates the use of is a wrapper class of the Gipsa/Listic Labs retina model."<<std::endl;
  35. std::cout<<"* This retina model allows spatio-temporal image processing (applied on still images, video sequences)."<<std::endl;
  36. std::cout<<"* As a summary, these are the retina model properties:"<<std::endl;
  37. std::cout<<"* => It applies a spectral whithening (mid-frequency details enhancement)"<<std::endl;
  38. std::cout<<"* => high frequency spatio-temporal noise reduction"<<std::endl;
  39. std::cout<<"* => low frequency luminance to be reduced (luminance range compression)"<<std::endl;
  40. std::cout<<"* => local logarithmic luminance compression allows details to be enhanced in low light conditions\n"<<std::endl;
  41. std::cout<<"* for more information, reer to the following papers :"<<std::endl;
  42. std::cout<<"* 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;
  43. std::cout<<"* 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;
  44. std::cout<<"* => reports comments/remarks at benoit.alexandre.vision@gmail.com"<<std::endl;
  45. std::cout<<"* => more informations and papers at : http://sites.google.com/site/benoitalexandrevision/"<<std::endl;
  46. std::cout<<"****************************************************"<<std::endl;
  47. std::cout<<" NOTE : this program generates the default retina parameters file 'RetinaDefaultParameters.xml'"<<std::endl;
  48. std::cout<<" => you can use this to fine tune parameters and load them if you save to file 'RetinaSpecificParameters.xml'"<<std::endl;
  49. // basic input arguments checking
  50. if (argc<2)
  51. {
  52. help("bad number of parameter");
  53. return -1;
  54. }
  55. bool useLogSampling = !strcmp(argv[argc-1], "log"); // check if user wants retina log sampling processing
  56. std::string inputMediaType=argv[1];
  57. // declare the retina input buffer... that will be fed differently in regard of the input media
  58. cv::Mat inputFrame;
  59. cv::VideoCapture videoCapture; // in case a video media is used, its manager is declared here
  60. //////////////////////////////////////////////////////////////////////////////
  61. // checking input media type (still image, video file, live video acquisition)
  62. if (!strcmp(inputMediaType.c_str(), "-image") && argc >= 3)
  63. {
  64. std::cout<<"RetinaDemo: processing image "<<argv[2]<<std::endl;
  65. // image processing case
  66. inputFrame = cv::imread(std::string(argv[2]), 1); // load image in RGB mode
  67. }else
  68. if (!strcmp(inputMediaType.c_str(), "-video"))
  69. {
  70. if (argc == 2 || (argc == 3 && useLogSampling)) // attempt to grab images from a video capture device
  71. {
  72. videoCapture.open(0);
  73. }else// attempt to grab images from a video filestream
  74. {
  75. std::cout<<"RetinaDemo: processing video stream "<<argv[2]<<std::endl;
  76. videoCapture.open(argv[2]);
  77. }
  78. // grab a first frame to check if everything is ok
  79. videoCapture>>inputFrame;
  80. }else
  81. {
  82. // bad command parameter
  83. help("bad command parameter");
  84. return -1;
  85. }
  86. if (inputFrame.empty())
  87. {
  88. help("Input media could not be loaded, aborting");
  89. return -1;
  90. }
  91. //////////////////////////////////////////////////////////////////////////////
  92. // Program start in a try/catch safety context (Retina may throw errors)
  93. try
  94. {
  95. // create a retina instance with default parameters setup, uncomment the initialisation you wanna test
  96. cv::Ptr<cv::bioinspired::Retina> myRetina;
  97. // if the last parameter is 'log', then activate log sampling (favour foveal vision and subsamples peripheral vision)
  98. if (useLogSampling)
  99. {
  100. myRetina = cv::bioinspired::createRetina(inputFrame.size(), true, cv::bioinspired::RETINA_COLOR_BAYER, true, 2.0, 10.0);
  101. }
  102. else// -> else allocate "classical" retina :
  103. myRetina = cv::bioinspired::createRetina(inputFrame.size());
  104. // save default retina parameters file in order to let you see this and maybe modify it and reload using method "setup"
  105. myRetina->write("RetinaDefaultParameters.xml");
  106. // load parameters if file exists
  107. myRetina->setup("RetinaSpecificParameters.xml");
  108. myRetina->clearBuffers();
  109. // declare retina output buffers
  110. cv::Mat retinaOutput_parvo;
  111. cv::Mat retinaOutput_magno;
  112. // processing loop with stop condition
  113. bool continueProcessing=true; // FIXME : not yet managed during process...
  114. while(continueProcessing)
  115. {
  116. // if using video stream, then, grabbing a new frame, else, input remains the same
  117. if (videoCapture.isOpened())
  118. videoCapture>>inputFrame;
  119. // run retina filter
  120. myRetina->run(inputFrame);
  121. // Retrieve and display retina output
  122. myRetina->getParvo(retinaOutput_parvo);
  123. myRetina->getMagno(retinaOutput_magno);
  124. cv::imshow("retina input", inputFrame);
  125. cv::imshow("Retina Parvo", retinaOutput_parvo);
  126. cv::imshow("Retina Magno", retinaOutput_magno);
  127. cv::waitKey(5);
  128. }
  129. }catch(const cv::Exception& e)
  130. {
  131. std::cerr<<"Error using Retina : "<<e.what()<<std::endl;
  132. }
  133. // Program end message
  134. std::cout<<"Retina demo end"<<std::endl;
  135. return 0;
  136. }