-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithm_CondensationB.h
163 lines (123 loc) · 4.72 KB
/
Algorithm_CondensationB.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef ALGORITHM_CondensationB_H
#define ALGORITHM_CondensationB_H
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/video/tracking.hpp>
#include <random>
#include <cmath>
#include <chrono>
#include <opencv2/ml>
class CondensationB: public CondensationB{
public:
typedef std::chrono::high_resolution_clock Clock;
typedef std::vector<cv::Point2f> Features;
typedef std::vector<double> Weights;
typedef struct{
double estimation;
unsigned int nParticles;
Features particles;
Weights weights;
} State;
typedef std::default_random_engine Generator;
typedef std::normal_distribution<double> Distribution;
private:
cv::Mat gray; // current gray-level image
cv::Mat gray_prev; // previous gray-level image
// global system state
State predictState;
State observState;
//covariance
double Rx;
double Qx;
// Luminance Invariant
Features xLinearPrev;
Features xLinearPredict;
Features xLinearObserv;
Weights xLinearWeights;
// config goodFeatureToTrack for linear feature detection or Harris corner
/**
void goodFeaturesToTrack(InputArray image, OutputArray corners, int maxCorners, double qualityLevel,
double minDistance, InputArray mask=noArray(), int blockSize=3, bool useHarrisDetector=false, double k=0.04 )
**/
int maxCorners = 23; // Maximum number of corners to return.
double qualityLevel = 0.01; // Minimal accepted quality of image corners.
double minDistance = 10; // Minimum possible Euclidean distance between the returned corners.
int blockSize = 3; // Size of an average block for computing a derivative covariation matrix
bool useHarrisDetector = false;
double k = 0.04;// Free parameter of the Harris detector
//OFC (optical flow constraint)
Features xOfcPrev;
Features xOfcObserv;
Features xOfcPredict;
Weights xOfcWeights;
/**
void calcOpticalFlowPyrLK( InputArray prevImg, InputArray nextImg, InputArray prevPts,
InputOutputArray nextPts, OutputArray status, OutputArray err, Size winSize=Size(21,21),
int maxLevel=3, TermCriteria criteria=TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01),
int flags=0, double minEigThreshold=1e-4 )
**/
std::vector<uchar> status; // status of tracked features
std::vector<float> err; // error in tracking
public:
CondensationB();
void process(const cv::Mat &in, cv::Mat &out){
// convert to gray-level image
cv::cvtColor(in, gray, CV_BGR2GRAY);
}
void init(){ // initialize
//cv::calcCovarMatrix();
//cv::Mahalanobis();
}
void observe(){ // actualize observation
for (int i = 0; i < predictState.nParticles; ++i) {
cv::goodFeaturesToTrack(gray,xLinearObserv[i],maxCorners,qualityLevel,minDistance);
cv::calcOpticalFlowPyrLK(gray_prev,gray,xOfcPrev[i],xOfcObserv[i],status,err);
}
}
void measure(){ // weight possible trajectoire
// mesurer distance euclidienne entre points(Lineaire et OFC)
for (int i = 0; i < predictState.nParticles; ++i) {
xLinearWeights[i] = hypothenus(xLinearPredict[i],xLinearObserv[i]);
xOfcWeights[i] = hypothenus(xOfcPredict[i],xOfcObserv[i]);
}
}
void predict(){ // propagate N particule
for (int i = 0; i < predictState.nParticles; ++i) {
cv::Point2f x0 = xLinearPrev[i];
cv::Point2f xk = xLinearObserv[i];
cv::Point2f prediction;
prediction.x = linearPredict(x0.x,xk.x);
prediction.y = linearPredict(x0.y,xk.y);
xLinearPredict[i] = prediction;
}
}
float linearPredict(float x0, float xk){
// X_i(k+1) = f_x(X_i(k-1),X_i(k))
double wk = gaussian(0,Rx);
double vk = gaussian(0,Qx);
return (xk-x0)*vk + xk + wk;
}
void likelihoodValuation(){ // estimate Global State
}
void ReSampling(){ // reSample N particles
}
void normalize(){ // Normalize weight
}
//utils
unsigned int hypothenus(const cv::Point a, const cv::Point b){
return std::sqrt(std::pow(a.x-b.x,2)+std::pow(a.y-b.y,2));
}
double gaussian(float mu, float sigma){
Generator g;
Distribution d(mu,sigma);
return d(g);
}
double update(double muX, double muZ , double sigmaZ){
static const double PI = 3.14159265358979323846;
return 1.0/(std::sqrt(2.0*PI) * sigmaZ) * std::exp(-0.5 * ( (muZ-muX) / (sigmaZ*sigmaZ)));
}
//derivating: fprime(x) = (f(x+dx) - f(x-dx)) / (2*dx)
};
#endif // ALGORITHM_CondensationB_H