具有卡尔曼滤波器的物体轨道

时间:2014-01-23 10:02:23

标签: android opencv image-processing kalman-filter

我想实现卡尔曼滤波器来跟踪一个点,这是我的代码

    public Mat onCameraFrame(CvCameraViewFrame inputFrame) {        
    mRgba = inputFrame.rgba();
    mGray = inputFrame.gray();      

    switch (viewMode) {

    case VIEW_MODE_ColorDetect:


            /**
             * 
             * some code for color detect 
             * 
             */

            int ObjectCenterX = (int)((mboundingRect.tl().x + mboundingRect.br().x) / 2);
            int ObjectCenterY = (int)((mboundingRect.tl().y + mboundingRect.br().y) / 2);

            //get the object center point , and I whant to track it by kalman filter.
            Core.circle(mRgba, new Point(ObjectCenterX, ObjectCenterY), 5, ColorGreen, 2);


            /***********************************************Kalman************************************************************/
            try {                   
                Mat ObjectPoint = new Mat(1, 2, CvType.CV_32F);
                int[] PreObjectCenter = {ObjectCenterX, ObjectCenterY};
                ObjectPoint.put(0, 0, PreObjectCenter[0]);
                ObjectPoint.put(0, 1, PreObjectCenter[1]);
                ObjectPoint = myKalmanFilter(ObjectPoint);

                /**Question 2: How to get corrected point from Mat?**/

            } catch (Exception e) {
                Log.e("In camera fram: ", e.toString());
            }
            /*****************************************************************************************************************/

        }
     return mRgba;



return mRgba;
}

private Mat myKalmanFilter(Mat objectPoint){
    KalmanFilter KF = new KalmanFilter();
    try {
        KF.predict(objectPoint);

        KF.correct(objectPoint);
    } catch (Exception e) {
        Log.e("myKalmanFilter: ", e.toString());
    }

    return objectPoint;
}

我有两个问题,Q1。我为此代码收到以下错误:

  

17:44:22.727:E / cv :: error()(24902):OpenCV错误:断言失败(类型== B.type()&&(type == CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2))in void cv :: gemm(cv :: InputArray,cv :: InputArray,double,cv :: InputArray,double,cv :: OutputArray,int),file / home / reports / ci / slave_desktop / 50-SDK / opencv / modules / core / src / matmul.cpp,第711行   17:44:22.727:E / org.opencv.video(24902):video :: predict_10()抓住了cv ::异常:/ home / reports / ci / slave_desktop / 50-SDK / opencv / modules / core / src / matmul.cpp:711:错误:(-215)type == B.type()&& (函数== CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)函数void cv :: gemm(cv :: InputArray,cv :: InputArray,double,cv :: InputArray,double ,cv :: OutputArray,int)   17:44:22.727:E / myKalmanFilter:(24902):CvException [org.opencv.core.CvException:cv :: Exception:/ home / reports / ci / slave_desktop / 50-SDK / opencv / modules / core / src / matmul.cpp:711:错误:(-215)type == B.type()&& (函数== CV_32FC1 || type == CV_64FC1 || type == CV_32FC2 || type == CV_64FC2)函数void cv :: gemm(cv :: InputArray,cv :: InputArray,double,cv :: InputArray,double ,cv :: OutputArray,int)   17:44:22.727:E / myKalmanFilter:(24902):]

Q2。修改它可以工作的代码后,如何从Mat获得x和y点?

修改 我仍然不知道如何设置KalmanFilter维度,我设置如private KalmanFilter KF = new KalmanFilter(1, 2, 16, CvType.CV_32F);和eclipse show

  

线程[< 1> main](暂停(例外UnsatisfiedLinkError))
      KalmanFilter。(int,int,int,int)行:72
      Tutorial1Activity。()行:65       Class.newInstanceImpl()行:不可用[native method]       Class.newInstance()行:1319
      Instrumentation.newActivity(ClassLoader,String,Intent)行:1069       ActivityThread.performLaunchActivity(ActivityThread $ ActivityClientRecord,Intent)行:2258
      ActivityThread.handleLaunchActivity(ActivityThread $ ActivityClientRecord,Intent)行:2387       ActivityThread.access $ 600(ActivityThread,ActivityThread $ ActivityClientRecord,Intent)line:151
      ActivityThread $ H.handleMessage(消息)行:1331
      ActivityThread $ H(Handler).dispatchMessage(Message)行:99       Looper.loop()行:155       ActivityThread.main(String [])行:5485
      Method.invokeNative(Object,Object [],Class,Class [],Class,int,boolean)line:not available [native method]
      Method.invoke(Object,Object ...)行:511
      ZygoteInit $ MethodAndArgsCaller.run()行:1028       ZygoteInit.main(String [])行:795       NativeStart.main(String [])行:不可用[本机方法]

2 个答案:

答案 0 :(得分:2)

  • 请不要为每个预测/修正创建一个新的KalmanFilter,它必须在整个程序的持续时间内保持状态。

  • 您需要为测量大小和dynparamsize创建具有正确尺寸的KalmanFilter,调用空构造函数将产生上述错误。

  • KF.predict(objectPoint)将覆盖objectPoint中的内容,因此您需要使用预测值进行更正,而不是使用测量值进行更正。或者在那里使用2个不同的Mats,或者反转预测/正确的顺序

  • 不幸的是,opencv java包装器不允许你为卡尔曼滤波器设置statePre或转换Mats。它的当前界面有点无法使用。你要么需要jni来设置它们,要么使用javacv绑定(破旧过时,但至少要更好地处理这部分)

答案 1 :(得分:-1)

很好的例子

卡尔曼滤波器实现(跟踪鼠标位置)

http://opencvexamples.blogspot.com/2014/01/kalman-filter-implementation-tracking.html