-
Notifications
You must be signed in to change notification settings - Fork 1
/
Kalman.cpp
83 lines (70 loc) · 2.76 KB
/
Kalman.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
#include "Kalman.h"
#include "opencv2/opencv.hpp"
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
TKalmanFilter::TKalmanFilter(Point2f pt,float dt,float Accel_noise_mag)
{
//ïðèðàùåíèå âðåìåíè (÷åì ìåíüøå ýòà âåëè÷èíà, òåì "èíåðòíåå" öåëü)
deltatime = dt; //0.2
// Óñêîðåíèå ìû íå çíàåì, ïîýòîìó îòíîñèì åãî ê øóìó ïðîöåññà.
// Çàòî ìû ìîæåì ïðåäïîëàãàòü, êàêèå âåëè÷èíû óñêîðåíèÿ ìîæåò âûäàòü îòñëåæèâàåìûé îáúåêò.
// Øóì ïðîöåññà. (ñòàíäàðòíîå îòêëîíåíèå âåëè÷èíû óñêîðåíèÿ: ì/ñ^2)
// ïîêàçûâàåò, íàñêîëüêî ñèëüíî îáúåêò ìîæåò óñêîðèòüñÿ.
//float Accel_noise_mag = 0.5;
//4 ïåðåìåííûõ ñîñòîÿíèÿ, 2 ïåðåìåííûõ èçìåðåíèÿ
kalman = new KalmanFilter( 4, 2, 0 );
// Ìàòðèöà ïåðåõîäà
kalman->transitionMatrix = (Mat_<float>(4, 4) << 1,0,deltatime,0, 0,1,0,deltatime, 0,0,1,0, 0,0,0,1);
// init...
LastResult = pt;
kalman->statePre.at<float>(0) = pt.x; // x
kalman->statePre.at<float>(1) = pt.y; // y
kalman->statePre.at<float>(2) = 0;
kalman->statePre.at<float>(3) = 0;
kalman->statePost.at<float>(0)=pt.x;
kalman->statePost.at<float>(1)=pt.y;
setIdentity(kalman->measurementMatrix);
kalman->processNoiseCov=(Mat_<float>(4, 4) <<pow(deltatime,4.0)/4.0,0,pow(deltatime,3.0)/2.0,0,0,pow(deltatime,4.0)/4.0,0,pow(deltatime,3.0)/2.0,pow(deltatime,3.0)/2.0 ,0 ,pow(deltatime,2.0) ,0,
0,pow(deltatime,3.0)/2.0,0,pow(deltatime,2.0));
kalman->processNoiseCov*=Accel_noise_mag;
setIdentity(kalman->measurementNoiseCov, Scalar::all(0.1));
setIdentity(kalman->errorCovPost, Scalar::all(.1));
}
//---------------------------------------------------------------------------
TKalmanFilter::~TKalmanFilter()
{
delete kalman;
}
//---------------------------------------------------------------------------
Point2f TKalmanFilter::GetPrediction()
{
Mat prediction = kalman->predict();
LastResult=Point2f(prediction.at<float>(0),prediction.at<float>(1));
return LastResult;
}
//---------------------------------------------------------------------------
Point2f TKalmanFilter::Update(Point2f p, bool DataCorrect)
{
Mat measurement(2,1,CV_32FC1);
if(!DataCorrect)
{
measurement.at<float>(0) = LastResult.x; //óòî÷íÿåì èñïîëüçóÿ ïðåäñêàçàíèå
measurement.at<float>(1) = LastResult.y;
}
else
{
measurement.at<float>(0) = p.x; //óòî÷íÿåì, èñïîëüçóÿ äàííûå èçìåðåíèé
measurement.at<float>(1) = p.y;
}
// Êîððåêöèÿ
Mat estimated = kalman->correct(measurement);
LastResult.x=estimated.at<float>(0); //óòî÷íÿåì, èñïîëüçóÿ äàííûå èçìåðåíèé
LastResult.y=estimated.at<float>(1);
return LastResult;
}
//---------------------------------------------------------------------------