今天研究了一下卡尔曼滤波跟踪,同时也看了一下卡尔曼滤波Opencv的源代码,总是看懂了。下面是opencv自带的一个程序,代码如下:
- // kalman.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include "opencv2/video/tracking.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include <stdio.h>
- using namespace cv;
- static inline Point calcPoint(Point2f center, double R, double angle)
- {
- return center + Point2f((float)cos(angle), (float)-sin(angle))*(float)R;
- }
- static void help()
- {
- printf( "\nExamle of c calls to OpenCV's Kalman filter.\n"
- " Tracking of rotating point.\n"
- " Rotation speed is constant.\n"
- " Both state and measurements vectors are 1D (a point angle),\n"
- " Measurement is the real point angle + gaussian noise.\n"
- " The real and the estimated points are connected with yellow line segment,\n"
- " the real and the measured points are connected with red line segment.\n"
- " (if Kalman filter works correctly,\n"
- " the yellow segment should be shorter than the red one).\n"
- "\n"
- " Pressing any key (except ESC) will reset the tracking with a different speed.\n"
- " Pressing ESC will stop the program.\n"
- );
- }
- int main(int, char**)
- {
- help();
- Mat img(500, 500, CV_8UC3);
- KalmanFilter KF(2, 1, 0);
- //[x1,x2]=[角度,角速度]
- /*
- 运动模型:x1(k+1) = x1(k) + x2(k)*T
- x2(k+1) = x2(k)
- 状态转移方程:
- x^ = AX + w
- 测量方程:
- z = Hx + v
- */
- //状态估计值x=[x1,x2] --> state
- Mat state(2, 1, CV_32F); /* (phi, delta_phi) */
- Mat processNoise(2, 1, CV_32F);
- //当前观测值Z=Hx + v ---> measurement
- Mat measurement = Mat::zeros(1, 1, CV_32F);
- char code = (char)-1;
- for(;;)
- {
- randn( state, Scalar::all(0), Scalar::all(0.1) );
- //transitionMatrix对应到状态转移方程中的矩阵A
- KF.transitionMatrix = *(Mat_<float>(2, 2) << 1, 1, 0, 1);
- //measurementMatrix对应到测量方程的矩阵H
- setIdentity(KF.measurementMatrix);
- //processNoiseCov对应过程噪声协方差Q
- setIdentity(KF.processNoiseCov, Scalar::all(1e-5));
- //measurementNoiseCov对一个测量噪声协方差R
- setIdentity(KF.measurementNoiseCov, Scalar::all(1e-1));
- //errorCovPost对应最优值对应的偏差P(k|k)
- setIdentity(KF.errorCovPost, Scalar::all(1));
- //statePost对应系统状态最优值x(k|k)
- randn(KF.statePost, Scalar::all(0), Scalar::all(0.1));
- for(;;)
- {
- Point2f center(img.cols*0.5f, img.rows*0.5f);
- float R = img.cols/3.f;
- //角度
- double stateAngle = state.at<float>(0);
- Point statePt = calcPoint(center, R, stateAngle);
- //X(k|k-1) = A*X(k-1|k-1)
- Mat prediction = KF.predict();
- //角度
- double predictAngle = prediction.at<float>(0);
- Point predictPt = calcPoint(center, R, predictAngle);
- randn( measurement, Scalar::all(0), Scalar::all(KF.measurementNoiseCov.at<float>(0)));
- // generate measurement
- //Z(k) = H*X(k)
- measurement += KF.measurementMatrix*state;
- //角度
- double measAngle = measurement.at<float>(0);
- Point measPt = calcPoint(center, R, measAngle);
- // plot points
- #define drawCross( center, color, d ) \
- line( img, Point( center.x - d, center.y - d ), \
- Point( center.x + d, center.y + d ), color, 1, CV_AA, 0); \
- line( img, Point( center.x + d, center.y - d ), \
- Point( center.x - d, center.y + d ), color, 1, CV_AA, 0 )
- img = Scalar::all(0);
- drawCross( statePt, Scalar(255,255,255), 3 );
- drawCross( measPt, Scalar(0,0,255), 3 );
- drawCross( predictPt, Scalar(0,255,0), 3 );
- line( img, statePt, measPt, Scalar(0,0,255), 3, CV_AA, 0 );
- line( img, statePt, predictPt, Scalar(0,255,255), 3, CV_AA, 0 );
- if(theRNG().uniform(0,4) != 0)
- //X(k|k) = X(k|k-1) + Kg(k)*[Z(k) - H*X(k|k-1)
- KF.correct(measurement);
- randn( processNoise, Scalar(0), Scalar::all(sqrt(KF.processNoiseCov.at<float>(0, 0))));
- //X(k) = AX(k-1) + W(k)
- state = KF.transitionMatrix*state + processNoise;
- imshow( "Kalman", img );
- code = (char)waitKey(100);
- if( code > 0 )
- break;
- }
- if( code == 27 || code == 'q' || code == 'Q' )
- break;
- }
- return 0;
- }
同时为了更好的理解代码,我们需要知道一下的东西
代码1.
- Mat statePre; //!< predicted state (x'(k)): x(k)=A*x(k-1)+B*u(k)
- Mat statePost; //!< corrected state (x(k)): x(k)=x'(k)+K(k)*(z(k)-H*x'(k))
- Mat transitionMatrix; //!< state transition matrix (A)
- Mat controlMatrix; //!< control matrix (B) (not used if there is no control)
- Mat measurementMatrix; //!< measurement matrix (H)
- Mat processNoiseCov; //!< process noise covariance matrix (Q)
- Mat measurementNoiseCov;//!< measurement noise covariance matrix (R)
- Mat errorCovPre; //!< priori error estimate covariance matrix (P'(k)): P'(k)=A*P(k-1)*At + Q)*/
- Mat gain; //!< Kalman gain matrix (K(k)): K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)
- Mat errorCovPost; //!< posteriori error estimate covariance matrix (P(k)): P(k)=(I-K(k)*H)*P'(k)
一看上面的注释大概也明白什么意思了。
此外,还需要看2断代码
- const Mat& KalmanFilter::predict(const Mat& control)
- {
- // update the state: x'(k) = A*x(k)
- statePre = transitionMatrix*statePost;
- if( control.data )
- // x'(k) = x'(k) + B*u(k)
- statePre += controlMatrix*control;
- // update error covariance matrices: temp1 = A*P(k)
- temp1 = transitionMatrix*errorCovPost;
- // P'(k) = temp1*At + Q
- gemm(temp1, transitionMatrix, 1, processNoiseCov, 1, errorCovPre, GEMM_2_T);
- // handle the case when there will be measurement before the next predict.
- statePre.copyTo(statePost);
- return statePre;
- }
这个段代码其实就是:X(k|k-1) = A(k-1|k-1) ,同时得到预测结果X(k|k-1)的偏差P(k|k-1)
- const Mat& KalmanFilter::correct(const Mat& measurement)
- {
- // temp2 = H*P'(k)
- temp2 = measurementMatrix * errorCovPre;
- // temp3 = temp2*Ht + R
- gemm(temp2, measurementMatrix, 1, measurementNoiseCov, 1, temp3, GEMM_2_T);
- // temp4 = inv(temp3)*temp2 = Kt(k)
- solve(temp3, temp2, temp4, DECOMP_SVD);
- // K(k):卡尔曼增益
- gain = temp4.t();
- // temp5 = z(k) - H*x'(k)
- temp5 = measurement - measurementMatrix*statePre;
- // x(k) = x'(k) + K(k)*temp5
- statePost = statePre + gain*temp5;
- // P(k) = P'(k) - K(k)*temp2
- errorCovPost = errorCovPre - gain*temp2;
- return statePost;
- }
上面的代码其实就是:求Kg(k)=P(k|k-1)H' / (HP(k|k-1)H' + R) ,X(k|k) = X(k|k-1) + Kg(k)(Z(k) - HX(k|k-1),P(k|k) = ( 1 - Kg(k)H)P(k|k-1)
原文:http://blog.csdn.net/suky520/article/details/20745479