Move the sources to trunk
[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 #ifndef _EiC
19 #include "cv.h"
20 #include "highgui.h"
21 #include <math.h>
22 #endif
23
24 int main(int argc, char** argv)
25 {
26     const float A[] = { 1, 1, 0, 1 };
27     
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);
34     char code = -1;
35
36     cvZero( measurement );
37     cvNamedWindow( "Kalman", 1 );
38
39     for(;;)
40     {
41         cvRandArr( &rng, state, CV_RAND_NORMAL, cvRealScalar(0), cvRealScalar(0.1) );
42         
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) );
49         
50         for(;;)
51         {
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))) 
55
56             float state_angle = state->data.fl[0];
57             CvPoint state_pt = calc_point(state_angle);
58             
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;
64
65             cvRandArr( &rng, measurement, CV_RAND_NORMAL, cvRealScalar(0),
66                        cvRealScalar(sqrt(kalman->measurement_noise_cov->data.fl[0])) );
67
68             /* generate measurement */
69             cvMatMulAdd( kalman->measurement_matrix, state, measurement, measurement );
70
71             measurement_angle = measurement->data.fl[0];
72             measurement_pt = calc_point(measurement_angle);
73             
74             /* plot points */
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 )
80
81             cvZero( img );
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 );
87             
88             cvKalmanCorrect( kalman, measurement );
89
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 );
93
94             cvShowImage( "Kalman", img );
95             code = (char) cvWaitKey( 100 );
96             
97             if( code > 0 )
98                 break;
99         }
100         if( code == 27 || code == 'q' || code == 'Q' )
101             break;
102     }
103     
104     cvDestroyWindow("Kalman");
105
106     return 0;
107 }
108
109 #ifdef _EiC
110 main(1, "kalman.c");
111 #endif