OpenCV 3 Tracker won't work after reinitialization

Nikola picture Nikola · Jul 15, 2015 · Viewed 7.9k times · Source

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!

Answer

Thom picture Thom · Sep 16, 2015

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