-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearData.cpp
86 lines (60 loc) · 2.01 KB
/
LinearData.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
84
85
86
//
// LinearData.cpp
// projet
//
// Created by Etienne Dejoie on 05/01/2016.
//
//
using namespace std;
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]));
}
}
}
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
}
float calculateError(pair<float, float> d, vector<float> model){
return pow(d.second - model[0]*d.first - model[1], 2);
}
float calculateError(vector<pair<float,float> > dat, vector<float> model){
float S = 0;
int n = dat.size();
for(pair<float,float> p : dat)
S += calculateError(p, model);
return S/n;
}
};