tracker.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include <opencv2/opencv.hpp>
  2. #include <opencv2/tracking.hpp>
  3. #include <opencv2/core/ocl.hpp>
  4. using namespace cv;
  5. using namespace std;
  6. // Convert to string
  7. #define SSTR( x ) static_cast< std::ostringstream & >( \
  8. ( std::ostringstream() << std::dec << x ) ).str()
  9. int main(int argc, char **argv)
  10. {
  11. // List of tracker types in OpenCV 3.2
  12. // NOTE : GOTURN implementation is buggy and does not work.
  13. string trackerTypes[7] = {"BOOSTING", "MIL", "KCF", "TLD","MEDIANFLOW", "GOTURN", "CSRT"};
  14. // vector <string> trackerTypes(types, std::end(types));
  15. // Create a tracker
  16. string trackerType = trackerTypes[2];
  17. Ptr<Tracker> tracker;
  18. #if (CV_MINOR_VERSION < 3)
  19. {
  20. tracker = Tracker::create(trackerType);
  21. }
  22. #else
  23. {
  24. if (trackerType == "BOOSTING")
  25. tracker = TrackerBoosting::create();
  26. if (trackerType == "MIL")
  27. tracker = TrackerMIL::create();
  28. if (trackerType == "KCF")
  29. tracker = TrackerKCF::create();
  30. if (trackerType == "TLD")
  31. tracker = TrackerTLD::create();
  32. if (trackerType == "MEDIANFLOW")
  33. tracker = TrackerMedianFlow::create();
  34. if (trackerType == "GOTURN")
  35. tracker = TrackerGOTURN::create();
  36. if (trackerType == "CSRT")
  37. tracker = TrackerCSRT::create();
  38. }
  39. #endif
  40. // Read video
  41. VideoCapture video("videos/chaplin.mp4");
  42. // Exit if video is not opened
  43. if(!video.isOpened())
  44. {
  45. cout << "Could not read video file" << endl;
  46. return 1;
  47. }
  48. // Read first frame
  49. Mat frame;
  50. bool ok = video.read(frame);
  51. // Define initial boundibg box
  52. Rect2d bbox(287, 23, 86, 320);
  53. // Uncomment the line below to select a different bounding box
  54. bbox = selectROI(frame, false);
  55. // Display bounding box.
  56. rectangle(frame, bbox, Scalar( 255, 0, 0 ), 2, 1 );
  57. imshow("Tracking", frame);
  58. tracker->init(frame, bbox);
  59. while(video.read(frame))
  60. {
  61. // Start timer
  62. double timer = (double)getTickCount();
  63. // Update the tracking result
  64. bool ok = tracker->update(frame, bbox);
  65. // Calculate Frames per second (FPS)
  66. float fps = getTickFrequency() / ((double)getTickCount() - timer);
  67. if (ok)
  68. {
  69. // Tracking success : Draw the tracked object
  70. rectangle(frame, bbox, Scalar( 255, 0, 0 ), 2, 1 );
  71. }
  72. else
  73. {
  74. // Tracking failure detected.
  75. putText(frame, "Tracking failure detected", Point(100,80), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0,0,255),2);
  76. }
  77. // Display tracker type on frame
  78. putText(frame, trackerType + " Tracker", Point(100,20), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50,170,50),2);
  79. // Display FPS on frame
  80. putText(frame, "FPS : " + SSTR(int(fps)), Point(100,50), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(50,170,50), 2);
  81. // Display frame.
  82. imshow("Tracking", frame);
  83. // Exit if ESC pressed.
  84. int k = waitKey(1);
  85. if(k == 27)
  86. {
  87. break;
  88. }
  89. }
  90. }