Assertion Error in Kalman Filter Java OpenCV 3.0 [closed]

asked 2015-03-20 03:56:16 -0600

Francisco gravatar image

updated 2015-03-21 11:43:41 -0600

Hi everyone! I have a problem with OpenCV3.0.
I used KalmanFilter in OpenCV3.0, when i run my program,


Error (fixed):

OpenCV Error: Assertion failed (type== B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2))
in cv::gemm, file  ..\..\..\..\opencv\modules\core\src\matmul.cpp, line 882
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\core\src\matmul.cpp:882: error: (-215) type == B.type() && (type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2) in function cv::gemm ]

New bug in line

        Mat prediction = kalman.predict();

OpenCV Error: Assertion failed (a_size.width == len) in cv::gemm, file ..\..\..\..\opencv\modules\core\src\matmul.cpp, line 889 

Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\core\src\matmul.cpp:889: error: (-215) a_size.width == len in function cv::gemm

My class:

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

    public void init() {

    }

    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 = { new float[] { 1, 0, 1, 0 },
                new float[] { 0, 1, 0, 1 }, new float[] { 0, 0, 1, 0 },
                new float[] { 0, 0, 0, 1 } };

        for (int j = 0; j < 4; j++) {
            for (int k = 0; k < 4; k++) {
                transitionMatrix.put(j, k, tM[j][k]);
            }
        }

        kalman.set_transitionMatrix(transitionMatrix);
        measurement = new MatOfFloat(2, 1, CvType.CV_32F);

        LastResult = pt;
        Mat statePre = new MatOfFloat(4, 1); // 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 MatOfFloat(2, 1);
        statePost.put(0, 0, pt.x);
        statePost.put(1, 0, pt.y);
        kalman.set_statePost(statePost);

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

        for (int j = 0; j < 4; j++) {
            for (int k = 0; k < 4; k++) {
                processNoiseCov.put(j, k, dTime[j][k]);
            }
        }

        processNoiseCov.mul(processNoiseCov, Accel_noise_mag);
        kalman.set_processNoiseCov(processNoiseCov);

        kalman.set_measurementNoiseCov(new Mat(2, 2, CvType.CV_32F, new Scalar(
                0.1)));
        kalman.set_errorCovPost(new Mat(4, 4, CvType.CV_32F, new Scalar(0.1)));
    }

    public Point getPrediction() {
        Mat prediction = kalman.predict();
        LastResult = new Point(prediction.get(0, 0)[0], prediction.get(1, 0)[0]);
        return LastResult;
    }

    public Point update(Point p, boolean dataCorrect) {
        measurement = new MatOfFloat(2, 1, CvType.CV_32F);
        if (!dataCorrect) {
            measurement.put(0, 0, LastResult.x);
            measurement.put(1, 0, LastResult.y);
        } else {
            measurement.put(0 ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Francisco
close date 2015-03-24 10:03:30.205398

Comments

1

why is your TransitionMatrix CvType.CV_8UC1 ? shouldn't that be CvType.CV_32FC1 ?

berak gravatar imageberak ( 2015-03-20 04:04:00 -0600 )edit

Thanks, i fixed, but i have a new bug :(

OpenCV Error: Assertion failed (a_size.width == len) in cv::gemm, file ..\..\..\..\opencv\modules\core\src\matmul.cpp, line 889

Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\core\src\matmul.cpp:889: error: (-215) a_size.width == len in function cv::gemm
Francisco gravatar imageFrancisco ( 2015-03-20 06:41:05 -0600 )edit

some of your Mat's seem to have the wrong size / shape: * processNoiseCov and errorCovPost are 4x4 * measurementNoiseCov is 2x2 here.

also, to initialize, better use a flat 1d float array (also more readable):

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

-

berak gravatar imageberak ( 2015-03-20 07:24:29 -0600 )edit

Thank you! I checked and edited it. I read from KalmanIntro, where: n = q = 4, m = 2. However, it's still that bug.

Francisco gravatar imageFrancisco ( 2015-03-21 11:42:05 -0600 )edit