multitracker.cpp 3.43 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 "roiSelector.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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

  // for showing the speed
  double fps;
  std::string text;
  char buffer [50];

  // set the default tracking algorithm
  std::string trackingAlg = "KCF";

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

  // create the tracker
  MultiTracker trackers(trackingAlg);

  // container of the tracked objects
  vector<Rect2d> objects;

  // set input video
  std::string video = argv[1];
  VideoCapture cap(video);

  Mat frame;

  // get bounding box
  cap >> frame;
Kurnianggoro's avatar
Kurnianggoro committed
89
  selectROI("tracker",frame,objects);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

  //quit when the tracked object(s) is not provided
  if(objects.size()<1)
    return 0;

  // initialize the tracker
  trackers.add(frame,objects);

  // 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;

108
    // start the timer
109
#ifdef HAVE_OPENCV
110
    timer.start();
111 112 113
#else
    timer=clock();
#endif
114 115

    //update the tracking result
116
    trackers.update(frame);
117 118

    // calculate the processing speed
119
#ifdef HAVE_OPENCV
120 121 122
    timer.stop();
    fps=1.0/timer.value;
    timer.reset();
123 124 125 126 127 128
#else
    timer=clock();
    trackers.update(frame);
    timer=clock()-timer;
    fps=(double)CLOCKS_PER_SEC/(double)timer;
#endif
129 130 131

    // draw the tracked object
    for(unsigned i=0;i<trackers.objects.size();i++)
Kurnianggoro's avatar
Kurnianggoro committed
132
      rectangle( frame, trackers.objects[i], Scalar( 255, 0, 0 ), 2, 1 );
133 134

    // draw the processing speed
135
    sprintf (buffer, "speed: %.0f fps", fps);
136 137 138 139 140 141 142 143 144 145 146
    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;
  }

}