mohikhsan
mohikhsan

Reputation: 1538

OpenCV Kalman Filter Initialization Error

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.

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

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));
}

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?

Upvotes: 1

Views: 2166

Answers (1)

berak
berak

Reputation: 39796

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
    ...

Upvotes: 1

Related Questions