2 Tracking of rotating point.
3 Rotation speed is constant.
4 Both state and measurements vectors are 1D (a point angle),
5 Measurement is the real point angle + gaussian noise.
6 The real and the estimated points are connected with yellow line segment,
7 the real and the measured points are connected with red line segment.
8 (if Kalman filter works correctly,
9 the yellow segment should be shorter than the red one).
10 Pressing any key (except ESC) will reset the tracking with a different speed.
11 Pressing ESC will stop the program.
15 #pragma package <opencv>
24 int main(int argc, char** argv)
26 const float A[] = { 1, 1, 0, 1 };
28 IplImage* img = cvCreateImage( cvSize(500,500), 8, 3 );
29 CvKalman* kalman = cvCreateKalman( 2, 1, 0 );
30 CvMat* state = cvCreateMat( 2, 1, CV_32FC1 ); /* (phi, delta_phi) */
31 CvMat* process_noise = cvCreateMat( 2, 1, CV_32FC1 );
32 CvMat* measurement = cvCreateMat( 1, 1, CV_32FC1 );
33 CvRNG rng = cvRNG(-1);
36 cvZero( measurement );
37 cvNamedWindow( "Kalman", 1 );
41 cvRandArr( &rng, state, CV_RAND_NORMAL, cvRealScalar(0), cvRealScalar(0.1) );
43 memcpy( kalman->transition_matrix->data.fl, A, sizeof(A));
44 cvSetIdentity( kalman->measurement_matrix, cvRealScalar(1) );
45 cvSetIdentity( kalman->process_noise_cov, cvRealScalar(1e-5) );
46 cvSetIdentity( kalman->measurement_noise_cov, cvRealScalar(1e-1) );
47 cvSetIdentity( kalman->error_cov_post, cvRealScalar(1));
48 cvRandArr( &rng, kalman->state_post, CV_RAND_NORMAL, cvRealScalar(0), cvRealScalar(0.1) );
52 #define calc_point(angle) \
53 cvPoint( cvRound(img->width/2 + img->width/3*cos(angle)), \
54 cvRound(img->height/2 - img->width/3*sin(angle)))
56 float state_angle = state->data.fl[0];
57 CvPoint state_pt = calc_point(state_angle);
59 const CvMat* prediction = cvKalmanPredict( kalman, 0 );
60 float predict_angle = prediction->data.fl[0];
61 CvPoint predict_pt = calc_point(predict_angle);
62 float measurement_angle;
63 CvPoint measurement_pt;
65 cvRandArr( &rng, measurement, CV_RAND_NORMAL, cvRealScalar(0),
66 cvRealScalar(sqrt(kalman->measurement_noise_cov->data.fl[0])) );
68 /* generate measurement */
69 cvMatMulAdd( kalman->measurement_matrix, state, measurement, measurement );
71 measurement_angle = measurement->data.fl[0];
72 measurement_pt = calc_point(measurement_angle);
75 #define draw_cross( center, color, d ) \
76 cvLine( img, cvPoint( center.x - d, center.y - d ), \
77 cvPoint( center.x + d, center.y + d ), color, 1, CV_AA, 0); \
78 cvLine( img, cvPoint( center.x + d, center.y - d ), \
79 cvPoint( center.x - d, center.y + d ), color, 1, CV_AA, 0 )
82 draw_cross( state_pt, CV_RGB(255,255,255), 3 );
83 draw_cross( measurement_pt, CV_RGB(255,0,0), 3 );
84 draw_cross( predict_pt, CV_RGB(0,255,0), 3 );
85 cvLine( img, state_pt, measurement_pt, CV_RGB(255,0,0), 3, CV_AA, 0 );
86 cvLine( img, state_pt, predict_pt, CV_RGB(255,255,0), 3, CV_AA, 0 );
88 cvKalmanCorrect( kalman, measurement );
90 cvRandArr( &rng, process_noise, CV_RAND_NORMAL, cvRealScalar(0),
91 cvRealScalar(sqrt(kalman->process_noise_cov->data.fl[0])));
92 cvMatMulAdd( kalman->transition_matrix, state, process_noise, state );
94 cvShowImage( "Kalman", img );
95 code = (char) cvWaitKey( 100 );
100 if( code == 27 || code == 'q' || code == 'Q' )
104 cvDestroyWindow("Kalman");