title: OpenCV Kalman filter
body: <p>I have three gyroscope values, pitch, roll and yaw. I would like to add Kalman filter to get more accurate values. I found the opencv library, which implements a Kalman filter, but I can't understand it how is it really work.</p>
  
  <p>Could you give me any help which can help me? I didn't find any related topics on the internet.</p>
  
  <p>I tried to make it work for one axis.</p>
  
  <pre><code>const float A[] = { 1, 1, 0, 1 };
  CvKalman* kalman;
  CvMat* state = NULL;
  CvMat* measurement;
  
  void kalman_filter(float FoE_x, float prev_x)
  {
      const CvMat* prediction = cvKalmanPredict( kalman, 0 );
      printf("KALMAN: %f %f %f\n" , prev_x, prediction-&gt;data.fl[0] , prediction-&gt;data.fl[1] );
      measurement-&gt;data.fl[0] = FoE_x;
      cvKalmanCorrect( kalman, measurement);
  }
  </code></pre>
  
  <p>in main</p>
  
  <pre><code>kalman = cvCreateKalman( 2, 1, 0 );
  state = cvCreateMat( 2, 1, CV_32FC1 );
  measurement = cvCreateMat( 1, 1, CV_32FC1 );
  cvSetIdentity( kalman-&gt;measurement_matrix,cvRealScalar(1) );
  memcpy( kalman-&gt;transition_matrix-&gt;data.fl, A, sizeof(A));
  cvSetIdentity( kalman-&gt;process_noise_cov, cvRealScalar(2.0) );
  cvSetIdentity(kalman-&gt;measurement_noise_cov, cvRealScalar(3.0));
  cvSetIdentity( kalman-&gt;error_cov_post, cvRealScalar(1222));
  kalman-&gt;state_post-&gt;data.fl[0] = 0;
  </code></pre>
  
  <p>And I call this everytime, when I receive data from gyro:</p>
  
  <pre><code>kalman_filter(prevr, mpe-&gt;getGyrosDegrees().roll);
  </code></pre>
  
  <p>I thought in kalman_filter the first parameter is the previous value and the second is the currect value. I'm not and this code doesn't work... I know I have a lot of work with it, but I don't know how to continue, what to change...</p>