Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/video/tracking.hpp>
- using namespace cv;
- using namespace std;
- // plot points
- #define drawCross( center, color, d ) \
- line( img, Point( center.x - d, center.y - d ), \
- Point( center.x + d, center.y + d ), color, 2, CV_AA, 0); \
- line( img, Point( center.x + d, center.y - d ), \
- Point( center.x - d, center.y + d ), color, 2, CV_AA, 0 )
- struct mouse_info_struct { int x,y; };
- struct mouse_info_struct mouse_info = {-1,-1};
- vector<Point> mousev,kalmanv;
- KalmanFilter KF;
- void on_mouse(int event, int x, int y, int flags, void* param) {
- {
- mouse_info.x = x;
- mouse_info.y = y;
- }
- }
- void reset(){
- /*
- KF.statePre.at<float>(0) = mouse_info.x;
- KF.statePre.at<float>(1) = mouse_info.y;
- KF.statePre.at<float>(2) = 0;
- KF.statePre.at<float>(3) = 0;
- KF.transitionMatrix = *(Mat_<float>(4, 4) << 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
- //*/
- setIdentity(KF.measurementMatrix);
- setIdentity(KF.processNoiseCov, Scalar::all(1e-4));
- setIdentity(KF.measurementNoiseCov, Scalar::all(1e-1));
- setIdentity(KF.errorCovPost, Scalar::all(.1));
- mousev.clear();
- kalmanv.clear();
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- Mat img(500, 500, CV_8UC3);
- KF = *(new KalmanFilter(4, 2, 0));
- //Mat_<float> state(4, 1); //(x, y, Vx, Vy)
- //Mat processNoise(4, 1, CV_32F);
- Mat_<float> measurement(2,1); measurement.setTo(Scalar(0));
- char code = (char)-1;
- namedWindow("mk");
- setMouseCallback("mk", on_mouse, 0);
- reset();
- for(;;)
- {
- KF.predict();
- measurement(0) = mouse_info.x;
- measurement(1) = mouse_info.y;
- Point measPt(measurement(0),measurement(1));
- mousev.push_back(measPt);
- Mat estimated = KF.correct(measurement);
- Point statePt(estimated.at<float>(0),estimated.at<float>(1));
- kalmanv.push_back(statePt);
- img = Scalar::all(0);
- drawCross( statePt, Scalar(255,255,255), 5 );
- drawCross( measPt, Scalar(0,0,255), 5 );
- for (int i = 0; i < mousev.size()-1; i++) {
- line(img, mousev[i], mousev[i+1], Scalar(255,255,0), 1);
- }
- for (int i = 0; i < kalmanv.size()-1; i++) {
- line(img, kalmanv[i], kalmanv[i+1], Scalar(0,255,0), 1);
- }
- imshow( "mk", img );
- code = (char)waitKey(100);
- if( code > 0) reset();
- if( code == 27) break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement