multitracker.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*----------------------------------------------
  2. * Usage:
  3. * example_tracking_multitracker <video_name> [algorithm]
  4. *
  5. * example:
  6. * example_tracking_multitracker Bolt/img/%04d.jpg
  7. * example_tracking_multitracker faceocc2.webm KCF
  8. *
  9. * Note: after the OpenCV library is installed,
  10. * please re-compile this code with "HAVE_OPENCV" parameter activated
  11. * to enable the high precission of fps computation
  12. *--------------------------------------------------*/
  13. /* after the OpenCV library is installed
  14. * please uncomment the the line below and re-compile this code
  15. * to enable high precission of fps computation
  16. */
  17. //#define HAVE_OPENCV
  18. #include <opencv2/core/utility.hpp>
  19. #include <opencv2/tracking.hpp>
  20. #include <opencv2/videoio.hpp>
  21. #include <opencv2/highgui.hpp>
  22. #include <iostream>
  23. #include <cstring>
  24. #include <ctime>
  25. #include "samples_utility.hpp"
  26. #ifdef HAVE_OPENCV
  27. #include <opencv2/flann.hpp>
  28. #endif
  29. #define RESET "\033[0m"
  30. #define RED "\033[31m" /* Red */
  31. #define GREEN "\033[32m" /* Green */
  32. using namespace std;
  33. using namespace cv;
  34. int main( int argc, char** argv ){
  35. // show help
  36. if(argc<2){
  37. cout<<
  38. " Usage: example_tracking_multitracker <video_name> [algorithm]\n"
  39. " examples:\n"
  40. " example_tracking_multitracker Bolt/img/%04d.jpg\n"
  41. " example_tracking_multitracker faceocc2.webm MEDIANFLOW\n"
  42. " \n"
  43. " Note: after the OpenCV library is installed,\n"
  44. " please re-compile with the HAVE_OPENCV parameter activated\n"
  45. " to enable the high precission of fps computation.\n"
  46. << endl;
  47. return 0;
  48. }
  49. // timer
  50. #ifdef HAVE_OPENCV
  51. cvflann::StartStopTimer timer;
  52. #else
  53. clock_t timer;
  54. #endif
  55. // for showing the speed
  56. double fps;
  57. String text;
  58. char buffer [50];
  59. // set the default tracking algorithm
  60. String trackingAlg = "KCF";
  61. // set the tracking algorithm from parameter
  62. if(argc>2)
  63. trackingAlg = argv[2];
  64. // create the tracker
  65. legacy::MultiTracker trackers;
  66. // container of the tracked objects
  67. vector<Rect> ROIs;
  68. vector<Rect2d> objects;
  69. // set input video
  70. String video = argv[1];
  71. VideoCapture cap(video);
  72. Mat frame;
  73. // get bounding box
  74. cap >> frame;
  75. selectROIs("tracker",frame,ROIs);
  76. //quit when the tracked object(s) is not provided
  77. if(ROIs.size()<1)
  78. return 0;
  79. std::vector<Ptr<legacy::Tracker> > algorithms;
  80. for (size_t i = 0; i < ROIs.size(); i++)
  81. {
  82. algorithms.push_back(createTrackerByName_legacy(trackingAlg));
  83. objects.push_back(ROIs[i]);
  84. }
  85. // initialize the tracker
  86. trackers.add(algorithms,frame,objects);
  87. // do the tracking
  88. printf(GREEN "Start the tracking process, press ESC to quit.\n" RESET);
  89. for ( ;; ){
  90. // get frame from the video
  91. cap >> frame;
  92. // stop the program if no more images
  93. if(frame.rows==0 || frame.cols==0)
  94. break;
  95. // start the timer
  96. #ifdef HAVE_OPENCV
  97. timer.start();
  98. #else
  99. timer=clock();
  100. #endif
  101. //update the tracking result
  102. trackers.update(frame);
  103. // calculate the processing speed
  104. #ifdef HAVE_OPENCV
  105. timer.stop();
  106. fps=1.0/timer.value;
  107. timer.reset();
  108. #else
  109. timer=clock();
  110. trackers.update(frame);
  111. timer=clock()-timer;
  112. fps=(double)CLOCKS_PER_SEC/(double)timer;
  113. #endif
  114. // draw the tracked object
  115. for(unsigned i=0;i<trackers.getObjects().size();i++)
  116. rectangle( frame, trackers.getObjects()[i], Scalar( 255, 0, 0 ), 2, 1 );
  117. // draw the processing speed
  118. sprintf (buffer, "speed: %.0f fps", fps);
  119. text = buffer;
  120. putText(frame, text, Point(20,20), FONT_HERSHEY_PLAIN, 1, Scalar(255,255,255));
  121. // show image with the tracked object
  122. imshow("tracker",frame);
  123. //quit on ESC button
  124. if(waitKey(1)==27)break;
  125. }
  126. }