Update to 2.0.0 tree from current Fremantle build
[opencv] / samples / c / kalman.c
1 /*
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.
12 */
13
14 #ifdef _CH_
15 #pragma package <opencv>
16 #endif
17
18 #define CV_NO_BACKWARD_COMPATIBILITY
19
20 #ifndef _EiC
21 #include "cv.h"
22 #include "highgui.h"
23 #include <math.h>
24 #endif
25
26 int main(int argc, char** argv)
27 {
28     const float A[] = { 1, 1, 0, 1 };
29
30     IplImage* img = cvCreateImage( cvSize(500,500), 8, 3 );
31     CvKalman* kalman = cvCreateKalman( 2, 1, 0 );
32     CvMat* state = cvCreateMat( 2, 1, CV_32FC1 ); /* (phi, delta_phi) */
33     CvMat* process_noise = cvCreateMat( 2, 1, CV_32FC1 );
34     CvMat* measurement = cvCreateMat( 1, 1, CV_32FC1 );
35     CvRNG rng = cvRNG(-1);
36     char code = -1;
37
38     cvZero( measurement );
39     cvNamedWindow( "Kalman", 1 );
40
41     for(;;)
42     {
43         cvRandArr( &rng, state, CV_RAND_NORMAL, cvRealScalar(0), cvRealScalar(0.1) );
44
45         memcpy( kalman->transition_matrix->data.fl, A, sizeof(A));
46         cvSetIdentity( kalman->measurement_matrix, cvRealScalar(1) );
47         cvSetIdentity( kalman->process_noise_cov, cvRealScalar(1e-5) );
48         cvSetIdentity( kalman->measurement_noise_cov, cvRealScalar(1e-1) );
49         cvSetIdentity( kalman->error_cov_post, cvRealScalar(1));
50         cvRandArr( &rng, kalman->state_post, CV_RAND_NORMAL, cvRealScalar(0), cvRealScalar(0.1) );
51
52         for(;;)
53         {
54             #define calc_point(angle)                                      \
55                 cvPoint( cvRound(img->width/2 + img->width/3*cos(angle)),  \
56                          cvRound(img->height/2 - img->width/3*sin(angle)))
57
58             float state_angle = state->data.fl[0];
59             CvPoint state_pt = calc_point(state_angle);
60
61             const CvMat* prediction = cvKalmanPredict( kalman, 0 );
62             float predict_angle = prediction->data.fl[0];
63             CvPoint predict_pt = calc_point(predict_angle);
64             float measurement_angle;
65             CvPoint measurement_pt;
66
67             cvRandArr( &rng, measurement, CV_RAND_NORMAL, cvRealScalar(0),
68                        cvRealScalar(sqrt(kalman->measurement_noise_cov->data.fl[0])) );
69
70             /* generate measurement */
71             cvMatMulAdd( kalman->measurement_matrix, state, measurement, measurement );
72
73             measurement_angle = measurement->data.fl[0];
74             measurement_pt = calc_point(measurement_angle);
75
76             /* plot points */
77             #define draw_cross( center, color, d )                                 \
78                 cvLine( img, cvPoint( center.x - d, center.y - d ),                \
79                              cvPoint( center.x + d, center.y + d ), color, 1, CV_AA, 0); \
80                 cvLine( img, cvPoint( center.x + d, center.y - d ),                \
81                              cvPoint( center.x - d, center.y + d ), color, 1, CV_AA, 0 )
82
83             cvZero( img );
84             draw_cross( state_pt, CV_RGB(255,255,255), 3 );
85             draw_cross( measurement_pt, CV_RGB(255,0,0), 3 );
86             draw_cross( predict_pt, CV_RGB(0,255,0), 3 );
87             cvLine( img, state_pt, measurement_pt, CV_RGB(255,0,0), 3, CV_AA, 0 );
88             cvLine( img, state_pt, predict_pt, CV_RGB(255,255,0), 3, CV_AA, 0 );
89
90             cvKalmanCorrect( kalman, measurement );
91
92             cvRandArr( &rng, process_noise, CV_RAND_NORMAL, cvRealScalar(0),
93                        cvRealScalar(sqrt(kalman->process_noise_cov->data.fl[0])));
94             cvMatMulAdd( kalman->transition_matrix, state, process_noise, state );
95
96             cvShowImage( "Kalman", img );
97             code = (char) cvWaitKey( 100 );
98
99             if( code > 0 )
100                 break;
101         }
102         if( code == 27 || code == 'q' || code == 'Q' )
103             break;
104     }
105
106     cvDestroyWindow("Kalman");
107
108     return 0;
109 }
110
111 #ifdef _EiC
112 main(1, "kalman.c");
113 #endif