Goal
In this tutorial you will learn how to
- Create a MultiTracker object.
- Track several objects at once using the MultiTracker object.
Source Code
21 int main(
int argc,
char** argv ){
25 " Usage: example_tracking_multitracker <video_name> [algorithm]\n" 27 " example_tracking_multitracker Bolt/img/%04d.jpg\n" 28 " example_tracking_multitracker faceocc2.webm MEDIANFLOW\n" 34 std::string trackingAlg =
"KCF";
38 trackingAlg = argv[2];
47 vector<Rect2d> objects;
51 std::string video = argv[1];
68 trackers.add(frame,objects);
72 printf(
"Start the tracking process, press ESC to quit.\n");
78 if(frame.rows==0 || frame.cols==0)
83 trackers.update(frame);
88 for(
unsigned i=0;i<trackers.objects.size();i++)
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:596
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:1294
int waitKey(int delay=0)
Waits for a pressed key.
Explanation
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.
Selection of multiple 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.
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 )
Obtaining the result
for(unsigned i=0;i<trackers.objects.size();i++)
You can access the result from the public variable cv::MultiTracker::objects provided by the MultiTracker class as shown in the code.