multitracker.cpp 3.63 KB
Newer Older
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 10 11
 *
 * Note: after the OpenCV libary is installed,
 * please re-compile this code with "HAVE_OPENCV" parameter activated
 * to enable the high precission of fps computation
12 13
 *--------------------------------------------------*/

14 15 16 17 18
/* after the OpenCV libary is installed
 * please uncomment the the line below and re-compile this code
 * to enable high precission of fps computation
 */
//#define HAVE_OPENCV
19

20 21 22 23 24 25 26
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>
#include <ctime>
27
#include "samples_utility.hpp"
28

29 30 31 32
#ifdef HAVE_OPENCV
#include <opencv2/flann.hpp>
#endif

33 34 35 36 37 38 39 40 41 42 43 44 45
#define RESET   "\033[0m"
#define RED     "\033[31m"      /* Red */
#define GREEN   "\033[32m"      /* Green */

using namespace std;
using namespace cv;

int main( int argc, char** argv ){
  // show help
  if(argc<2){
    cout<<
      " Usage: example_tracking_multitracker <video_name> [algorithm]\n"
      " examples:\n"
46
      " example_tracking_multitracker Bolt/img/%04d.jpg\n"
47
      " example_tracking_multitracker faceocc2.webm MEDIANFLOW\n"
48 49 50 51
      " \n"
      " Note: after the OpenCV libary is installed,\n"
      " please re-compile with the HAVE_OPENCV parameter activated\n"
      " to enable the high precission of fps computation.\n"
52 53 54 55 56
      << endl;
    return 0;
  }

  // timer
57
#ifdef HAVE_OPENCV
58
  cvflann::StartStopTimer timer;
59 60 61
#else
  clock_t timer;
#endif
62 63 64

  // for showing the speed
  double fps;
65
  String text;
66 67 68
  char buffer [50];

  // set the default tracking algorithm
69
  String trackingAlg = "KCF";
70 71 72 73 74 75

  // set the tracking algorithm from parameter
  if(argc>2)
    trackingAlg = argv[2];

  // create the tracker
76
  MultiTracker trackers;
77 78

  // container of the tracked objects
79
  vector<Rect> ROIs;
80 81 82
  vector<Rect2d> objects;

  // set input video
83
  String video = argv[1];
84 85 86 87 88 89
  VideoCapture cap(video);

  Mat frame;

  // get bounding box
  cap >> frame;
90
  selectROIs("tracker",frame,ROIs);
91 92

  //quit when the tracked object(s) is not provided
klchang's avatar
klchang committed
93
  if(ROIs.size()<1)
94 95
    return 0;

96 97 98 99 100 101 102
  std::vector<Ptr<Tracker> > algorithms;
  for (size_t i = 0; i < ROIs.size(); i++)
  {
      algorithms.push_back(createTrackerByName(trackingAlg));
      objects.push_back(ROIs[i]);
  }

103
  // initialize the tracker
104
  trackers.add(algorithms,frame,objects);
105 106 107 108 109 110 111 112 113 114 115

  // do the tracking
  printf(GREEN "Start the tracking process, press ESC to quit.\n" RESET);
  for ( ;; ){
    // get frame from the video
    cap >> frame;

    // stop the program if no more images
    if(frame.rows==0 || frame.cols==0)
      break;

116
    // start the timer
117
#ifdef HAVE_OPENCV
118
    timer.start();
119 120 121
#else
    timer=clock();
#endif
122 123

    //update the tracking result
124
    trackers.update(frame);
125 126

    // calculate the processing speed
127
#ifdef HAVE_OPENCV
128 129 130
    timer.stop();
    fps=1.0/timer.value;
    timer.reset();
131 132 133 134 135 136
#else
    timer=clock();
    trackers.update(frame);
    timer=clock()-timer;
    fps=(double)CLOCKS_PER_SEC/(double)timer;
#endif
137 138

    // draw the tracked object
139 140
    for(unsigned i=0;i<trackers.getObjects().size();i++)
      rectangle( frame, trackers.getObjects()[i], Scalar( 255, 0, 0 ), 2, 1 );
141 142

    // draw the processing speed
143
    sprintf (buffer, "speed: %.0f fps", fps);
144 145 146 147 148 149 150 151 152 153 154
    text = buffer;
    putText(frame, text, Point(20,20), FONT_HERSHEY_PLAIN, 1, Scalar(255,255,255));

    // show image with the tracked object
    imshow("tracker",frame);

    //quit on ESC button
    if(waitKey(1)==27)break;
  }

}