Advertisement
jack06215

[OpenCV] Kalman filter 2D vector points

Jul 8th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. // header
  2. #include <iostream>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <opencv2/video/tracking.hpp>
  5.  
  6. using namespace std;
  7. using namespace cv;
  8.  
  9. class Kalman
  10. {
  11. private:
  12.     vector<KalmanFilter> filters;
  13.     void initialize(int numPts);
  14.     void initState(vector<Point2f> &pts);
  15.     bool initialized;
  16.  
  17. public:
  18.     Kalman(vector<Point2f> &pts);
  19.     Kalman(int numPts);
  20.     vector<Point2f> correct(vector<Point2f> &pts);
  21.     vector<Point2f> predict();
  22.     void reset();
  23. };
  24.  
  25. // c file
  26. #include "kalman.h"
  27.  
  28.  
  29. Kalman::Kalman(int numPts) :
  30.     filters(numPts), initialized(false)
  31. {
  32.     initialize(numPts);
  33. }
  34. Kalman::Kalman(vector<Point2f> &pts) :
  35.     filters(pts.size()), initialized(false)
  36. {
  37.     initialize((int)pts.size());
  38.     initState(pts);
  39. }
  40.  
  41. void Kalman::initialize(int numPts)
  42. {
  43.     for (size_t i = 0; i < numPts; ++i) {
  44.         KalmanFilter KF(4, 2, 0);
  45.         //A matrix
  46.         KF.transitionMatrix = (Mat_<float>(4, 4) << 1, 0, 1, 0,
  47.             0, 1, 0, 1,
  48.             0, 0, 1, 0,
  49.             0, 0, 0, 1);
  50.         setIdentity(KF.measurementMatrix);
  51.  
  52.         cv::setIdentity(KF.processNoiseCov, cv::Scalar::all(1e-5));       // set process noise
  53.         cv::setIdentity(KF.measurementNoiseCov, cv::Scalar::all(1e-4));   // set measurement noise
  54.         cv::setIdentity(KF.errorCovPost, cv::Scalar::all(1));             // error covariance
  55.  
  56.         //setIdentity(KF.processNoiseCov, Scalar::all(1e-4));
  57.         //setIdentity(KF.measurementNoiseCov, Scalar::all(1e-1));
  58.         //setIdentity(KF.errorCovPost, Scalar::all(.1));
  59.  
  60.  
  61.         filters[i] = (KF);
  62.     }
  63. }
  64. void Kalman::initState(vector<Point2f> &pts)
  65. {
  66.     CV_Assert(filters.size() == pts.size());
  67.     for (size_t i = 0; i < pts.size(); ++i)
  68.     {
  69.         filters[i].statePre.at<float>(0) = pts[i].x;
  70.         filters[i].statePre.at<float>(1) = pts[i].y;
  71.         filters[i].statePre.at<float>(2) = 1;
  72.         filters[i].statePre.at<float>(3) = 1;
  73.     }
  74.     initialized = true;
  75. }
  76. void Kalman::reset()
  77. {
  78.     initialized = false;
  79. }
  80.  
  81. vector<Point2f> Kalman::correct(vector<Point2f> &pts)
  82. {
  83.     CV_Assert(filters.size() == pts.size());
  84.  
  85.     if (!initialized)
  86.     {
  87.         initState(pts);
  88.         return pts;
  89.     }
  90.  
  91.     vector<Point2f> estimation(pts.size());
  92.  
  93.     Mat measurement(2, 1, CV_32F);
  94.     for (size_t i = 0; i < filters.size(); i++)
  95.     {
  96.         filters[i].predict();
  97.         measurement.at<float>(0) = pts[i].x;
  98.         measurement.at<float>(1) = pts[i].y;
  99.  
  100.         Mat estimated = filters[i].correct(measurement);
  101.  
  102.         estimation[i] = Point(estimated.at<float>(0),
  103.             estimated.at<float>(1));
  104.  
  105.     }
  106.     return estimation;
  107. }
  108.  
  109. vector<Point2f> Kalman::predict()
  110. {
  111.     vector<Point2f> prediction(filters.size(), Point2f(0, 0));
  112.     if (!initialized)
  113.         return prediction;
  114.  
  115.     Mat predict(2, 1, CV_32F);
  116.     for (size_t i = 0; i < filters.size(); i++)
  117.     {
  118.         predict = filters[i].predict();
  119.         prediction[i] = Point(predict.at<float>(0),
  120.             predict.at<float>(1));
  121.  
  122.     }
  123.     return prediction;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement