tracker.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <opencv2/core/utility.hpp>
  2. #include <opencv2/tracking.hpp>
  3. #include <opencv2/videoio.hpp>
  4. #include <opencv2/highgui.hpp>
  5. #include <iostream>
  6. #include <cstring>
  7. #include "samples_utility.hpp"
  8. using namespace std;
  9. using namespace cv;
  10. static const char* keys =
  11. { "{@tracker_algorithm | | Tracker algorithm }"
  12. "{@video_name | | video name }"
  13. "{@start_frame |0| Start frame }"
  14. "{@bounding_frame |0,0,0,0| Initial bounding frame}"};
  15. static void help()
  16. {
  17. cout << "\nThis example shows the functionality of \"Long-term optical tracking API\""
  18. "-- pause video [p] and draw a bounding box around the target to start the tracker\n"
  19. "Example of <video_name> is in opencv_extra/testdata/cv/tracking/\n"
  20. "Call:\n"
  21. "./tracker <tracker_algorithm> <video_name> <start_frame> [<bounding_frame>]\n"
  22. "tracker_algorithm can be: MIL, BOOSTING, MEDIANFLOW, TLD, KCF, GOTURN, MOSSE.\n"
  23. << endl;
  24. cout << "\n\nHot keys: \n"
  25. "\tq - quit the program\n"
  26. "\tp - pause video\n";
  27. }
  28. int main( int argc, char** argv ){
  29. CommandLineParser parser( argc, argv, keys );
  30. String tracker_algorithm = parser.get<String>( 0 );
  31. String video_name = parser.get<String>( 1 );
  32. int start_frame = parser.get<int>( 2 );
  33. if( tracker_algorithm.empty() || video_name.empty() )
  34. {
  35. help();
  36. return -1;
  37. }
  38. int coords[4]={0,0,0,0};
  39. bool initBoxWasGivenInCommandLine=false;
  40. {
  41. String initBoundingBox=parser.get<String>(3);
  42. for(size_t npos=0,pos=0,ctr=0;ctr<4;ctr++){
  43. npos=initBoundingBox.find_first_of(',',pos);
  44. if(npos==string::npos && ctr<3){
  45. printf("bounding box should be given in format \"x1,y1,x2,y2\",where x's and y's are integer coordinates of opposed corners of bdd box\n");
  46. printf("got: %s\n",initBoundingBox.substr(pos,string::npos).c_str());
  47. printf("manual selection of bounding box will be employed\n");
  48. break;
  49. }
  50. int num=atoi(initBoundingBox.substr(pos,(ctr==3)?(string::npos):(npos-pos)).c_str());
  51. if(num<=0){
  52. printf("bounding box should be given in format \"x1,y1,x2,y2\",where x's and y's are integer coordinates of opposed corners of bdd box\n");
  53. printf("got: %s\n",initBoundingBox.substr(pos,npos-pos).c_str());
  54. printf("manual selection of bounding box will be employed\n");
  55. break;
  56. }
  57. coords[ctr]=num;
  58. pos=npos+1;
  59. }
  60. if(coords[0]>0 && coords[1]>0 && coords[2]>0 && coords[3]>0){
  61. initBoxWasGivenInCommandLine=true;
  62. }
  63. }
  64. //open the capture
  65. VideoCapture cap;
  66. cap.open( video_name );
  67. cap.set( CAP_PROP_POS_FRAMES, start_frame );
  68. if( !cap.isOpened() )
  69. {
  70. help();
  71. cout << "***Could not initialize capturing...***\n";
  72. cout << "Current parameter's value: \n";
  73. parser.printMessage();
  74. return -1;
  75. }
  76. Mat frame;
  77. namedWindow( "Tracking API", 1 );
  78. Mat image;
  79. Rect boundingBox;
  80. bool paused = false;
  81. //instantiates the specific Tracker
  82. Ptr<Tracker> tracker = createTrackerByName(tracker_algorithm);
  83. if (!tracker)
  84. {
  85. cout << "***Error in the instantiation of the tracker...***\n";
  86. return -1;
  87. }
  88. //get the first frame
  89. cap >> frame;
  90. frame.copyTo( image );
  91. if(initBoxWasGivenInCommandLine){
  92. boundingBox.x = coords[0];
  93. boundingBox.y = coords[1];
  94. boundingBox.width = std::abs( coords[2] - coords[0] );
  95. boundingBox.height = std::abs( coords[3]-coords[1]);
  96. printf("bounding box with vertices (%d,%d) and (%d,%d) was given in command line\n",coords[0],coords[1],coords[2],coords[3]);
  97. rectangle( image, boundingBox, Scalar( 255, 0, 0 ), 2, 1 );
  98. }
  99. else
  100. boundingBox = selectROI("Tracking API", image);
  101. imshow( "Tracking API", image );
  102. bool initialized = false;
  103. int frameCounter = 0;
  104. int64 timeTotal = 0;
  105. for ( ;; )
  106. {
  107. if( !paused )
  108. {
  109. if(initialized){
  110. cap >> frame;
  111. if(frame.empty()){
  112. break;
  113. }
  114. frame.copyTo( image );
  115. }
  116. if( !initialized )
  117. {
  118. //initializes the tracker
  119. tracker->init(frame, boundingBox);
  120. initialized = true;
  121. }
  122. else if( initialized )
  123. {
  124. int64 frameTime = getTickCount();
  125. //updates the tracker
  126. if( tracker->update( frame, boundingBox ) )
  127. {
  128. rectangle( image, boundingBox, Scalar( 255, 0, 0 ), 2, 1 );
  129. }
  130. frameTime = getTickCount() - frameTime;
  131. timeTotal += frameTime;
  132. }
  133. imshow( "Tracking API", image );
  134. frameCounter++;
  135. }
  136. char c = (char) waitKey( 2 );
  137. if( c == 'q' )
  138. break;
  139. if( c == 'p' )
  140. paused = !paused;
  141. }
  142. double s = frameCounter / (timeTotal / getTickFrequency());
  143. printf("FPS: %f\n", s);
  144. return 0;
  145. }