OpenCV 4.5.3, VS2017, MFC, C++, Win10
Using legacy tracking Multitracker. In all of the example code they push_back all of the trackers at the same time. The code can be seen in the OCV 4.5.3 docs at
https://docs.opencv.org/4.x/d5/d07/tutorial_multitracker.html
Earlier setup code has:
legacy::MultiTracker trackers;
std::vector<Ptr<legacy::Tracker> > algorithms;
vector<Rect2d> objects;
vector<cv::Rect> ROIs;
The code in question has:
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);
However, what I need to do is add trackers at different times. For instance, the first ROI might enter the video on frame 10 and the second ROI may enter on frame 30. So the two ROIs are not on the first frame at the same time. So I was skipping the for loop and when the fist ROI enters frame 10 I have
Ptr<cv::legacy::Tracker> TBtracker;
TBtracker = legacy::TrackerKCF::create();
algorithms.push_back(TBtracker);
objects.push_back(TargetBox);|
trackers.add(algorithms,frame,objects);
And then when the second ROI enters on frame 30 I have
Ptr<cv::legacy::Tracker> TB2tracker;
TB2tracker = legacy::TrackerKCF::create();
algorithms.push_back(TB2tracker);
objects.push_back(TargetBox2);|
trackers.add(algorithms,frame,objects);
What is happening, however, is that the objects[] have the correct rectangles but when the time comes to update the trackers with
trackers.update(frame);
The code to get the new tracked boxes
// draw the tracked object
for(unsigned i=0;i<trackers.getObjects().size();i++)
rectangle( frame, trackers.getObjects()[i], Scalar( 255, 0, 0 ), 2, 1 );
The trackers.getObjects()[i] , with i being 1 or the second object, the second rectangle now seems to be the previous first ROI rectangle or at least just slightly off from the first ROI. It has nothing to do with the second object.
Has anyone ever used Multitracker when the various trackers are added at different times?