OpenCV  3.4.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 #include "samples_utility.hpp"
18 
19 using namespace std;
20 using namespace cv;
21 
22 int main( int argc, char** argv ){
23  // show help
24  if(argc<2){
25  cout<<
26  " Usage: example_tracking_multitracker <video_name> [algorithm]\n"
27  " examples:\n"
28  " example_tracking_multitracker Bolt/img/%04d.jpg\n"
29  " example_tracking_multitracker faceocc2.webm MEDIANFLOW\n"
30  << endl;
31  return 0;
32  }
33 
34  // set the default tracking algorithm
35  std::string trackingAlg = "KCF";
36 
37  // set the tracking algorithm from parameter
38  if(argc>2)
39  trackingAlg = argv[2];
40 
41  // create the tracker
43  MultiTracker trackers;
45 
46  // container of the tracked objects
48  vector<Rect2d> objects;
50 
51  // set input video
52  std::string video = argv[1];
53  VideoCapture cap(video);
54 
55  Mat frame;
56 
57  // get bounding box
58  cap >> frame;
60  vector<Rect> ROIs;
61  selectROIs("tracker",frame,ROIs);
63 
64  //quit when the tracked object(s) is not provided
65  if(ROIs.size()<1)
66  return 0;
67 
68  // initialize the tracker
70  std::vector<Ptr<Tracker> > algorithms;
71  for (size_t i = 0; i < ROIs.size(); i++)
72  {
73  algorithms.push_back(createTrackerByName(trackingAlg));
74  objects.push_back(ROIs[i]);
75  }
76 
77  trackers.add(algorithms,frame,objects);
79 
80  // do the tracking
81  printf("Start the tracking process, press ESC to quit.\n");
82  for ( ;; ){
83  // get frame from the video
84  cap >> frame;
85 
86  // stop the program if no more images
87  if(frame.rows==0 || frame.cols==0)
88  break;
89 
90  //update the tracking result
92  trackers.update(frame);
94 
96  // draw the tracked object
97  for(unsigned i=0;i<trackers.getObjects().size();i++)
98  rectangle( frame, trackers.getObjects()[i], Scalar( 255, 0, 0 ), 2, 1 );
100 
101  // show image with the tracked object
102  imshow("tracker",frame);
103 
104  //quit on ESC button
105  if(waitKey(1)==27)break;
106  }
107 
108 }
Scalar_< double > Scalar
Definition: types.hpp:664
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.
Definition: videoio.hpp:601
bool add(Ptr< Tracker > newTracker, InputArray image, const Rect2d &boundingBox)
Add a new object to be tracked.
const std::vector< Rect2d > & getObjects() const
Returns a reference to a storage for the tracked objects, each object corresponds to one tracker algo...
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:1321
void selectROIs(const String &windowName, InputArray img, std::vector< Rect > &boundingBoxes, bool showCrosshair=true, bool fromCenter=false)
Selects ROIs on the given image. Function creates a window and allows user to select a ROIs using mou...
bool update(InputArray image)
Update the current tracking status. The result will be saved in the internal storage.
int waitKey(int delay=0)
Waits for a pressed key.

Explanation

  1. Create the MultiTracker object

    MultiTracker trackers;

    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

    vector<Rect> ROIs;
    selectROIs("tracker",frame,ROIs);

    You can use selectROI to select multiple objects with the result stored in a vector of cv::Rect2d as shown in the code.

  3. Adding the tracked object to MultiTracker

    std::vector<Ptr<Tracker> > algorithms;
    for (size_t i = 0; i < ROIs.size(); i++)
    {
    algorithms.push_back(createTrackerByName(trackingAlg));
    objects.push_back(ROIs[i]);
    }
    trackers.add(algorithms,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.getObjects().size();i++)
    rectangle( frame, trackers.getObjects()[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.