卡尔曼滤波器没有给出正确的结果

时间:2016-02-02 15:10:52

标签: c++ opencv image-processing kalman-filter

我是卡尔曼滤镜跟踪的初学者,我正在按照教程(http://opencvexamples.blogspot.com/2014/01/kalman-filter-implementation-tracking.html)来实现多个对象跟踪。我有一个结构对象,我在其中有一个kalman过滤器,如下所示。

 struct sAsparagus
    {
        int iId;
        int iFrameId;
        int iWidth;
        int iHeight;
        int iX;
        int iY;
        int iZ;
        cv::KalmanFilter KF;
    };

然后,我试图初始化从blob检测中获得的值如下。

 for (CvBlobs::const_iterator it = blobs.begin(); it !=blobs.end();++it)
    {
        sAsparagus sAsp;
        sAsp.iFrameId = iCounter;
        sAsp.iWidth = (it->second->maxx - it->second->minx);
        sAsp.iHeight = (it->second->maxy - it->second->miny);
        sAsp.iX = it->second->centroid.x;
        sAsp.iY = it->second->centroid.y;
        sAsp.KF = cv::KalmanFilter(4, 2, 0);
        sAsp.KF.transitionMatrix = *(cv::Mat_<float>(4,4)<<1,0,1,0,   0,1,0,1,   0,0,1,0,   0,0,0,1);
        sAsp.KF.statePre.at<float>(0) = sAsp.iX;
        sAsp.KF.statePre.at<float>(1) = sAsp.iY;
        sAsp.KF.statePre.at<float>(2) = 0;
        sAsp.KF.statePre.at<float>(3) = 0;
        setIdentity(sAsp.KF.measurementMatrix);
        setIdentity(sAsp.KF.processNoiseCov,  cv::Scalar::all(1e-2));
        setIdentity(sAsp.KF.measurementNoiseCov, cv::Scalar::all(10));
        setIdentity(sAsp.KF.errorCovPost, cv::Scalar::all(.1));
        vGlobal.push_back(sAsp);
    }

然后,我尝试使用预测和正确的函数如下。

 for (int i =0; i<vGlobal.size(); i++)
    {
        cv::Mat_<float> measurement(2,1); measurement.setTo(cv::Scalar(0));
        cv::Mat prediction = vGlobal[i].KF.predict();
        cv::Point pPredict(prediction.at<float>(0), prediction.at<float>(1));

        measurement(0) = vGlobal[i].iX;
        measurement(1) = vGlobal[i].iY;

        cv::Mat mEstimated = vGlobal[i].KF.correct(measurement);

        std::cout<<"Prediction values: "<<pPredict.x<<", "<<pPredict.y<<std::endl;
        cv::Point pEstimated(mEstimated.at<float>(0), mEstimated.at<float>(1));
        std::cout<<"Measurement values: "<<measurement(0)<<", "<<measurement(1)<<std::endl;
        std::cout<<"Estimated values: "<<pEstimated.x<<", "<<pEstimated.y<<std::endl;
 }

但我没有得到正确的结果。上述程序的样本输出是

  Prediction values: 0, 0
  Measurement values: 368, 511
  Estimated values: 7, 10

我认为这些结果不对。我需要一个类似于测量值的值。我哪里错了?

1 个答案:

答案 0 :(得分:1)

您应该设置statePost,而不是statePre

sAsp.KF.statePost.at<float>(0) = sAsp.iX;
sAsp.KF.statePost.at<float>(1) = sAsp.iY;
sAsp.KF.statePost.at<float>(2) = 0;
sAsp.KF.statePost.at<float>(3) = 0;

没有控制矩阵,predict()就是这样做的:

statePre = TransitionMatrix * statePost