# Multitracker adding objects at different times

**URL:** <https://forum.opencv.org/t/multitracker-adding-objects-at-different-times/6415>\
**Category:** C++\
**Created:** [November 24, 2021, 6:07pm UTC](https://forum.opencv.org/t/multitracker-adding-objects-at-different-times/6415 "2021-11-24T18:07:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehardin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ehardin/32/2541_2.png) [@ehardin](https://forum.opencv.org/u/ehardin)\
**Post date:** [November 24, 2021, 6:07pm UTC](https://forum.opencv.org/t/multitracker-adding-objects-at-different-times/6415/1 "2021-11-24T18:07:52Z")

</div>

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](https://docs.opencv.org/4.x/d5/d07/tutorial_multitracker.html)

Earlier setup code has:

```auto
 legacy::MultiTracker trackers;
 std::vector<Ptr<legacy::Tracker> > algorithms;
 vector<Rect2d> objects;
vector<cv::Rect> ROIs;

```

The code in question has:

```auto
  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

```auto
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

```auto
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

```auto
// 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?

---

<div class="post-metadata">

**Author:** ![ehardin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ehardin/32/2541_2.png) [@ehardin](https://forum.opencv.org/u/ehardin)\
**Post date:** [November 24, 2021, 8:16pm UTC](https://forum.opencv.org/t/multitracker-adding-objects-at-different-times/6415/2 "2021-11-24T20:16:03Z")

</div>

Solution:

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

Ed
