Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV 3 Tracker won't work after reinitialization

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!

like image 387
Nikola Avatar asked Jul 15 '15 14:07

Nikola


Video Answer


2 Answers

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

like image 88
Thom Avatar answered Nov 18 '22 09:11

Thom


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);
like image 24
Mustafa Avatar answered Nov 18 '22 09:11

Mustafa