Ask Your Question

Revision history [back]

It's an answer but that's not my program. I have used many WEB samples and it gives

#include <iostream>
#include <vector>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/tracking.hpp>

using namespace cv;
using namespace std;


struct mouse_info_struct { int x,y; };
struct mouse_info_struct mouse_info = {-1,-1}, last_mouse;


            // 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 )


vector<Point> mousev,kalmanv;

void on_mouse(int event, int x, int y, int flags, void* param) {
 {
  last_mouse = mouse_info;
  mouse_info.x = x;
  mouse_info.y = y;

  cout << "got mouse " << x <<","<< y <<endl;
 }
}


int main (int argc, char * const argv[]) 
{
    Mat img(500, 500, CV_8UC3);
    KalmanFilter KF(6, 2, 0);
    Mat_<float> state(6, 1); 
    Mat processNoise(6, 1, CV_32F);
    Mat_<float> measurement(2,1); measurement.setTo(Scalar(0));
    char code = (char)-1;

    namedWindow("mouse kalman");
    setMouseCallback("mouse kalman", on_mouse, 0);

    for(;;)
    {
        if (mouse_info.x < 0 || mouse_info.y < 0) 
        {
            imshow("mouse kalman", img);
            waitKey(30);
            continue;
        }
        else
            break;
    }
    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.statePre.at<float>(4) = 0;
    KF.statePre.at<float>(5) = 0;

    KF.transitionMatrix = (Mat_<float>(6, 6) << 1,0,1,0,0.5,0, 0,1,0,1,0,0.5, 0,0,1,0,1,0, 0,0,0,1,0,1, 0,0,0,0,1,0, 0,0,0,0,0,1);
    KF.measurementMatrix = (Mat_<float>(2, 6) << 1,0,1,0,0.5,0, 0,1,0,1,0,0.5);

    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();
    while (1==1)
    {

        Mat prediction = KF.predict();
        Point predictPt(prediction.at<float>(0),prediction.at<float>(1));

        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( "mouse kalman", img );
        code = (char)waitKey(10);

        if( code > 0 )
            break;
        }

    return 0;
}

It's an answer but that's not my program. Please don't vote. I have used many WEB samples and it gives

#include <iostream>
#include <vector>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/tracking.hpp>

using namespace cv;
using namespace std;


struct mouse_info_struct { int x,y; };
struct mouse_info_struct mouse_info = {-1,-1}, last_mouse;


            // 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 )


vector<Point> mousev,kalmanv;

void on_mouse(int event, int x, int y, int flags, void* param) {
 {
  last_mouse = mouse_info;
  mouse_info.x = x;
  mouse_info.y = y;

  cout << "got mouse " << x <<","<< y <<endl;
 }
}


int main (int argc, char * const argv[]) 
{
    Mat img(500, 500, CV_8UC3);
    KalmanFilter KF(6, 2, 0);
    Mat_<float> state(6, 1); 
    Mat processNoise(6, 1, CV_32F);
    Mat_<float> measurement(2,1); measurement.setTo(Scalar(0));
    char code = (char)-1;

    namedWindow("mouse kalman");
    setMouseCallback("mouse kalman", on_mouse, 0);

    for(;;)
    {
        if (mouse_info.x < 0 || mouse_info.y < 0) 
        {
            imshow("mouse kalman", img);
            waitKey(30);
            continue;
        }
        else
            break;
    }
    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.statePre.at<float>(4) = 0;
    KF.statePre.at<float>(5) = 0;

    KF.transitionMatrix = (Mat_<float>(6, 6) << 1,0,1,0,0.5,0, 0,1,0,1,0,0.5, 0,0,1,0,1,0, 0,0,0,1,0,1, 0,0,0,0,1,0, 0,0,0,0,0,1);
    KF.measurementMatrix = (Mat_<float>(2, 6) << 1,0,1,0,0.5,0, 0,1,0,1,0,0.5);

    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();
    while (1==1)
    {

        Mat prediction = KF.predict();
        Point predictPt(prediction.at<float>(0),prediction.at<float>(1));

        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( "mouse kalman", img );
        code = (char)waitKey(10);

        if( code > 0 )
            break;
        }

    return 0;
}

It's an answer but that's not my program. Please don't vote. I have used many WEB samples and it gives

#include <iostream>
#include <vector>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/tracking.hpp>

using namespace cv;
using namespace std;


struct mouse_info_struct { int x,y; };
struct mouse_info_struct mouse_info = {-1,-1}, last_mouse;


            // 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 )


vector<Point> mousev,kalmanv;

void on_mouse(int event, int x, int y, int flags, void* param) {
 {
  last_mouse = mouse_info;
  mouse_info.x = x;
  mouse_info.y = y;

  cout << "got mouse " << x <<","<< y <<endl;
 }
}


int main (int argc, char * const argv[]) 
{
    Mat img(500, 500, CV_8UC3);
    KalmanFilter KF(6, 2, 0);
    Mat_<float> state(6, 1); 
    Mat processNoise(6, 1, CV_32F);
    Mat_<float> measurement(2,1); measurement.setTo(Scalar(0));
    char code = (char)-1;

    namedWindow("mouse kalman");
    setMouseCallback("mouse kalman", on_mouse, 0);

    for(;;)
    {
        if (mouse_info.x < 0 || mouse_info.y < 0) 
        {
            imshow("mouse kalman", img);
            waitKey(30);
            continue;
        }
        else
            break;
    }
    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.statePre.at<float>(4) = 0;
    KF.statePre.at<float>(5) = 0;

    KF.transitionMatrix = (Mat_<float>(6, 6) << 1,0,1,0,0.5,0, 0,1,0,1,0,0.5, 0,0,1,0,1,0, 0,0,0,1,0,1, 0,0,0,0,1,0, 0,0,0,0,0,1);
    KF.measurementMatrix = (Mat_<float>(2, 6) << 1,0,1,0,0.5,0, 0,1,0,1,0,0.5);

    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();
    while (1==1)
    {

        Mat prediction = KF.predict();
        Point predictPt(prediction.at<float>(0),prediction.at<float>(1));

        measurement(0) = mouse_info.x;
        measurement(1) = mouse_info.y;
        Point measPt(measurement(0),measurement(1));
        cout << norm(predictPt - measPt) << "\t";
        {
              Point2f predictPt(prediction.at<float>(0),prediction.at<float>(1));
              Point2f measPt(measurement(0),measurement(1));
              cout << norm(predictPt - measPt) << "\n";
       }


        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( "mouse kalman", img );
        code = (char)waitKey(10);

        if( code > 0 )
            break;
        }

    return 0;
}