OpenCV 3 Tracker won't work after reinitialization

淺唱寂寞╮ 提交于 2019-11-29 14:05:55

I just ran into the same problem, here's how I got it to work :

tracker->clear();

Ptr<Tracker> trackerNew = Tracker::create( tracker_algorithm );

tracker = trackerNew;
tracker->init( image, boundingBox );

Might not be the right way or the prettiest but it does the job :)

If you want to track a new ROI (region of interest) then I suggest that you should create a new tracker instead of clearing and trying to reuse a previous tracker. Re-use when you need to call init will provide no extra benefit. As you have observed, re-initializing a tracker is not allowed by default.

But if you want to resume tracking of the same object with your correction, it might be possible by doing following steps (I have not tried it myself yet. following code is just a pseudo code)

Ptr<TrackerModel> model = tracker->getModel();
Ptr<TrackerTargetState> lastTargetstate = getLastTargetState();

// Make changes to lastTargetState (update position etc)

// Set lastTargetState, I am not sure if you need to actually set it
// or just editing the object through pointer should work.
model->setLastTargetState(lastTargetstate);

I ran into the same problem and here's my solution: Open the file in opencv_contrib/modules/tracking/src/tracker.cpp and apply the following changes:

-  if( isInit )
+  /*if( isInit )
   {
     return false;
   }
+  */

I recompiled opencv3 and reinstalled it. That fixed it for me. I think they did not want people to reinitialize the tracker for some reason. I am not sure why?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!