CKalmanFilter.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include "CKalmanFilter.h"
3 
4 using namespace cv;
5 using namespace std;
6 
7 // Constructor
8 CKalmanFilter::CKalmanFilter(vector<Vec2f> p){
9 
10  kalman = new KalmanFilter( 4, 4, 0 ); // 4 measurement and state parameters
11  kalman->transitionMatrix = (Mat_<float>(4, 4) << 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1);
12 
13  // Initialization
14  prevResult = p;
15  kalman->statePre.at<float>(0) = p[0][0]; // r1
16  kalman->statePre.at<float>(1) = p[0][1]; // theta1
17  kalman->statePre.at<float>(2) = p[1][0]; // r2
18  kalman->statePre.at<float>(3) = p[1][1]; // theta2
19 
20  kalman->statePost.at<float>(0)=p[0][0];
21  kalman->statePost.at<float>(1)=p[0][1];
22  kalman->statePost.at<float>(2)=p[1][0];
23  kalman->statePost.at<float>(3)=p[1][1];
24 
25  setIdentity(kalman->measurementMatrix);
26  setIdentity(kalman->processNoiseCov, Scalar::all(1e-4));
27  setIdentity(kalman->measurementNoiseCov, Scalar::all(1e-1));
28  setIdentity(kalman->errorCovPost, Scalar::all(5));
29 }
30 
31 // Destructor
33  delete kalman;
34 }
35 
36 // Prediction
37 vector<Vec2f> CKalmanFilter::predict(){
38  Mat prediction = kalman->predict(); // predict the state of the next frame
39  prevResult[0][0] = prediction.at<float>(0);prevResult[0][1] = prediction.at<float>(1);
40  prevResult[1][0] = prediction.at<float>(2);prevResult[1][1] = prediction.at<float>(3);
41  return prevResult;
42 
43 }
44 
45 // Correct the prediction based on the measurement
46 vector<Vec2f> CKalmanFilter::update(vector<Vec2f> measure){
47 
48 
49  Mat_<float> measurement(4,1);
50  measurement.setTo(Scalar(0));
51 
52  measurement.at<float>(0) = measure[0][0];measurement.at<float>(1) = measure[0][1];
53  measurement.at<float>(2) = measure[1][0];measurement.at<float>(3) = measure[1][1];
54 
55  Mat estimated = kalman->correct(measurement); // Correct the state of the next frame after obtaining the measurements
56 
57  // Update the measurement
58  if(estimated.at<float>(0) < estimated.at<float>(2)){
59  measure[0][0] = estimated.at<float>(0);measure[0][1] = estimated.at<float>(1);
60  measure[1][0] = estimated.at<float>(2);measure[1][1] = estimated.at<float>(3);
61  }
62  else{
63  measure[0][0] = estimated.at<float>(2);measure[0][1] = estimated.at<float>(3);
64  measure[1][0] = estimated.at<float>(0);measure[1][1] = estimated.at<float>(1);
65  }
66 
67  waitKey(1);
68 
69  return measure; // return the measurement
70 
71 }
void setIdentity(geometry_msgs::Transform &tx)
cv::Mat_< float > measurement(2, 1)
vector< Vec2f > update(vector< Vec2f >)
CKalmanFilter(vector< Vec2f >)
vector< Vec2f > predict()


multi_object_tracking_lidar
Author(s): Praveen Palanisamy
autogenerated on Sun Apr 28 2019 02:57:37