I'm trying to implement a multi target tracking with Kalman filter. Each object has an instance of Kalman Filter. The true position of the objects $(x,Y)$ are the corrected state out of the KF after the prediction and correction steps. Till now everything is clear.

For associating observations to objects I'm using till now euclidean distance. But I've read here that a more raffinate distance could be used: the mahalanobis distance, that take in account the shape of the association gate. This shape, due to the fact the KF deals with gaussian pdf is an ellipsoid, there the mean is the center and the shape is the covariance.

Now, I'm stucked on where to get this two parameters. I think that for the mean I can take the predicted state out of KF. But what about the covariance matrix?

In particular I'm using the OpenCV implementation of Kalman Filter, and, reading in the source, I have those two matrixes that are updated each prediction step:

Mat errorCovPre;        //!< priori error estimate covariance matrix (P'(k)): P'(k)=A*P(k-1)*At + Q)*/
Mat errorCovPost;       //!< posteriori error estimate covariance matrix (P(k)): P(k)=(I-K(k)*H)*P'(k)

I'm not really into the probabilistic derivation of the Kalman filter so.. what is the right one?


It is actually neither of these covariance matrices. The covariance "S" in the Mahalanobis distance is the covariance of the residual vector (difference between the predicted Kalman measurement "z_hat" and the measurement "z" you are attempting to associate). Thus, S = errorCovPre + R where R is the measurement covariance matrix, which is often specified as a diagonal matrix containing the variance of each measurement dimension contained in the measurement vector "z". It is also important to remember that z_hat = H * x_predicted where x_predicted is the predicted state of the Kalman filter and "H" is the observation matrix that maps the state space into the measurement space.

  • $\begingroup$ ok.. so, R is what, in the opencv kalman implementation, is defined measurementNoiseCov.. Thanks! where can I find some sources about that? $\endgroup$ – nkint Feb 12 '14 at 12:15
  • $\begingroup$ yes, R is the measurement noise covariance. If your measurements are [x,y] positions (or able to be converted to [x,y] positions), then the R covariance matrix will look like [varX,0;0,varY]. That is, it is a diagonal matrix with the diagonal elements equal to the measurement error variance of X and Y. For more information, look at slides 4.12-4.16 here (dropbox.com/s/p6ubw3j13ufr9u2/…) and slides 5.8-5.11 here (dropbox.com/s/pps3z98v86llcqt/…). $\endgroup$ – concipiotech Feb 13 '14 at 1:53
  • $\begingroup$ ok, very cool! what about if I want to draw it? I have a function like void ellipse(Point center, Size axes, double angle) and I want to draw the ellipsoidal gate centred in the mean (the predicted state) and as a shape an ellipsoid given from the S matrix? Do I need also a threshold parameter, right? How can I get the axes length and the angle to describe the ellipsoid? $\endgroup$ – nkint Feb 13 '14 at 22:23
  • $\begingroup$ See answer below. My response was too long for a comment. $\endgroup$ – concipiotech Feb 15 '14 at 22:05

To draw the ellipsoid, the first thing you need to do is solve the eigensystem for the covariance matrix "S". This will result in two eigenvalues lambda_1 and lambda_2 as well as the corresponding eigenvectors v_1 and v_2. v_1 and v_2 will give you the axes of the ellipsoid. To find the angle or rotation, you would take the eigenvector corresponding to the largest eigenvalue and compute its projection (using the dot product) onto the axis from which you are expressing the angle. So, for example, if you find that the eigenvector corresponding to the largest eigenvalue is (x,y) = [1,2] and your angle is expressed relative to the x-axis, then your angle would be arcos([1,2] . [1,0]/sqrt(5)) = 63.43 degrees. Now that you have the angle of orientation, you need the length of the axes. The length of the axes is given by the square root of each eigenvalue (remember which eigenvalue goes with which axis) multiplied by a scaling factor (SF). The SF is based upon the dimensionality of the ellipsoid "N" and the probability region "p" that you want to depict. In general, the scale factor SF = sqrt(2 InverseGammaRegularized[N/2,0,p]). For N=2, this reduces to SF = sqrt(-2 ln(1-p)). Of course, the center of the ellipsoid is equal to the predicted state (mean).

  • $\begingroup$ This seems to be continuation of the other answer rather than a stand-alone separate answer, so I think the answers should be merged. $\endgroup$ – Juho Kokkala Jun 28 '17 at 18:14

Your Answer

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

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