OpenCV 3 Tracker won't work after reinitialization

旧街凉风 提交于 2019-11-28 07:41:17

问题


I have issue using OpenCV 3 tracking module for tracking. It behaves same, either I use interface class (cv::Tracker) or class with implementation (like cv::TrackerMedianFlow or cv::TrackerMIL, etc). Sample is a bit modified sample from OpenCV sample folder After correct creation

Ptr<Tracker> tracker = Tracker::create( tracker_algorithm );
if ( tracker == NULL )
{
    std::cout << "***Error in the instantiation of the tracker...***\n";
    return -1;
}

initialization works just fine

if ( !tracker->init( frame, boundingBox ) )
{
    std::cout << "***Could not initialize tracker...***\n";
    return -1;
}

Problem occurs late on, withing main loop, when tracking is lost. I use separate detector for defining new target. When I find new target, I clear tracker and initialize it with new value

                    tracker->clear( );
                    if ( !tracker->init( frame, detectedNewBBox) )
                    {
                        std::cout << "***Could not initialize tracker without history...***\n";
                        return -1;
                    }

However, initialization always returns false. I am trying to find out WHY tracker cannot be initialized? Data was check few time, and looks pretty correct. I even conducted small experiment, trying to initialize tracker right after creation with same data it won't initialize withing loop and it works perfect. Am I doing something wrong? I was unable to find any documentation on this... Here is link to available documentation on this: OpenCV 3 Tracker documentation

Thanks for any effort!


回答1:


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




回答2:


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



回答3:


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?



来源:https://stackoverflow.com/questions/31432815/opencv-3-tracker-wont-work-after-reinitialization

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