Multi-target Tracking: calculate the association gate from Kalman filter 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?
 A: 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.
A: 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).
