How to reset or update KCF tracker ROI when it lose the target
Hello I am using KCF tracking algorithm, my problem is when the target exit from window, the tracker won't reset and show it's rectangle on edge of window wrongly. in ideal state tracker should delete the rectangle when it lose the target.
These are my codes:
int main(int argc, char** argv) {
Rect2d roi;
Mat frame;
// create a tracker object
Ptr<Tracker> tracker = Tracker::create("KCF");
VideoCapture cap("C2_0002.mp4");
cap >> frame;
resize(frame, frame, Size(frame.cols / 2, frame.rows / 2));
roi = selectROI("tracker", frame);
//quit if ROI was not selected
if (roi.width == 0 || roi.height == 0)
return 0;
// initialize the tracker
tracker->init(frame, roi);
// perform the tracking process
printf("Start the tracking process, press ESC to quit.\n");
for (;; ) {
// get frame from the video
cap >> frame;
resize(frame, frame, Size(frame.cols / 2, frame.rows / 2));
// stop the program if no more images
if (frame.rows == 0 || frame.cols == 0)
break;
// update the tracking result
tracker->update(frame, roi);
rectangle(frame, roi, Scalar(255, 0, 0), 2, 1);
imshow("tracker", frame);
if (waitKey(1) == 27)break;
}
}
Also you can see a short video of my simulation and see the problem:
www.0up.ir/do.php?downf=4_e2aa9.mp4
(if you can't download it, please copy link and paste it in your browser address bar)
have You got any solution?
@saniket123 , please do not post answers here, if you have a question or comment. thank you.
@berak i was not able to post either comment or answer, but it in answer box it got posted automatically after sometime. i will take care from next time
^^ oh, thanks for the notice. maybe we have to review priviledges for new users, then.
and the tracker situation is still the same, you can't re-init a tracker, but you have to release() it, and build a new one.
@berak i think this could be one solution not sure.. but asking cud it be possible? after tracker update check the roi. if it goes out of frame with some values. use goto label.. suppose my frame is 640X480 and roi is 120X120 which is moving in frame. if roi goes out of frame then using goto label. can we do it?
how do you get a new ROI to initialize it ?