I am working on a video object tracking problem. I am using Kalman filter to predict and correct the object position return by an algorithm such as CamShift. I want to adjust the likelihood probability of the states so as to customize the weight of the states before feeding to the Kalman filter. Currently this the code I have:

#Define kalman filter
kalman = cv2.KalmanFilter(4, 2, 2)
# initial position: (x0,y0)
state = np.array([col+w/2,row+h/2,0,0],dtype='float64')
kalman.transitionMatrix = np.array([[1.,0., .1, 0.],
                                [0., 1., 0., .1],
                                [0., 0., 1., 0.],
                                [0., 0., 0., 1.]])
kalman.measurementMatrix = 1. * np.eye(2, 4)
kalman.processNoiseCov = 1e-5 * np.eye(4, 4)
kalman.measurementNoiseCov = 1e-3 * np.eye(2, 2)
kalman.errorCovPost = 1e-1 * np.eye(4, 4)
kalman.statePost = state

Here, 'row' and 'col' are the top-left row and column indices, respectively of the current rectangular box containing the object position, and 'w' and 'h' are the width and height, respectively of the rectangular box containing the object.

Now, suppose (x1,y1) is the position returned by my algorithm (e.g. CamShift,Optical flow etc.).

Now I pass (x1,y1) and the explicitly calculated velocity (v) and acceleration(a) to Kalman filter as:

#Predict using kalman filter
prediction = kalman.predict()

# Update kalman state using optical flow results

measurement = (np.dot(kalman.measurementNoiseCov, np.random.randn(2, 
measurement = np.dot(kalman.measurementMatrix, state) + measurement
# use optical flow to correct kalman filter

It is all fine till here. But what if I want to give only less than 0.5 probability weightage to the previous state (=[x0,y0,0,0]) compared to the present state (=[x1,y1,v,a]) while computing 'prediction_new'. I have extensively searched the Kalman filter OpenCV library as well as online, but couldn't find an answer. Appreciate help on this. Thanks in advance.


1 Answer 1


The idea of Kalman is it fuses the data with the optimal weighing to get results.
Optimal requires definition and in the case of Kalman it means that if your model is right this is the optimal.

Since we operate Kalman filter under the assumption the model is right no Kalman Filter will allow you doing what you want.

Now, it seems that in your case what you don't like is the transient phase when there are low number of measurements.
What I can suggest is that for this start phase you will built your own filter based on your weighing and after few iterations send your result to the Kalman Filter as a starting point.

  • $\begingroup$ Thank you, Royi! Sorry for the long delay in responding. It worked. Thank you! $\endgroup$ Dec 26, 2019 at 14:27
  • $\begingroup$ I did. I also accepted your answer. $\endgroup$ Dec 26, 2019 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.