OpenCV  3.1.0
Open Source Computer Vision
Using MultiTracker

Goal

In this tutorial you will learn how to

Source Code

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 #include <opencv2/core/utility.hpp>
11 #include <opencv2/tracking.hpp>
12 #include <opencv2/videoio.hpp>
13 #include <opencv2/highgui.hpp>
14 #include <iostream>
15 #include <cstring>
16 #include <ctime>
17 
18 using namespace std;
19 using namespace cv;
20 
21 int main( int argc, char** argv ){
22  // show help
23  if(argc<2){
24  cout<<
25  " Usage: example_tracking_multitracker <video_name> [algorithm]\n"
26  " examples:\n"
27  " example_tracking_multitracker Bolt/img/%04d.jpg\n"
28  " example_tracking_multitracker faceocc2.webm MEDIANFLOW\n"
29  << endl;
30  return 0;
31  }
32 
33  // set the default tracking algorithm
34  std::string trackingAlg = "KCF";
35 
36  // set the tracking algorithm from parameter
37  if(argc>2)
38  trackingAlg = argv[2];
39 
40  // create the tracker
42  MultiTracker trackers(trackingAlg);
44 
45  // container of the tracked objects
47  vector<Rect2d> objects;
49 
50  // set input video
51  std::string video = argv[1];
52  VideoCapture cap(video);
53 
54  Mat frame;
55 
56  // get bounding box
57  cap >> frame;
59  selectROI("tracker",frame,objects);
61 
62  //quit when the tracked object(s) is not provided
63  if(objects.size()<1)
64  return 0;
65 
66  // initialize the tracker
68  trackers.add(frame,objects);
70 
71  // do the tracking
72  printf("Start the tracking process, press ESC to quit.\n");
73  for ( ;; ){
74  // get frame from the video
75  cap >> frame;
76 
77  // stop the program if no more images
78  if(frame.rows==0 || frame.cols==0)
79  break;
80 
81  //update the tracking result
83  trackers.update(frame);
85 
87  // draw the tracked object
88  for(unsigned i=0;i<trackers.objects.size();i++)
89  rectangle( frame, trackers.objects[i], Scalar( 255, 0, 0 ), 2, 1 );
91 
92  // show image with the tracked object
93  imshow("tracker",frame);
94 
95  //quit on ESC button
96  if(waitKey(1)==27)break;
97  }
98 
99 }
STL namespace.
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple, thick, or filled up-right rectangle.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
Definition: affine.hpp:51
Class for video capturing from video files, image sequences or cameras. The class provides C++ API fo...
Definition: videoio.hpp:389
Rect2d selectROI(Mat img, bool fromCenter=true)
This class is used to track multiple objects using the specified tracker algorithm. The MultiTracker is naive implementation of multiple object tracking. It process the tracked objects independently without any optimization accross the tracked objects.
Definition: tracker.hpp:1265
int waitKey(int delay=0)
Waits for a pressed key.

Explanation

  1. Create the MultiTracker object

    MultiTracker trackers(trackingAlg);

    You can create the MultiTracker object and use the same tracking algorithm for all tracked object as shown in the snippet. If you want to use different type of tracking algorithm for each tracked object, you should define the tracking algorithm whenever a new object is added to the MultiTracker object.

  2. Selection of multiple objects

    selectROI("tracker",frame,objects);

    You can use cv::selectROI to select multiple objects with the result stored in a vector of cv::Rect2d as shown in the code. You can also use another kind of selection scheme, please refer to cv::selectROI for detailed information.

  3. Adding the tracked object to MultiTracker

    trackers.add(frame,objects);

    You can add all tracked objects at once to the MultiTracker as shown in the code. In this case, all objects will be tracked using same tracking algorithm as specified in decaration of MultiTracker object. If you want to use different tracker algorithms for each tracked object, You should add the tracked objects one by one and specify their tracking algorithm using the variant of cv::MultiTracker::add.

    See also
    cv::MultiTracker::add( const String& trackerType, const Mat& image, const Rect2d& boundingBox )
  4. Obtaining the result

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

    You can access the result from the public variable cv::MultiTracker::objects provided by the MultiTracker class as shown in the code.