I am working on a tracking application where I use the kalman filter to validate my current measurement of the position. I use the code from this question:
How to find the probability of Kalman filter states? [OpenCV+Python]
At first I calculate velocity (v) and accelearation (a) of my moving object at (x, y). These 4 values are used as my kalman state. I initiate the kalman filter as follows:
(np.eye(n,m) generates the identity matrix with dimensions nxm):

def initKalman(init_state fps):
    kalman = cv.KalmanFilter(4, 4, 2)
    kalman.transitionMatrix = np.array([[1., 0., 1/fps, 0.],
                                        [0., 1., 0., 1/fps],
                                        [0., 0., 1., 0.],
                                        [0, 0., 0., 1.]])

    kalman.measurementMatrix = 1. * np.eye(2, 4)
    kalman.measurementNoiseCov = 1e-3 * np.eye(2, 2)
    kalman.processNoiseCov = 1e-5 * np.eye(4, 4) 
    kalman.errorCovPost = 1e-1 * np.eye(4, 4)
    kalman.statePost = init_state.reshape(4, 1)

    return kalman

kinematics = np.array((velocity, acceleration), dtype=np.float32)
kalman_state = np.concatenate((point, kinematics))
kalman_filter = initKalman(kalman_state, fps = 15)

During operation the correction is done as follows:

def correct_kalman(kalman, state):
    measurement = (np.dot(kalman.measurementNoiseCov, np.random.randn(2, 1))).reshape(-1)
    measurement = np.dot(kalman.measurementMatrix, state) + measurement
    return kalman.correct(measurement)

kinematics = np.array((velocity, acceleration), dtype=np.float32)
kalman_state = np.concatenate((point, kinematics))
correct_kalman(kalman_filter, kalman_state)

It seems to work witch is great, but im trying to understand why. In my understanding it shouldn't work because in correct_kalman() the velocity and acceleration are ommited in this code line: measurement = np.dot(kalman.measurementMatrix, state) + measurement because the measurementmatrix is just 2 x 4. (In fact when i set acceleration and velocity to 0 the filter behaves exactly the same.)
For Example take the kalman_state = np.array([10., 20., 25., 75.]) and calculate the dot product with the measurementMatrix = 1. * np.eye(2, 4)
then measurement = np.dot(kalman.measurementMatrix, kalman_state) is just

>>> measurement
array([10., 20.])

v and a are gone.
So I changed my measurementMatrix and my measurementNoiseCov to 4 x 4 dimensionality and adjusted my correction acordingly by using np.random.randn(4, 1) but now the kalman filter is way to sluggish and falls behind the measurement.

Why is the first approach working if v and a are not used?
How can I change the measurement matrix in a more targeted way than just iteratively adjusting the values?

Thanks for the help!

  • $\begingroup$ Maybe I have to calculate the x and y velocities and acclearations independently? $\endgroup$ – David Salb Feb 13 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.