title: Tracking with kalman filter: prediction vs correction
body: <p>I have a CV tracking algorithm that gives me the 2D coordinates of the centroid of the object of interest (a red ball) in real time. I want to use a Kalman Filter to obtain the predicted coordinates of the ball in the next frame (future).</p>
  <p>The thing is that I don't know if I should:</p>
  <ul>
  <li>Predict (state k), Correct (state k), and then Predict again (state k+1).</li>
  <li>Correct (state k), and then Predict (state k+1).</li>
  <li>Predict (state k), Correct (state k).</li>
  </ul>
  <p>The first two approaches gave me decent results. However, the results obtained in the last approach were practically the same as the mesures (I guess this is because I am not doing a prediction for the next future state k+1).</p>
  <p>What is the proper way to obtain the predicted coordinates of the ball in the following frame (future state k+1) using a Kalman Filter?</p>
  <p><strong>Code used:</strong></p>
  <p>Initialization of Kalman filter:</p>
  <pre><code>kf = cv2.KalmanFilter(4, 2) #position x,y and velocity x,y
  kf.measurementMatrix = np.array([[1, 0, 0, 0], [0, 1, 0, 0]], np.float32)
  kf.transitionMatrix = np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32)
  kf.processNoiseCov =1*np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], np.float32)
  kf.measurementNoiseCov = 1*np.array([[1, 0], [0, 1]], np.float32)
  </code></pre>
  <p>First approach:</p>
  <pre><code>def Estimate(kf,x,y):
  
       predicted = kf.predict()
       measured = np.array([[np.float32(coordX)], [np.float32(coordY)]])
       estimate=kf.correct(measured)
       predicted = kf.predict()
  
       return predicted    
  </code></pre>
  <p>Second approach:</p>
  <pre><code>def Estimate(kf,x,y):
  
       measured = np.array([[np.float32(coordX)], [np.float32(coordY)]])
       estimate=kf.correct(measured)
       predicted = kf.predict()
  
       return predicted    
  </code></pre>
  <p>Note: the function Estimate is called inside a while loop every time a new pair of coordinates is obtained.</p>
  <p>Edit: in these links you can see the results of the first and second approaches, respectively:
  <a href="https://www.youtube.com/watch?v=PyRhmvwN5tA" rel="nofollow noreferrer">First approach</a>
  <a href="https://www.youtube.com/watch?v=qbUTjeFlcgU" rel="nofollow noreferrer">Second approach</a></p>