mirror of
https://github.com/opencv/opencv.git
synced 2024-11-29 22:00:25 +08:00
112 lines
4.2 KiB
C
112 lines
4.2 KiB
C
#include "opencv2/video/tracking.hpp"
|
|
#include "opencv2/highgui/highgui.hpp"
|
|
|
|
#include <stdio.h>
|
|
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 argc, char** argv)
|
|
{
|
|
const float A[] = { 1, 1, 0, 1 };
|
|
help();
|
|
IplImage* img = cvCreateImage( cvSize(500,500), 8, 3 );
|
|
CvKalman* kalman = cvCreateKalman( 2, 1, 0 );
|
|
CvMat* state = cvCreateMat( 2, 1, CV_32FC1 ); /* (phi, delta_phi) */
|
|
CvMat* process_noise = cvCreateMat( 2, 1, CV_32FC1 );
|
|
CvMat* measurement = cvCreateMat( 1, 1, CV_32FC1 );
|
|
CvRNG rng = cvRNG(-1);
|
|
char code = -1;
|
|
|
|
cvZero( measurement );
|
|
cvNamedWindow( "Kalman", 1 );
|
|
|
|
for(;;)
|
|
{
|
|
cvRandArr( &rng, state, CV_RAND_NORMAL, cvRealScalar(0), cvRealScalar(0.1) );
|
|
|
|
memcpy( kalman->transition_matrix->data.fl, A, sizeof(A));
|
|
cvSetIdentity( kalman->measurement_matrix, cvRealScalar(1) );
|
|
cvSetIdentity( kalman->process_noise_cov, cvRealScalar(1e-5) );
|
|
cvSetIdentity( kalman->measurement_noise_cov, cvRealScalar(1e-1) );
|
|
cvSetIdentity( kalman->error_cov_post, cvRealScalar(1));
|
|
cvRandArr( &rng, kalman->state_post, CV_RAND_NORMAL, cvRealScalar(0), cvRealScalar(0.1) );
|
|
|
|
for(;;)
|
|
{
|
|
#define calc_point(angle) \
|
|
cvPoint( cvRound(img->width/2 + img->width/3*cos(angle)), \
|
|
cvRound(img->height/2 - img->width/3*sin(angle)))
|
|
|
|
float state_angle = state->data.fl[0];
|
|
CvPoint state_pt = calc_point(state_angle);
|
|
|
|
const CvMat* prediction = cvKalmanPredict( kalman, 0 );
|
|
float predict_angle = prediction->data.fl[0];
|
|
CvPoint predict_pt = calc_point(predict_angle);
|
|
float measurement_angle;
|
|
CvPoint measurement_pt;
|
|
|
|
cvRandArr( &rng, measurement, CV_RAND_NORMAL, cvRealScalar(0),
|
|
cvRealScalar(sqrt(kalman->measurement_noise_cov->data.fl[0])) );
|
|
|
|
/* generate measurement */
|
|
cvMatMulAdd( kalman->measurement_matrix, state, measurement, measurement );
|
|
|
|
measurement_angle = measurement->data.fl[0];
|
|
measurement_pt = calc_point(measurement_angle);
|
|
|
|
/* plot points */
|
|
#define draw_cross( center, color, d ) \
|
|
cvLine( img, cvPoint( center.x - d, center.y - d ), \
|
|
cvPoint( center.x + d, center.y + d ), color, 1, CV_AA, 0); \
|
|
cvLine( img, cvPoint( center.x + d, center.y - d ), \
|
|
cvPoint( center.x - d, center.y + d ), color, 1, CV_AA, 0 )
|
|
|
|
cvZero( img );
|
|
draw_cross( state_pt, CV_RGB(255,255,255), 3 );
|
|
draw_cross( measurement_pt, CV_RGB(255,0,0), 3 );
|
|
draw_cross( predict_pt, CV_RGB(0,255,0), 3 );
|
|
cvLine( img, state_pt, measurement_pt, CV_RGB(255,0,0), 3, CV_AA, 0 );
|
|
cvLine( img, state_pt, predict_pt, CV_RGB(255,255,0), 3, CV_AA, 0 );
|
|
|
|
cvKalmanCorrect( kalman, measurement );
|
|
|
|
cvRandArr( &rng, process_noise, CV_RAND_NORMAL, cvRealScalar(0),
|
|
cvRealScalar(sqrt(kalman->process_noise_cov->data.fl[0])));
|
|
cvMatMulAdd( kalman->transition_matrix, state, process_noise, state );
|
|
|
|
cvShowImage( "Kalman", img );
|
|
code = (char) cvWaitKey( 100 );
|
|
|
|
if( code > 0 )
|
|
break;
|
|
}
|
|
if( code == 27 || code == 'q' || code == 'Q' )
|
|
break;
|
|
}
|
|
|
|
cvDestroyWindow("Kalman");
|
|
|
|
return 0;
|
|
}
|
|
|
|
#ifdef _EiC
|
|
main(1, "kalman.c");
|
|
#endif
|