Ask Your Question
0

How to improve Kalman Filter performance?

asked 2015-10-14 08:53:35 -0500

lfcooh gravatar image

I want to detect and track player on video but when a detected object lost, it get a new tracking ID. I want to get old tracking id with old object. How can I improve it? This code owner tell me " You should config Kalman Filter " . I'm very confuse about it.

public class Kalman extends KalmanFilter {
private KalmanFilter kalman;
private Point LastResult;
private double deltatime;

public void init() {

}

public Kalman(Point pt){
    kalman = new KalmanFilter(4, 2, 0, CvType.CV_32F);

    Mat transitionMatrix = new Mat(4, 4, CvType.CV_32F, new Scalar(0));
    float[] tM = { 
            1, 0, 1, 0, 
            0, 1, 0, 1,
            0, 0, 1, 0,
            0, 0, 0, 1 } ;
    transitionMatrix.put(0,0,tM);

    kalman.set_transitionMatrix(transitionMatrix);

    LastResult = pt;
    Mat statePre = new Mat(4, 1, CvType.CV_32F, new Scalar(0)); // Toa do (x,y), van toc (0,0)
    statePre.put(0, 0, pt.x);
    statePre.put(1, 0, pt.y);
    kalman.set_statePre(statePre);

    kalman.set_measurementMatrix(Mat.eye(2,4, CvType.CV_32F));

    Mat processNoiseCov = Mat.eye(4, 4, CvType.CV_32F);
    processNoiseCov = processNoiseCov.mul(processNoiseCov, 1e-4); //1e-4
    kalman.set_processNoiseCov(processNoiseCov);

    Mat id1 = Mat.eye(2,2, CvType.CV_32F);
    id1 = id1.mul(id1,1e-1);   
    kalman.set_measurementNoiseCov(id1);

    Mat id2 = Mat.eye(4,4, CvType.CV_32F);
    //id2 = id2.mul(id2,0.1);
    kalman.set_errorCovPost(id2);
}

public Kalman(Point pt, double dt, double Accel_noise_mag) {
    kalman = new KalmanFilter(4, 2, 0, CvType.CV_32F);
    deltatime = dt;

    Mat transitionMatrix = new Mat(4, 4, CvType.CV_32F, new Scalar(0));
    float[] tM = { 
            1, 0, 1, 0, 
            0, 1, 0, 1,
            0, 0, 1, 0,
            0, 0, 0, 1 } ;
    transitionMatrix.put(0,0,tM);

    kalman.set_transitionMatrix(transitionMatrix);

    // init
    LastResult = pt;
    Mat statePre = new Mat(4, 1, CvType.CV_32F, new Scalar(0)); // Toa do (x,y), van toc (0,0)
    statePre.put(0, 0, pt.x);
    statePre.put(1, 0, pt.y);
    statePre.put(2, 0, 0);
    statePre.put(3, 0, 0);
    kalman.set_statePre(statePre);

    Mat statePost = new Mat(4, 1, CvType.CV_32F, new Scalar(0));
    statePost.put(0, 0, pt.x);
    statePost.put(1, 0, pt.y);
    statePost.put(2, 0, 0);
    statePost.put(3, 0, 0);
    kalman.set_statePost(statePost);

    kalman.set_measurementMatrix(Mat.eye(2,4, CvType.CV_32F));

    //Mat processNoiseCov = Mat.eye(4, 4, CvType.CV_32F);
    Mat processNoiseCov = new Mat(4, 4, CvType.CV_32F, new Scalar(0));
    float[] dTime = { (float) (Math.pow(deltatime, 4.0) / 4.0), 0,
            (float) (Math.pow(deltatime, 3.0) / 2.0), 0, 0,
            (float) (Math.pow(deltatime, 4.0) / 4.0), 0,
            (float) (Math.pow(deltatime, 3.0) / 2.0),
            (float) (Math.pow(deltatime, 3.0) / 2.0), 0,
            (float) Math.pow(deltatime, 2.0), 0, 0,
            (float) (Math.pow(deltatime, 3.0) / 2.0), 0,
            (float) Math.pow(deltatime, 2.0) };
    processNoiseCov.put(0, 0, dTime);

    processNoiseCov = processNoiseCov.mul(processNoiseCov, Accel_noise_mag); // Accel_noise_mag = 0.5
    kalman.set_processNoiseCov(processNoiseCov);

    Mat id1 = Mat.eye(2,2, CvType.CV_32F);
    id1 = id1.mul(id1,1e-1);
    kalman.set_measurementNoiseCov(id1);

    Mat id2 = Mat.eye(4,4, CvType.CV_32F);
    id2 = id2.mul(id2,.1);
    kalman.set_errorCovPost(id2);
}

public Point getPrediction() {
    Mat prediction ...
(more)
edit retag flag offensive close merge delete

Comments

Do you have an example of each case? Please tell me. Thank a lot

lfcooh gravatar imagelfcooh ( 2015-10-15 06:29:30 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-10-14 10:47:17 -0500

Eduardo gravatar image

updated 2015-10-14 10:48:29 -0500

There are basically two cases:

  • the object is detected, you have to perform the prediction and correction step with the new measurements (the current detected position)
  • the object is not detected, you can predict the position of the object using the Kalman Filter (of course you cannot predict the position for a very long time and you have to set a thresold where you decide that the object is definitely lost).
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2015-10-14 08:53:35 -0500

Seen: 480 times

Last updated: Oct 14 '15