OpenCV卡尔曼滤波器初始化错误

问题描述:

我想跟踪图像中的30个点,并希望使用OpenCV库中的卡尔曼滤波器来稳定跟踪.我以前对单个点进行过此操作,然后成功地将点的位置和速度用作状态.然后,对于30个点,我只是决定创建30个卡尔曼滤波器,每个点一个,并将其放入一个数组中.但是,我得到一个断言错误.

I want to track 30 points in an image and want to stabilize the tracking using the Kalman Filter in the OpenCV library. I did this before for a single point and succeeded using the position and velocity of the point as states. Then, for 30 points I just decided to create 30 Kalman Filters, one for each point and put them in an array. However, I got an assertion error.

这是跟踪图像中那30个点的正确/最佳方法吗?有更好的方法可以做到这一点吗?

Is this the right/best way to track those 30 points in the image? Are there better ways to do this?

我的代码如下.问题发生在StatePre行中.

My code is below. The problem occurs in the StatePre line.

vector<KalmanFilter> ijvEdgeKF(30);

for(int i = 0; i < 30; i++){
    Point temp = calcEndPoint(ijv,170,i*360/30); //Calculates initial point
    ijvEdgeKF[i].statePre.at<float>(0) = temp.x; //State x
    ijvEdgeKF[i].statePre.at<float>(1) = temp.y; //State y
    ijvEdgeKF[i].statePre.at<float>(2) = 0; //State Vx
    ijvEdgeKF[i].statePre.at<float>(3) = 0; //State Vy

    ijvEdgeKF[i].transitionMatrix = *(Mat_<float>(4, 4) << 1,0,0,0,   0,1,0,0,  0,0,1,0,  0,0,0,1);
    setIdentity(ijvEdgeKF[i].measurementMatrix);
    setIdentity(ijvEdgeKF[i].processNoiseCov, Scalar::all(1e-4));
    setIdentity(ijvEdgeKF[i].measurementNoiseCov, Scalar::all(1e-1));
    setIdentity(ijvEdgeKF[i].errorCovPost, Scalar::all(.1));
}

已解决.问题出在KalmanFilter初始化中.我没有在数组中初始化过滤器,所以这里是解决方法:

Solved. Problem was in the KalmanFilter initialization. I did not initialize the filter in the arrays so here is the solution:

vector<KalmanFilter> ijvEdgeKF;
ijvEdgeKF.clear();

for(int i = 0; i < 30; i++){
    Point temp = calcEndPoint(ijv,170,i*360/30); //Calculates initial point
    KalmanFilter tempKF(4,2,0);
    tempKF.statePre.at<float>(0) = temp.x; //State x
    tempKF.statePre.at<float>(1) = temp.y; //State y
    tempKF.statePre.at<float>(2) = 0; //State Vx
    tempKF.statePre.at<float>(3) = 0; //State Vy

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

尽管仍然有一个问题,这是跟踪图像中多个点的唯一方法还是有更好的方法?

Still have one question though, is this the only way to track the multiple points in an image or is there a better way?

您没有正确初始化KalmanFilters,因此statePre Mat为空.

you're not initializing your KalmanFilters correctly, thus the statePre Mat is empty.

for(int i = 0; i < 30; i++){
    ijvEdgeKF[i].init(4,4);  // int dynamParams, int measureParams
    ijvEdgeKF[i].statePre.at<float>(0) = 3; //State x
    ...