Ask Your Question

How to reset or update KCF tracker ROI when it lose the target

asked 2016-11-05 16:32:10 -0500

alireza.kenarang gravatar image

updated 2020-10-07 19:30:26 -0500

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

(if you can't download it, please copy link and paste it in your browser address bar)

edit retag flag offensive close merge delete


have You got any solution?

saniket123 gravatar imagesaniket123 ( 2018-08-20 01:21:46 -0500 )edit

@saniket123 , please do not post answers here, if you have a question or comment. thank you.

berak gravatar imageberak ( 2018-08-20 01:28:47 -0500 )edit

@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

saniket123 gravatar imagesaniket123 ( 2018-08-20 03:40:00 -0500 )edit

^^ oh, thanks for the notice. maybe we have to review priviledges for new users, then.

berak gravatar imageberak ( 2018-08-20 03:52:05 -0500 )edit

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 gravatar imageberak ( 2018-08-20 03:53:42 -0500 )edit

@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?

saniket123 gravatar imagesaniket123 ( 2018-08-20 04:02:37 -0500 )edit

how do you get a new ROI to initialize it ?

berak gravatar imageberak ( 2018-08-20 04:07:59 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-11-06 18:15:40 -0500

Tetragramm gravatar image

If you check the documentation HERE, you can see that the update function returns a bool that is whether or not the object was detected. If it wasn't detected, you can just not draw the box.

edit flag offensive delete link more


i checked it, when a object was detected it was true but when the tracker lost object it returns true again. it can't understand that it lost object. Do you have check it ever?

alireza.kenarang gravatar imagealireza.kenarang ( 2016-11-07 10:46:25 -0500 )edit

indeed my target will exit from frame and update function will return true continuous.

alireza.kenarang gravatar imagealireza.kenarang ( 2016-11-07 11:32:28 -0500 )edit

Huh, you're right. That's silly, this method has a quality response, why didn't they use it? This is in the OpenCV Contrib modules, so it's not been tested and vetted the same way the main tree has.

If you look at the source, you see it has the response variable. I would add a check, if response is below some threshold, return false and don't update. It may require some fine tuning to find when it's lost, and it may just be specific to your task. Maybe 50% of the "perfect" response from frame 0.

Tetragramm gravatar imageTetragramm ( 2016-11-07 17:59:33 -0500 )edit

what should i do now? have you any new way? it is meaningless and useless if i can't solve this problem. in fact using this algorithm is useless.

alireza.kenarang gravatar imagealireza.kenarang ( 2016-11-08 00:25:51 -0500 )edit

It is not useless. You can do any number of things to detect when it fails.

  1. Modify the code to use the response variable.
  2. Is the bounding box at the edge?
  3. How closely does the detected box match the template at the beginning?

Or of course, any of the other algorithms. If you do number 1 though, we would appreciate it if you contributed it back to the repository. It would bring the algorithm closer to completeness and closer to being moved into the primary modules.

Tetragramm gravatar imageTetragramm ( 2016-11-08 11:02:41 -0500 )edit

thanks for your reply, even though none of these ways aren't a certain and accurate method, but i will try for your second offer. thanks for your guidance.

alireza.kenarang gravatar imagealireza.kenarang ( 2016-11-09 22:38:24 -0500 )edit

opencv 3.2 has bug that the bool return value always return true. Use 3.4.1 or above should be fine.

coleone08 gravatar imagecoleone08 ( 2019-10-14 13:38:48 -0500 )edit

Question Tools

1 follower


Asked: 2016-11-05 16:32:10 -0500

Seen: 6,858 times

Last updated: Aug 20 '18