-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.hpp
171 lines (119 loc) · 4.31 KB
/
data.hpp
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
164
165
166
167
168
169
170
171
//
// data.cpp
//
//
// Created by Etienne Dejoie on 25/11/2015.
//
//
#ifndef DATA_H
#define DATA_H
using namespace std;
using namespace cv;
template <typename T> class Data{
public:
int minEstNb;
int nbParameters;
virtual vector<float> estimModel (vector<T> estimData) = 0; //renvoie un vecteur de taille nbParameter
virtual float calculateError1(T sousData, vector<float> model) = 0; //renvoie l'erreur d'un modèle avec une donnée
virtual float calculateError2(vector<T> sousData, vector<float> model) =0;// renvoie l'erreur avec une liste de donnée
virtual vector<T> getVc() = 0;
};
class LinearRansac : public Data<pair<float, float> >{
public:
vector<pair<float, float> > vc;
int minEstNb = 2;
int nbParameters = 2;
LinearRansac (vector<float> source, vector<float> target){
if (source.size() != target.size()) {
cout << "la taille de target est différente de celle de source, problème" << endl;
}
else {
for (int i = 0 ; i < source.size(); i++) {
vc.push_back(make_pair(source[i], target[i]));
}
}
}
virtual vector<pair<float,float> > getVc(){
return vc;
}
virtual vector<float> estimModel(vector<pair<float, float> > ll){
int n = ll.size();
// calculate the averages of arrays x and y
float xa = 0, ya = 0;
for (int i = 0; i < n; i++) {
xa += ll[i].first;
ya += ll[i].second;
}
xa /= n;
ya /= n;
// calculate auxiliary sums
float xx = 0, yy = 0, xy = 0;
for (int i = 0; i < n; i++) {
float tmpx = ll[i].first- xa, tmpy = ll[i].second - ya;
xx += tmpx * tmpx;
yy += tmpy * tmpy;
xy += tmpx * tmpy;
}
// calculate regression line parameters
float m_b = xy / xx;
float m_a = ya - m_b * xa;
vector<float> params;
params.push_back(m_b);
params.push_back(m_a);
return params;//return slope, intercept
}
virtual float calculateError1(pair<float, float> d, vector<float> model){
return pow(d.second - model[0]*d.first - model[1], 2);
}
virtual float calculateError2(vector<pair<float,float> > dat, vector<float> model){
float S = 0;
int n = dat.size();
for(pair<float,float> p : dat)
S += calculateError1(p, model);
return S/n;
}
};
class HomographyRansac : public Data<pair<Point2f, Point2f> >{
public:
vector<pair<Point2f, Point2f> > vc;
int minEstNb = 8;
int nbParameters = 4;
HomographyRansac (vector<Point2f> source, vector<Point2f> target){
if (source.size() != target.size()) {
cout << "la taille de target est différente de celle de source, problème" << endl;
}
else {
for (int i = 0 ; i < source.size(); i++) {
vc.push_back(make_pair(source[i], target[i]));
}
}
}
virtual vector<pair<Point2f, Point2f> > getVc(){
return vc;
}
virtual vector<float> estimModel(vector<pair<Point2f, Point2f> > ll){
vector<Point2f> scene1;
vector<Point2f> scene2;
for( int i = 0; i<ll.size(); i++){
scene1.push_back(ll[i].first);
scene2.push_back(ll[i].second);
}
Mat H = findHomography( scene1, scene2);
vector<float> V;
V.assign((float*)H.datastart, (float*)H.dataend);
return V;
}
virtual float calculateError1(pair<Point2f, Point2f> d, vector<float> model){
Mat M2=Mat(2,2,CV_32FC1);
memcpy(M2.data,model.data(),model.size()*sizeof(float));
return norm(M2*Mat(d.first, false) - Mat(d.second,false));
}
virtual float calculateError2(vector<pair<Point2f, Point2f> > dat, vector<float> model){
float S = 0;
int n = dat.size();
for(pair<Point2f, Point2f> p : dat)
S += calculateError1(p, model);
return S/n;
}
};
#endif