Multitracker adding objects at different times

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

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++)

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();



And then when the second ROI enters on frame 30 I have

Ptr<cv::legacy::Tracker> TB2tracker; 
TB2tracker = legacy::TrackerKCF::create();



What is happening, however, is that the objects[] have the correct rectangles but when the time comes to update the trackers with
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?


I just discovered MultiTracker_Alt which works a bit different and that seems to have been the solution for my needs.