5

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: http://www.0up.ir/do.php?downf=4_e2aa9.mp4

Prakash M
  • 659
  • 1
  • 12
  • 36
  • I was unable to load your video, maybe reupload ? – Michal Gallovic Nov 05 '16 at 21:42
  • @MichalGallovic - I'm sorry, i solve download link issue. you can download it now – Alireza Kenarang Nov 05 '16 at 21:52
  • resetting can be done as shown [here](http://stackoverflow.com/questions/31432815/opencv-3-tracker-wont-work-after-reinitialization) and [here](http://stackoverflow.com/questions/40408616/clear-roi-history-from-kcf-tracking-in-opencv) – Prakash M Dec 28 '16 at 08:36
  • Did you found the solution to this problem? I can't find the condition under which tracker update returns false. My ideas is to maybe lower the threshold or something. – Milos Milunovic Sep 20 '19 at 13:42

1 Answers1

0

can you specify it a little more?
a) you want to delete the whole tracker
b) you just don't want to print the rectangle if there is no update on tracker

I am not sure if the tracker will work again if the object returns (returns at same position or at another position next to the old exit position), but I think the most simple solution for displaying is:

Create a variable to count the steps were tracker->update(frame, roi) returns false and set it to 0 if tracker->update(frame, roi) returns true. Above a certain number(threshold) rectangle(frame, roi, Scalar(255, 0, 0), 2, 1) should not be called or you can delete the tracker/break the loop(like I said before I am not sure if it will work again, additionally the tracker uses active learning - it learns with every step: false training samples will lead to wrong behaviour)

fyi->the original paper: http://www.robots.ox.ac.uk/~joao/publications/henriques_eccv2012.pdf)

00zetti
  • 114
  • 8