How to reset or update KCF tracker ROI when it lose the target
<p>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.</p>

<p>These are my codes:</p>

<pre><code>int main(int argc, char** argv) {
        Rect2d roi;
        Mat frame;

        // create a tracker object
        Ptr&lt;Tracker&gt; tracker = Tracker::create("KCF");

        VideoCapture cap("C2_0002.mp4");

        cap &gt;&gt; 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-&gt;init(frame, roi);
        // perform the tracking process
        printf("Start the tracking process, press ESC to quit.\n");
        for (;; ) {

                // get frame from the video
            cap &gt;&gt; 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-&gt;update(frame, roi);

            rectangle(frame, roi, Scalar(255, 0, 0), 2, 1);

            imshow("tracker", frame);
            if (waitKey(1) == 27)break;
        }
}
</code></pre>

<p>Also you can see a short video of my simulation and see the problem:
<a href="http://www.0up.ir/do.php?downf=4_e2aa9.mp4" rel="nofollow noreferrer">http://www.0up.ir/do.php?downf=4_e2aa9.mp4</a></p>