retina_ocl.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <iostream>
  2. #include <cstring>
  3. #include "opencv2/core.hpp"
  4. #include "opencv2/imgproc.hpp"
  5. #include "opencv2/highgui.hpp"
  6. #include "opencv2/ocl.hpp"
  7. #include "opencv2/bioinspired.hpp"
  8. using namespace cv;
  9. using namespace cv::ocl;
  10. using namespace std;
  11. const int total_loop_count = 50;
  12. static void help(CommandLineParser cmd, const String& errorMessage)
  13. {
  14. cout << errorMessage << endl;
  15. cout << "Avaible options:" << endl;
  16. cmd.printMessage();
  17. }
  18. int main(int argc, char* argv[])
  19. {
  20. //set this to save kernel compile time from second time you run
  21. ocl::setBinaryDiskCache();
  22. const char* keys =
  23. "{ h | help | false | print help message }"
  24. "{ c | cpu | false | use cpu (original version) or gpu(OpenCL) to process the image }"
  25. "{ i | image | cat.jpg | specify the input image }";
  26. CommandLineParser cmd(argc, argv, keys);
  27. if(cmd.get<bool>("help"))
  28. {
  29. help(cmd, "Usage: ./retina_ocl [options]");
  30. return EXIT_FAILURE;
  31. }
  32. String fname = cmd.get<String>("i");
  33. bool useCPU = cmd.get<bool>("c");
  34. cv::Mat input = imread(fname);
  35. if(input.empty())
  36. {
  37. help(cmd, "Error opening: " + fname);
  38. return EXIT_FAILURE;
  39. }
  40. //////////////////////////////////////////////////////////////////////////////
  41. // Program start in a try/catch safety context (Retina may throw errors)
  42. try
  43. {
  44. // create a retina instance with default parameters setup, uncomment the initialisation you wanna test
  45. cv::Ptr<cv::bioinspired::Retina> oclRetina;
  46. cv::Ptr<cv::bioinspired::Retina> retina;
  47. // declare retina output buffers
  48. cv::ocl::oclMat retina_parvo_ocl;
  49. cv::ocl::oclMat retina_magno_ocl;
  50. cv::Mat retina_parvo;
  51. cv::Mat retina_magno;
  52. if(useCPU)
  53. {
  54. retina = cv::bioinspired::createRetina(input.size());
  55. retina->clearBuffers();
  56. }
  57. else
  58. {
  59. oclRetina = cv::bioinspired::createRetina_OCL(input.size());
  60. oclRetina->clearBuffers();
  61. }
  62. int64 temp_time = 0, total_time = 0;
  63. int loop_counter = 0;
  64. for(; loop_counter <= total_loop_count; ++loop_counter)
  65. {
  66. if(useCPU)
  67. {
  68. temp_time = cv::getTickCount();
  69. retina->run(input);
  70. retina->getParvo(retina_parvo);
  71. retina->getMagno(retina_magno);
  72. }
  73. else
  74. {
  75. cv::ocl::oclMat input_ocl(input);
  76. temp_time = cv::getTickCount();
  77. oclRetina->run(input_ocl);
  78. oclRetina->getParvo(retina_parvo_ocl);
  79. oclRetina->getMagno(retina_magno_ocl);
  80. }
  81. // will not count the first loop, which is considered as warm-up period
  82. if(loop_counter > 0)
  83. {
  84. temp_time = (cv::getTickCount() - temp_time);
  85. total_time += temp_time;
  86. printf("Frame id %2d: %3.4fms\n", loop_counter, (double)temp_time / cv::getTickFrequency() * 1000.0);
  87. }
  88. if(!useCPU)
  89. {
  90. retina_parvo = retina_parvo_ocl;
  91. retina_magno = retina_magno_ocl;
  92. }
  93. cv::imshow("retina input", input);
  94. cv::imshow("Retina Parvo", retina_parvo);
  95. cv::imshow("Retina Magno", retina_magno);
  96. cv::waitKey(10);
  97. }
  98. printf("Average: %.4fms\n", (double)total_time / total_loop_count / cv::getTickFrequency() * 1000.0);
  99. }
  100. catch(const cv::Exception& e)
  101. {
  102. std::cerr << "Error using Retina : " << e.what() << std::endl;
  103. }
  104. // Program end message
  105. std::cout << "Retina demo end" << std::endl;
  106. return EXIT_SUCCESS;
  107. }