opencv kalman filter(python) problem when no detection

asked 2020-03-25 22:19:36 -0500

MingCheng gravatar image

updated 2020-03-26 21:18:21 -0500

supra56 gravatar image

Hi, when I am trying to use the opencv Kalman Filter to track a target. My initial setup is as below:

deltaTime = 1/30 #time approximately for a single frame
state = np.zeros((4, 1), np.float32)
measure = np.zeros((2, 1), np.float32)

kalman = cv2.KalmanFilter(4, 2, 0) #control vector = 0, assume linear motion.

kalman.measurementMatrix = np.array([[1,0,0,0],

kalman.transitionMatrix = np.array([[1,0,deltaTime,0],

kalman.processNoiseCov = np.array([[1,0,0,0],
                                   [0,0,0,1]],np.float32) * 0.03

I am able to track the target properly when the measurement/observation of the object is exist.However, when the object is lost in the frame, the kalman filter's prediction stays constant at the last known position of the target. Meaning, the prediction component of the kalman filter is not working in my case. The code for detection and no detection is shown below:

    if ini.detection==1: #target detected, this component is working correctly
        state = kalman.predict()

        #ini.detection==0,no detection
        kalman.statePost = state #pass the last known state to KF as the previous state
        noDetectstate = np.zeros((2, 1), np.float32)
        noDetectstate[0] = state[0]
        noDetectstate[1] = state[1]
        state = kalman.predict() # The problem is here, the value of the state did not change after the object is not being detected

Thank you for any help in advance

edit retag flag offensive close merge delete


Basically is just the problem of when there is no measurement input, the result of the kalman filter prediction will not change anymore. Is there any way to solve this problem. Really appreciated

MingCheng gravatar imageMingCheng ( 2020-03-26 04:42:02 -0500 )edit