Kalman滤波(三)
来源:互联网 发布:淘宝隐形眼镜可靠吗 编辑:程序博客网 时间:2024/06/07 19:11
今天研究了一下卡尔曼滤波跟踪,同时也看了一下卡尔曼滤波Opencv的源代码,总是看懂了。下面是opencv自带的一个程序,代码如下:
-
-
-
- #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);
-
-
-
-
-
-
-
-
-
-
- Mat state(2, 1, CV_32F);
- Mat processNoise(2, 1, CV_32F);
-
- Mat measurement = Mat::zeros(1, 1, CV_32F);
- char code = (char)-1;
-
- for(;;)
- {
- randn( state, Scalar::all(0), Scalar::all(0.1) );
-
- KF.transitionMatrix = *(Mat_<float>(2, 2) << 1, 1, 0, 1);
-
- setIdentity(KF.measurementMatrix);
-
- setIdentity(KF.processNoiseCov, Scalar::all(1e-5));
-
- setIdentity(KF.measurementNoiseCov, Scalar::all(1e-1));
-
- setIdentity(KF.errorCovPost, Scalar::all(1));
-
- 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);
-
-
- 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)));
-
-
-
- measurement += KF.measurementMatrix*state;
-
-
- double measAngle = measurement.at<float>(0);
- Point measPt = calcPoint(center, R, measAngle);
-
-
- #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)
-
- KF.correct(measurement);
-
- randn( processNoise, Scalar(0), Scalar::all(sqrt(KF.processNoiseCov.at<float>(0, 0))));
-
- 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;
- Mat statePost;
- Mat transitionMatrix;
- Mat controlMatrix;
- Mat measurementMatrix;
- Mat processNoiseCov;
- Mat measurementNoiseCov;
- Mat errorCovPre;
- Mat gain;
- Mat errorCovPost;
一看上面的注释大概也明白什么意思了。此外,还需要看2断代码
- const Mat& KalmanFilter::predict(const Mat& control)
- {
-
- statePre = transitionMatrix*statePost;
-
- if( control.data )
-
- statePre += controlMatrix*control;
-
-
- temp1 = transitionMatrix*errorCovPost;
-
-
- gemm(temp1, transitionMatrix, 1, processNoiseCov, 1, errorCovPre, GEMM_2_T);
-
-
- 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 = measurementMatrix * errorCovPre;
-
-
- gemm(temp2, measurementMatrix, 1, measurementNoiseCov, 1, temp3, GEMM_2_T);
-
-
- solve(temp3, temp2, temp4, DECOMP_SVD);
-
-
- gain = temp4.t();
-
-
- temp5 = measurement - measurementMatrix*statePre;
-
-
- statePost = statePre + gain*temp5;
-
-
- 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