Ask Your Question
0

Use kalman in Java

asked 2014-11-07 06:56:26 -0600

updated 2014-11-07 07:02:54 -0600

berak gravatar image

Hi! I'm trying to use the Kalman filter in openCV in java but it I can't get it to work. I have made a simple example with some 2D-points moving.

Both the output from predict() and correct() and empty for this example.

    double[][] samplesArr = {{0,0},{1,0},{1,1},{2,1},{2,2}};

    Mat meas = new Mat(2,1,CvType.CV_32F);
    KalmanFilter kf = new KalmanFilter(4, 2);
    for(int i = 0; i < samplesArr.length; i++)
    {
        meas.put(0, 0, samplesArr[i]);
        Mat corr = kf.correct(meas);
        Mat pre = kf.predict();
        System.out.println(pre.dump());
    }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-11-07 07:45:03 -0600

berak gravatar image

updated 2014-11-07 08:01:33 -0600

if you are using opencv2.4, i got bad news: the kalmanfilter is not usable.

by default, the internal 'measurementMatrix' variable is set to all 0, your measurement input will be multiplied by that, - you see it's all dead from here on, so the prediction will be always 0, too.

if you are using opencv3.0, you can set it e.g. to Mat.eye(2,1,CvType.CV_32F);

double[][] samplesArr = {{0,0},{1,0},{1,1},{2,1},{2,2}};

Mat meas = new Mat(2,1,CvType.CV_32F);

KalmanFilter kf = new KalmanFilter(4, 2);

Mat mm = Mat.eye(2,4,CvType.CV_32F);
kf.set_measurementMatrix(mm);  // 3.0 only!
// you'll want to set other Mat's like errorCovPost and processNoiseCov, too,
// to change the 'adaption speed'

for(int i = 0; i < samplesArr.length; i++)
{
    meas.put(0, 0, samplesArr[i]);
    Mat corr = kf.correct(meas);
    Mat pre = kf.predict();
    System.out.println(pre.t().dump());
}

 [java] [0, 0, 0, 0]
 [java] [0.5, 0, 0, 0]
 [java] [0.80000001, 0.60000002, 0, 0]
 [java] [1.5384616, 0.84615386, 0, 0]
 [java] [1.8235294, 1.5588235, 0, 0]
edit flag offensive delete link more

Comments

Do you know something about BOWDescriptorExctactor in java?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2014-11-07 08:44:04 -0600 )edit
1

@thdrksdfthmn, off-topic, booo! ;)

(well, i failed at wrapping it for java (in a similar way than the python one) with 2.4 (another cv::Ptr problem there), but hopes are high, that it's doable in 3.0)

atm, there's no such thing.

berak gravatar imageberak ( 2014-11-07 09:04:29 -0600 )edit

Question Tools

Stats

Asked: 2014-11-07 06:56:26 -0600

Seen: 249 times

Last updated: Nov 07 '14