WurmD
WurmD

Reputation: 1483

Changing the Gain in OpenCV's Kalman Filter to make it more responsive

For a tracking a bounding box position and velocity in the image, where the measurements are not very noisy, but the bounding box moves very fast

state_size = 6; // x,y,w,h,vx,vy of BB
meas_size = 4; // x,y,w,h of BB
contr_size = 0;
int type = CV_32F;

initializing transition, process noise, and measurement noise with

cv::KalmanFilter kf;
kf.init(state_size, meas_size, contr_size, type);

setIdentity(kf.transitionMatrix);
kf.transitionMatrix.at<float>(2) = params.dt;
kf.transitionMatrix.at<float>(9) = params.dt;

kf.processNoiseCov.at<float>(0) = params.proc_noise_cov_p0;
kf.processNoiseCov.at<float>(7) = params.proc_noise_cov_p0;
kf.processNoiseCov.at<float>(14) = params.proc_noise_cov_p1;
kf.processNoiseCov.at<float>(21) = params.proc_noise_cov_p1;
kf.processNoiseCov.at<float>(28) = params.proc_noise_cov_p0;
kf.processNoiseCov.at<float>(35) = params.proc_noise_cov_p0;

setIdentity(kf.measurementNoiseCov, cv::Scalar(params.meas_noise_cov_p0));

1. How to make the filter more responsive?

2. What does the initial values for the transition matrix influence?

For making the filter more responsive OpenCV's Kalman filter seems to have a gain matrix https://docs.opencv.org/trunk/dd/d6a/classcv_1_1KalmanFilter.html#a077d73eb075b00779dc009a9057c27c3 (Kalman gain matrix (K(k)): K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R))

but it's not clear if/how we should increase it directly. It seems to not be influenced by the process noise covariance matrix (Q), but according to https://stackoverflow.com/a/3746110/1734357 processNoiseCov is exactly what I should increase to make it more responsive.

Upvotes: 1

Views: 1143

Answers (1)

Mehno
Mehno

Reputation: 908

The Kalman Filter is a linear filter that is optimal for linear systems with gaussian noise. Assume there is no noise and your system is perfectly linear. The transition matrix hence describes how the state changes and is not updated by the kalman filter. If the transition matrix is just the identity it means the state never changes. The transition matrix for your case should be an identity with an entry of dt in (0,4) and (1,5) (since given the last state of your rectangle the next state is the current position (x,y) + dt *(vx,vy). It depends on the application whether you have more information about the change in your state (what happen with your width and height).

The reality is influenced by noise. There are two different kinds considered in the kalman filter. Structural /process noise and measurement noise. Both influence the gain matrix. The gain matrix is not given by the user but calculated through the history, the measurement covariance and the process covariance. If the measurement is not noisy you might decrease the measurement covariance (it might be worthwhile to check at least in which range your variances are), By "increasing" the measurement matrix on decreases the influence of the observation in the current estimation. The other part is the process noise covariance. This part influences the P'(k). This is the covariance of the current state given the last state (and its covariance). If you know perfectly how your system behaves (which means you know your transition matrix), you can assume the process noise as zero. If you increase the process noise covariance you are saying, that there is an error in the propagation between two time stamps, that is not explained by the transition matrix and is random. (Maybe you know how much the width or height change on average. This variance can be used in the diagonal element, that corresponds to w and h (2 and 3)). So to make the filter more responsive you have to tell him, that you are unsure about the actual change and hence you have to increase the process noise. For parameter tuning i suggest you have a closer look at your data. So you have to increase the process noise AND decrease the measurement noise to make the filter more responsive.

Upvotes: 3

Related Questions