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