OpenCV 4.11.0-pre
Open Source Computer Vision
Loading...
Searching...
No Matches
Using MultiTracker

Goal

In this tutorial you will learn how to

  • Create a MultiTracker object.
  • Track several objects at once using the MultiTracker object.

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
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
19using namespace std;
20using namespace cv;
21
22int 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 legacy::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<legacy::Tracker> > algorithms;
71 for (size_t i = 0; i < ROIs.size(); i++)
72 {
73 algorithms.push_back(createTrackerByName_legacy(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}
n-dimensional dense array class
Definition mat.hpp:829
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:766
This class is used to track multiple objects using the specified tracker algorithm.
Definition tracking_legacy.hpp:352
const std::vector< Rect2d > & getObjects() const
Returns a reference to a storage for the tracked objects, each object corresponds to one tracker algo...
bool add(Ptr< cv::legacy::Tracker > newTracker, InputArray image, const Rect2d &boundingBox)
Add a new object to be tracked.
bool update(InputArray image)
Update the current tracking status. The result will be saved in the internal storage.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition core.hpp:107
STL namespace.

Explanation

  1. Create the MultiTracker object

    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<legacy::Tracker> > algorithms;
    for (size_t i = 0; i < ROIs.size(); i++)
    {
    algorithms.push_back(createTrackerByName_legacy(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::legacy::MultiTracker::add.

    See also
    cv::legacy::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::legacy::MultiTracker::objects provided by the MultiTracker class as shown in the code.