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).
The thing is that I don't know if I should:
- Predict (state k), Correct (state k), and then Predict again (state k+1).
- Correct (state k), and then Predict (state k+1).
- Predict (state k), Correct (state k).
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).
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?
Code used:
Initialization of Kalman filter:
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)
First approach:
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
Second approach:
def Estimate(kf,x,y):
measured = np.array([[np.float32(coordX)], [np.float32(coordY)]])
estimate=kf.correct(measured)
predicted = kf.predict()
return predicted
Note: the function Estimate is called inside a while loop every time a new pair of coordinates is obtained.
Edit: in these links you can see the results of the first and second approaches, respectively: First approach Second approach