Clear ROI history from kcf tracking in opencv

I use KCF tracking in OpenCV. everything is fine, and I can also track the object, but I have a problem: I set the ROI and the algorithm works fine, sometimes I need to change my ROI. there for the tracker should reset and track my new ROI, but it will not. in fact, the last ROI will remain in history, and this will affect the new location.

This is also a summary of my codes, I wrote important lines:

Rect2d roi;
Mat frame;
Ptr<Tracker> tracker = Tracker::create("KCF");
VideoCapture cap("C1_0001.mp4");
cap >> frame;
roi = selectROI("tracker", frame);

if (Condition = true)
{
roi = selectROI("tracker", frame);
}

tracker->init(frame, roi);
for (;; ) 
{
        cap >> frame;
        tracker->update(frame, roi);
}

I need to change roi when Condition is true.

+1
source share
1 answer

you need to call:

tracker->clear();
tracker = cv::Tracker::create("KCF");
tracker->init(frame, roi);

The problem was resolved here: OpenCV 3 Tracker will not work after reinitialization

+2

Source: https://habr.com/ru/post/1659955/


All Articles