opencv卡尔曼滤波多个对象跟踪错误

时间:2016-01-28 17:59:24

标签: c++ image opencv kalman-filter

我一直在尝试使用卡尔曼滤波器进行多目标跟踪。这是我的代码,

for (int i =0; i<vGlobal.size(); i++) // Vector of objects of interest
    {
        cv::Point pTemp = cv::Point(vGlobal[i].iX, vGlobal[i].iY);
        cv::KalmanFilter kTempKF(4,2,0);
        kTempKF.statePre.at<floatt>(0) = pTemp.x;
        kTempKF.statePre.at<float>(1) = pTemp.y;
        kTempKF.statePre.at<float>(2) = 0;
        kTempKF.statePre.at<float>(3) = 0;

        kTempKF.transitionMatrix = *(cv::Mat_<float>(4,4)<< 1,0,1,0,  0,1,0,1,  0,0,1,0,  0,0,0,1);
        cv::setIdentity(kTempKF.measurementMatrix);
        cv::setIdentity(kTempKF.processNoiseCov, cv::Scalar::all(1e-4));            
        cv::setIdentity(kTempKF.measurementNoiseCov, cv::Scalar::all(10));
        cv::setIdentity(kTempKF.errorCovPost, cv::Scalar::all(.1));
        vKalmanFilters.push_back(kTempKF);  
    }

我正在使用卡尔曼滤波器矢量来跟踪我的每个物体。我已经完成了上面的过滤器初始化。现在我尝试按照下面的代码进行预测和测量。

 for (int i=0; i<vKalmanFilters.size();i++)
    {
        cv::Mat mPrediction = vKalmanFilters[i].predict(); 

        cv::Point pPredict(mPrediction.at<float>(0), mPrediction.at<float>(1));
        mMeasurement(0) = vGlobal[i].iX;
        mMeasurement(1) = vGlobal[i].iY;


        cv::Mat mEstimated;


            mEstimated = vKalmanFilters[i].correct(mPrediction); // Run time Error occurs here


     }

当我尝试运行此程序时,我在正确的(预测)

中收到运行时错误
  OpenCV Error: Assertion failed (C.type() == type && (((flags&GEMM_3_T) == 0 && C.rows == d_size.height && C.cols == d_size.width) || ((flags&GEMM_3_T) != 0 && C.rows == d_size.width && C.cols == d_size.height))) in gemm, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/matmul.cpp, line 741
 terminate called after throwing an instance of 'cv::Exception'
 what():  /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/matmul.cpp:741: error: (-215) C.type() == type && (((flags&GEMM_3_T) == 0 && C.rows == d_size.height && C.cols == d_size.width) || ((flags&GEMM_3_T) != 0 && C.rows == d_size.width && C.cols == d_size.height)) in function gemm

我仍然是卡尔曼过滤器的初学者。错误发生在预测点。我的做法完全错了吗?请有人解释我哪里出错了。

1 个答案:

答案 0 :(得分:2)

您应该在correction step中使用mMeasurement(2x1矩阵)代替mPrediction(4x1矩阵):

mEstimated = vKalmanFilters[i].correct(mMeasurement);

鉴于你做到了:

cv::KalmanFilter kTempKF(4,2,0);
// 4 dynamic params <-- your state, e.g. [x y dx dy]
// 2 measurements params <-- your mMeasurement [x y]