使用OpenCV和C ++的卡尔曼滤波器

时间:2016-09-28 13:56:28

标签: c++ opencv kalman-filter

初​​始化:

    p->kalman_filter = new cv::KalmanFilter(state_dim, measurement_dim, 0);
    p->kalman_filter->transitionMatrix = *(cv::Mat_<float>(state_dim, state_dim)
                                                  << 1,0,1,0, 0,1,0,1,
                                                  0,0,TIME_DIFFERENCE,0,
                                                  0,0,0,TIME_DIFFERENCE);
    setIdentity(p->kalman_filter->measurementMatrix);
    setIdentity(p->kalman_filter->processNoiseCov, cv::Scalar::all(1e-4));
    setIdentity(p->kalman_filter->measurementNoiseCov, cv::Scalar::all(1e-1));
    setIdentity(p->kalman_filter->errorCovPost, cv::Scalar::all(.1));

TIME_DIFFERENCE是不变的。

    cv::Mat new_state;
    track t = p->tracks.at(track_id);

    cv::transpose((cv::Mat)t.estimated_state, p->kalman_filter->statePost);
    t.estimated_error_covariance.copyTo(p->kalman_filter->errorCovPost);

    new_state = p->kalman_filter->predict();

代码在预测时崩溃,错误源自predict()函数中的statePre = transitionMatrix*statePost;。该错误是由于类型断言失败引起的。我正在使用转置,因为t.estimated_state是statePost应设置的转置。我尝试使用convertTo()来改变t.estimated_state的类型。对于statePost也没有使用setTo()也没有工作

有人可以指出我哪里出错吗?

1 个答案:

答案 0 :(得分:0)

我能够解决我的问题。我应该将卡尔曼滤波器的类型设置为CV_64FC1,因为我提供的所有值都是double类型。我还将mat初始化transitionMatrix更改为double类型。 这是我的最终代码

p->kalman_filter = new cv::KalmanFilter(state_dim, measurement_dim, 0, CV_64FC1);
p->kalman_filter->transitionMatrix = *(cv::Mat_<double>(state_dim, state_dim)
                                                  << 1,0,1,0, 0,1,0,1,
                                                  0,0,TIME_DIFFERENCE,0,
                                                  0,0,0,TIME_DIFFERENCE);