From: Vadim Pisarevsky Date: Fri, 3 Dec 2010 17:46:36 +0000 (+0000) Subject: converted Kalman sample to C++ X-Git-Tag: accepted/2.0/20130307.220821~3948 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=fe72a6eeb3aa480efa8c0cf1087affff5148d969;p=profile%2Fivi%2Fopencv.git converted Kalman sample to C++ --- diff --git a/samples/c/kalman.c b/samples/c/kalman.c deleted file mode 100644 index 9784400..0000000 --- a/samples/c/kalman.c +++ /dev/null @@ -1,111 +0,0 @@ -#include "opencv2/video/tracking.hpp" -#include "opencv2/highgui/highgui.hpp" - -#include -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 diff --git a/samples/cpp/kalman.cpp b/samples/cpp/kalman.cpp new file mode 100644 index 0000000..95bfa2c --- /dev/null +++ b/samples/cpp/kalman.cpp @@ -0,0 +1,101 @@ +#include "opencv2/video/tracking.hpp" +#include "opencv2/highgui/highgui.hpp" + +#include + +using namespace cv; + +static inline Point calcPoint(Point2f center, double R, double angle) +{ + return center + Point2f((float)cos(angle), (float)-sin(angle))*(float)R; +} + +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, char**) +{ + help(); + Mat img(500, 500, CV_8UC3); + KalmanFilter KF(2, 1, 0); + Mat state(2, 1, CV_32F); /* (phi, delta_phi) */ + Mat processNoise(2, 1, CV_32F); + Mat measurement = Mat::zeros(1, 1, CV_32F); + char code = (char)-1; + + for(;;) + { + randn( state, Scalar::all(0), Scalar::all(0.1) ); + KF.transitionMatrix = *(Mat_(2, 2) << 1, 1, 0, 1); + + setIdentity(KF.measurementMatrix); + setIdentity(KF.processNoiseCov, Scalar::all(1e-5)); + setIdentity(KF.measurementNoiseCov, Scalar::all(1e-1)); + setIdentity(KF.errorCovPost, Scalar::all(1)); + + randn(KF.statePost, Scalar::all(0), Scalar::all(0.1)); + + for(;;) + { + Point2f center(img.cols*0.5f, img.rows*0.5f); + float R = img.cols/3.f; + double stateAngle = state.at(0); + Point statePt = calcPoint(center, R, stateAngle); + + Mat prediction = KF.predict(); + double predictAngle = prediction.at(0); + Point predictPt = calcPoint(center, R, predictAngle); + + randn( measurement, Scalar::all(0), Scalar::all(KF.measurementNoiseCov.at(0))); + + // generate measurement + measurement += KF.measurementMatrix*state; + + double measAngle = measurement.at(0); + Point measPt = calcPoint(center, R, measAngle); + + // plot points + #define drawCross( center, color, d ) \ + line( img, Point( center.x - d, center.y - d ), \ + Point( center.x + d, center.y + d ), color, 1, CV_AA, 0); \ + line( img, Point( center.x + d, center.y - d ), \ + Point( center.x - d, center.y + d ), color, 1, CV_AA, 0 ) + + img = Scalar::all(0); + drawCross( statePt, Scalar(255,255,255), 3 ); + drawCross( measPt, Scalar(0,0,255), 3 ); + drawCross( predictPt, Scalar(0,255,0), 3 ); + line( img, statePt, measPt, Scalar(0,0,255), 3, CV_AA, 0 ); + line( img, statePt, predictPt, Scalar(0,255,255), 3, CV_AA, 0 ); + + KF.correct(measurement); + + randn( processNoise, Scalar(0), Scalar::all(sqrt(KF.processNoiseCov.at(0, 0)))); + state = KF.transitionMatrix*state + processNoise; + + imshow( "Kalman", img ); + code = (char)waitKey(100); + + if( code > 0 ) + break; + } + if( code == 27 || code == 'q' || code == 'Q' ) + break; + } + + return 0; +}