kcf.cpp 1.79 KB
Newer Older
1 2
/*----------------------------------------------
 * Usage:
3
 * example_tracking_kcf <video_name>
4 5 6 7 8 9
 *
 * example:
 * example_tracking_kcf Bolt/img/%04.jpg
 * example_tracking_kcf faceocc2.webm
 *--------------------------------------------------*/

10
#include <opencv2/core/utility.hpp>
11
#include <opencv2/tracking.hpp>
12 13
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
14 15
#include <iostream>
#include <cstring>
16
#include "samples_utility.hpp"
17 18 19 20

using namespace std;
using namespace cv;

21 22 23 24 25 26 27 28 29 30 31 32
int main( int argc, char** argv ){
  // show help
  if(argc<2){
    cout<<
      " Usage: example_tracking_kcf <video_name>\n"
      " examples:\n"
      " example_tracking_kcf Bolt/img/%04.jpg\n"
      " example_tracking_kcf faceocc2.webm\n"
      << endl;
    return 0;
  }

33
  // create the tracker
34
  Ptr<Tracker> tracker = TrackerKCF::create();
35

36
  // set input video
37
  std::string video = argv[1];
38
  VideoCapture cap(video);
39 40 41 42 43

  Mat frame;

  // get bounding box
  cap >> frame;
44
  Rect2d roi= selectROI("tracker", frame, true, false);
45 46 47 48

  //quit if ROI was not selected
  if(roi.width==0 || roi.height==0)
    return 0;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

  // initialize the tracker
  tracker->init(frame,roi);

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

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

    // update the tracking result
64 65 66 67 68 69 70
    bool isfound = tracker->update(frame,roi);
    if(!isfound)
    {
        cout << "The target has been lost...\n";
        waitKey(0);
        return 0;
    }
71 72 73 74 75 76 77 78 79 80 81 82

    // draw the tracked object
    rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );

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

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

}