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#
/*----------------------------------------------
* Usage:
* example_tracking_multitracker <video_name> [algorithm]
*
* example:
* example_tracking_multitracker Bolt/img/%04d.jpg
* example_tracking_multitracker faceocc2.webm KCF
*--------------------------------------------------*/
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>
#include <ctime>
#include "samples_utility.hpp"
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"
" example_tracking_multitracker Bolt/img/%04d.jpg\n"
" example_tracking_multitracker faceocc2.webm MEDIANFLOW\n"
<< endl;
return 0;
}
// set the default tracking algorithm
std::string trackingAlg = "KCF";
// set the tracking algorithm from parameter
if(argc>2)
trackingAlg = argv[2];
// create the tracker
legacy::MultiTracker trackers;
// 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;
vector<Rect> ROIs;
selectROIs("tracker",frame,ROIs);
//quit when the tracked object(s) is not provided
if(ROIs.size()<1)
return 0;
// initialize the tracker
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);
// 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
trackers.update(frame);
// draw the tracked object
for(unsigned i=0;i<trackers.getObjects().size();i++)
rectangle( frame, trackers.getObjects()[i], Scalar( 255, 0, 0 ), 2, 1 );
// show image with the tracked object
imshow("tracker",frame);
//quit on ESC button
if(waitKey(1)==27)break;
}
}
Explanation#
Create the MultiTracker object
legacy::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.
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](#cv::Rect2d) as shown in the code.
Adding the tracked object to MultiTracker
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 )
Obtaining the result
You can access the result from the public variable cv::legacy::MultiTracker::objects provided by the MultiTracker class as shown in the code.