tutorial_multitracker.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include <opencv2/core/utility.hpp>
  10. #include <opencv2/tracking.hpp>
  11. #include <opencv2/videoio.hpp>
  12. #include <opencv2/highgui.hpp>
  13. #include <iostream>
  14. #include <cstring>
  15. #include <ctime>
  16. #include "samples_utility.hpp"
  17. using namespace std;
  18. using namespace cv;
  19. int main( int argc, char** argv ){
  20. // show help
  21. if(argc<2){
  22. cout<<
  23. " Usage: example_tracking_multitracker <video_name> [algorithm]\n"
  24. " examples:\n"
  25. " example_tracking_multitracker Bolt/img/%04d.jpg\n"
  26. " example_tracking_multitracker faceocc2.webm MEDIANFLOW\n"
  27. << endl;
  28. return 0;
  29. }
  30. // set the default tracking algorithm
  31. std::string trackingAlg = "KCF";
  32. // set the tracking algorithm from parameter
  33. if(argc>2)
  34. trackingAlg = argv[2];
  35. // create the tracker
  36. //! [create]
  37. legacy::MultiTracker trackers;
  38. //! [create]
  39. // container of the tracked objects
  40. //! [roi]
  41. vector<Rect2d> objects;
  42. //! [roi]
  43. // set input video
  44. std::string video = argv[1];
  45. VideoCapture cap(video);
  46. Mat frame;
  47. // get bounding box
  48. cap >> frame;
  49. //! [selectmulti]
  50. vector<Rect> ROIs;
  51. selectROIs("tracker",frame,ROIs);
  52. //! [selectmulti]
  53. //quit when the tracked object(s) is not provided
  54. if(ROIs.size()<1)
  55. return 0;
  56. // initialize the tracker
  57. //! [init]
  58. std::vector<Ptr<legacy::Tracker> > algorithms;
  59. for (size_t i = 0; i < ROIs.size(); i++)
  60. {
  61. algorithms.push_back(createTrackerByName_legacy(trackingAlg));
  62. objects.push_back(ROIs[i]);
  63. }
  64. trackers.add(algorithms,frame,objects);
  65. //! [init]
  66. // do the tracking
  67. printf("Start the tracking process, press ESC to quit.\n");
  68. for ( ;; ){
  69. // get frame from the video
  70. cap >> frame;
  71. // stop the program if no more images
  72. if(frame.rows==0 || frame.cols==0)
  73. break;
  74. //update the tracking result
  75. //! [update]
  76. trackers.update(frame);
  77. //! [update]
  78. //! [result]
  79. // draw the tracked object
  80. for(unsigned i=0;i<trackers.getObjects().size();i++)
  81. rectangle( frame, trackers.getObjects()[i], Scalar( 255, 0, 0 ), 2, 1 );
  82. //! [result]
  83. // show image with the tracked object
  84. imshow("tracker",frame);
  85. //quit on ESC button
  86. if(waitKey(1)==27)break;
  87. }
  88. }