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
pts=np.array([np.float32(x1),np.float32(y1),np.float32(v),np.float32(a)], 
dtype=np.float64)

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

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.

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.

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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