title: Opencv kalman filter prediction without new observtion
body: <p>I wan to use Opencv Kalman filter implementation for smooth some noise points. So I've tried to code a simple test for it.</p>
  
  <p>Let's say I have an observation (a point). Each frame I'm receiving new observation, I call Kalman predict and Kalman correct. The state coming after opencv Kalman filter correct is "following the point", it is ok.</p>
  
  <p>Then let's say I have a missing observation, I want anyway the Kalman filter to be updated and predict the new state. Here my code is failing: if I call kalman.predict() the value is no more updated.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;sys/time.h&gt;
  
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/video/tracking.hpp&gt;
  
  using namespace cv;
  using namespace std;
  
  //------------------------------------------------ convenience method for 
  //                                                 using kalman filter with 
  //                                                 Point objects
  cv::KalmanFilter KF;
  cv::Mat_&lt;float&gt; measurement(2,1); 
  Mat_&lt;float&gt; state(4, 1); // (x, y, Vx, Vy)
  
  void initKalman(float x, float y)
  {
      // Instantate Kalman Filter with
      // 4 dynamic parameters and 2 measurement parameters,
      // where my measurement is: 2D location of object,
      // and dynamic is: 2D location and 2D velocity.
      KF.init(4, 2, 0);
  
      measurement = Mat_&lt;float&gt;::zeros(2,1);
      measurement.at&lt;float&gt;(0, 0) = x;
      measurement.at&lt;float&gt;(0, 0) = y;
  
  
      KF.statePre.setTo(0);
      KF.statePre.at&lt;float&gt;(0, 0) = x;
      KF.statePre.at&lt;float&gt;(1, 0) = y;
  
      KF.statePost.setTo(0);
      KF.statePost.at&lt;float&gt;(0, 0) = x;
      KF.statePost.at&lt;float&gt;(1, 0) = y; 
  
      setIdentity(KF.transitionMatrix);
      setIdentity(KF.measurementMatrix);
      setIdentity(KF.processNoiseCov, Scalar::all(.005)); //adjust this for faster convergence - but higher noise
      setIdentity(KF.measurementNoiseCov, Scalar::all(1e-1));
      setIdentity(KF.errorCovPost, Scalar::all(.1));
  }
  
  Point kalmanPredict() 
  {
      Mat prediction = KF.predict();
      Point predictPt(prediction.at&lt;float&gt;(0),prediction.at&lt;float&gt;(1));
      return predictPt;
  }
  
  Point kalmanCorrect(float x, float y)
  {
      measurement(0) = x;
      measurement(1) = y;
      Mat estimated = KF.correct(measurement);
      Point statePt(estimated.at&lt;float&gt;(0),estimated.at&lt;float&gt;(1));
      return statePt;
  }
  
  //------------------------------------------------ main
  
  int main (int argc, char * const argv[]) 
  {
      Point s, p;
  
      initKalman(0, 0);
  
      p = kalmanPredict();
      cout &lt;&lt; "kalman prediction: " &lt;&lt; p.x &lt;&lt; " " &lt;&lt; p.y &lt;&lt; endl;
      /* 
       * output is: kalman prediction: 0 0
       *
       * note 1:
       *         ok, the initial value, not yet new observations
       */
  
      s = kalmanCorrect(10, 10);
      cout &lt;&lt; "kalman corrected state: " &lt;&lt; s.x &lt;&lt; " " &lt;&lt; s.y &lt;&lt; endl;
      /* 
       * output is: kalman corrected state: 5 5
       *
       * note 2:
       *         ok, kalman filter is smoothing the noisy observation and 
       *         slowly "following the point"
       *         .. how faster the kalman filter follow the point is 
       *            processNoiseCov parameter
       */
  
      p = kalmanPredict();
      cout &lt;&lt; "kalman prediction: " &lt;&lt; p.x &lt;&lt; " " &lt;&lt; p.y &lt;&lt; endl;
      /* 
       * output is: kalman prediction: 5 5
       *
       * note 3:
       *         mhmmm, same as the last correction, probabilly there are so few data that
       *         the filter is not predicting anything..
       */
  
      s = kalmanCorrect(20, 20);
      cout &lt;&lt; "kalman corrected state: " &lt;&lt; s.x &lt;&lt; " " &lt;&lt; s.y &lt;&lt; endl;
      /* 
       * output is: kalman corrected state: 10 10
       *
       * note 3:
       *         ok, same as note 2
       */
  
      p = kalmanPredict();
      cout &lt;&lt; "kalman prediction: " &lt;&lt; p.x &lt;&lt; " " &lt;&lt; p.y &lt;&lt; endl;
      s = kalmanCorrect(30, 30);
      cout &lt;&lt; "kalman corrected state: " &lt;&lt; s.x &lt;&lt; " " &lt;&lt; s.y &lt;&lt; endl;
      /* 
       * output is: kalman prediction: 10 10
       *            kalman corrected state: 16 16
       *
       * note 4:
       *         ok, same as note 2 and 3
       */    
  
  
      /*
       * now let's say I don't received observation for few frames,
       * I want anyway to update the kalman filter to predict 
       * the future states of my system
       *
       */
      for(int i=0; i&lt;5; i++) {
          p = kalmanPredict();
          cout &lt;&lt; "kalman prediction: " &lt;&lt; p.x &lt;&lt; " " &lt;&lt; p.y &lt;&lt; endl;
      }
      /* 
       * output is: kalman prediction: 16 16
       * kalman prediction: 16 16
       * kalman prediction: 16 16
       * kalman prediction: 16 16
       * kalman prediction: 16 16
       *
       * !!! kalman filter is still on 16, 16..
       *     no future prediction here..
       *     I'm exprecting the point to go further..
       *     why???
       *
       */    
  
      return 0;
  }
  </code></pre>
  
  <p>I think that this code is quite illustrative of what I don't understand. I've tried to follow <a href="http://www.cs.unc.edu/~welch/media/pdf/kalman_intro.pdf">some theory</a> and some <a href="http://www.morethantechnical.com/2011/06/17/simple-kalman-filter-for-tracking-using-opencv-2-2-w-code/">practical example</a> but don't still unserstand how to get new prediction of the future position..</p>
  
  <p>Anyone can help me in understand what I'm doing wrong?</p>