"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::MultiTrackertrackers;
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.
You can use selectROI to select multiple objects with the result stored in a vector of cv::Rect2d as shown in the code.
Adding the tracked object to MultiTracker
std::vector<Ptr<legacy::Tracker> > algorithms;
for (size_ti = 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.