-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEstimators.h
345 lines (305 loc) · 10.9 KB
/
Estimators.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#ifndef HOMOGRAPHY_EST_ESTIMATORS_H_
#define HOMOGRAPHY_EST_ESTIMATORS_H_
#include <vector>
#include <Eigen/Eigen>
#include "hest.h"
namespace hest {
Eigen::Matrix3d dlt_homography(
const std::vector<Eigen::Vector3d> &p1,
const std::vector<Eigen::Vector3d> &p2);
Eigen::Matrix3d est_rotation(
const std::vector<Eigen::Vector2d> &p1,
const std::vector<Eigen::Vector2d> &p2,
const std::vector<LineSegment> &ls1,
const std::vector<LineSegment> &ls2);
// Maps the endpoints and computes the point-to-line error in the first image
double computeHomographyLineResidual(
const LineSegment &line_segment1,
const LineSegment &line_segment2,
const Eigen::Matrix3d &H);
// Maps the endpoints and computes the point-to-line error in the first image
double computeRotationLineResidual(
const LineSegment &line_segment1,
const LineSegment &line_segment2,
const Eigen::Matrix3d &R);
double computeHomographyPointResidual(
const Eigen::Vector2d &p1,
const Eigen::Vector2d &p2,
const Eigen::Matrix3d &H);
double computeRotationPointResidual(
const Eigen::Vector2d &p1,
const Eigen::Vector2d &p2,
const Eigen::Matrix3d &R);
class LineHomographyEstimator {
public:
LineHomographyEstimator(const std::vector<LineSegment> &ls1,
const std::vector<LineSegment> &ls2,
const bool rot)
: line_segments1(ls1), line_segments2(ls2), pure_rotation(rot) {}
inline int min_sample_size() const {
if (pure_rotation) {
return 2;
} else {
return 4;
}
}
inline int non_minimal_sample_size() const { return 4; }
inline int num_data() const { return line_segments1.size(); }
int MinimalSolver(const std::vector<int> &sample,
std::vector<Eigen::Matrix3d> *H) const {
std::vector<LineSegment> ls1, ls2;
ls1.resize(sample.size());
ls2.resize(sample.size());
for (size_t k = 0; k < sample.size(); ++k) {
ls1[k] = line_segments1[sample[k]];
ls2[k] = line_segments2[sample[k]];
}
H->clear();
H->push_back(estimateHomographyLineSegments(ls1, ls2, pure_rotation));
return 1;
}
// Returns 0 if no model could be estimated and 1 otherwise.
// Implemented by a simple linear least squares solver.
int NonMinimalSolver(const std::vector<int> &sample,
Eigen::Matrix3d *H) const {
std::vector<LineSegment> ls1, ls2;
ls1.resize(sample.size());
ls2.resize(sample.size());
for (size_t k = 0; k < sample.size(); ++k) {
ls1[k] = line_segments1[sample[k]];
ls2[k] = line_segments2[sample[k]];
}
*H = estimateHomographyLineSegments(ls1, ls2, pure_rotation);
return 1;
}
// Evaluates the line on the i-th data point.
double EvaluateModelOnPoint(const Eigen::Matrix3d &H, int i) const {
double res;
if (pure_rotation) {
res = computeRotationLineResidual(line_segments1[i], line_segments2[i], H);
} else {
res = computeHomographyLineResidual(line_segments1[i], line_segments2[i], H);
}
return res * res;
}
// Linear least squares solver. Calls NonMinimalSolver.
inline void LeastSquares(const std::vector<int> &sample,
Eigen::Matrix3d *H) const {
std::vector<LineSegment> ls1, ls2;
ls1.resize(sample.size());
ls2.resize(sample.size());
for (size_t k = 0; k < sample.size(); ++k) {
ls1[k] = line_segments1[sample[k]];
ls2[k] = line_segments2[sample[k]];
}
refineHomography(ls1, ls2, *H, pure_rotation);
}
protected:
// Matrix holding the 2D points through which the line is fitted.
const std::vector<LineSegment> &line_segments1;
const std::vector<LineSegment> &line_segments2;
const bool pure_rotation;
};
class PointHomographyEstimator {
public:
PointHomographyEstimator(const std::vector<Eigen::Vector2d> &points1,
const std::vector<Eigen::Vector2d> &points2,
const bool rot)
: pts1(points1), pts2(points2), pure_rotation(rot) {}
inline int min_sample_size() const {
if (pure_rotation) {
return 2;
} else {
return 4;
}
}
inline int non_minimal_sample_size() const {
return 4;
}
inline int num_data() const { return pts1.size(); }
int MinimalSolver(const std::vector<int> &sample,
std::vector<Eigen::Matrix3d> *H) const {
std::vector<Eigen::Vector2d> ps1, ps2;
ps1.resize(sample.size());
ps2.resize(sample.size());
for (size_t k = 0; k < sample.size(); ++k) {
ps1[k] = pts1[sample[k]];
ps2[k] = pts2[sample[k]];
}
H->clear();
H->push_back(estimateHomographyPoints(ps1, ps2, pure_rotation));
return 1;
}
// Returns 0 if no model could be estimated and 1 otherwise.
// Implemented by a simple linear least squares solver.
int NonMinimalSolver(const std::vector<int> &sample,
Eigen::Matrix3d *H) const {
std::vector<Eigen::Vector2d> ps1, ps2;
ps1.resize(sample.size());
ps2.resize(sample.size());
for (size_t k = 0; k < sample.size(); ++k) {
ps1[k] = pts1[sample[k]];
ps2[k] = pts2[sample[k]];
}
*H = estimateHomographyPoints(ps1, ps2, pure_rotation);
return 1;
}
// Evaluates the line on the i-th data point.
double EvaluateModelOnPoint(const Eigen::Matrix3d &H, int i) const {
double res;
if (pure_rotation) {
res = computeRotationPointResidual(pts1[i], pts2[i], H);
} else {
res = computeHomographyPointResidual(pts1[i], pts2[i], H);
}
return res * res;
}
// Linear least squares solver. Calls NonMinimalSolver.
inline void LeastSquares(const std::vector<int> &sample,
Eigen::Matrix3d *H) const {
std::vector<Eigen::Vector2d> ps1, ps2;
ps1.resize(sample.size());
ps2.resize(sample.size());
for (size_t k = 0; k < sample.size(); ++k) {
ps1[k] = pts1[sample[k]];
ps2[k] = pts2[sample[k]];
}
refineHomography(ps1, ps2, *H, pure_rotation);
}
protected:
const std::vector<Eigen::Vector2d> &pts1;
const std::vector<Eigen::Vector2d> &pts2;
const bool pure_rotation;
};
class PointLineHomographyEstimator {
public:
PointLineHomographyEstimator(const std::vector<Eigen::Vector2d> &points1,
const std::vector<Eigen::Vector2d> &points2,
const std::vector<LineSegment> &ls1,
const std::vector<LineSegment> &ls2,
const bool rot)
: pts1(points1), pts2(points2), line_segments1(ls1), line_segments2(ls2), pure_rotation(rot) {}
inline int num_minimal_solvers() const {
if (pure_rotation) {
return 3;
} else {
return 2;
}
}
void min_sample_sizes(std::vector<std::vector<int>> *min_sample_sizes) const {
if (pure_rotation) {
*min_sample_sizes = {{2, 0}, {1, 1}, {0, 2}};
} else {
*min_sample_sizes = {{4, 0}, {0, 4}};
}
}
int num_data_types() const { return 2; }
void num_data(std::vector<int> *num_data) const {
*num_data = {static_cast<int>(pts1.size()), static_cast<int>(line_segments1.size())};
}
void solver_probabilities(std::vector<double> *solver_probabilites) const {
const float n_samples = pts1.size() + line_segments1.size();
const float ratio_pts = pts1.size() / n_samples;
const float ratio_lines = line_segments1.size() / n_samples;
if (pure_rotation) {
// Probs should be proportional to the number of features of that type: Ej: 70%p , 30%l
// pp: 0.7 * 0.7 = 0.49 ; pl: 0.7 * 0.3 + 0.3 * 0.7 = 0.42 ; ll: 0.3 * 0.3 = 0.09
*solver_probabilites = {ratio_pts * ratio_pts,
2 * ratio_pts * ratio_lines,
ratio_lines * ratio_lines};
} else {
*solver_probabilites = {ratio_pts, ratio_lines};
}
}
int MinimalSolver(const std::vector<std::vector<int>> &sample,
const int solver_idx, std::vector<Eigen::Matrix3d> *models) const {
models->clear();
std::vector<Eigen::Vector2d> ps1, ps2;
ps1.resize(sample[0].size());
ps2.resize(sample[0].size());
for (size_t k = 0; k < sample[0].size(); ++k) {
ps1[k] = pts1[sample[0][k]];
ps2[k] = pts2[sample[0][k]];
}
std::vector<LineSegment> ls1, ls2;
ls1.resize(sample[1].size());
ls2.resize(sample[1].size());
for (size_t k = 0; k < sample[1].size(); ++k) {
ls1[k] = line_segments1[sample[1][k]];
ls2[k] = line_segments2[sample[1][k]];
}
if (pure_rotation) {
models->push_back(est_rotation(ps1, ps2, ls1, ls2));
} else {
if (solver_idx == 0) {
// Solve with 4 points
models->push_back(estimateHomographyPoints(ps1, ps2, false));
} else {
// Solve with 4 lines
models->push_back(estimateHomographyLineSegments(ls1, ls2, false));
}
}
return models->size();
}
double EvaluateModelOnPoint(const Eigen::Matrix3d &H, int t, int i) const {
double res;
if (t == 0) {
// point
if (pure_rotation) {
res = computeRotationPointResidual(pts1[i], pts2[i], H);
} else {
res = computeHomographyPointResidual(pts1[i], pts2[i], H);
}
} else {
// line
if (pure_rotation) {
res = computeRotationLineResidual(line_segments1[i], line_segments2[i], H);
} else {
res = computeHomographyLineResidual(line_segments1[i], line_segments2[i], H);
}
}
return res * res;
}
void LeastSquares(const std::vector<std::vector<int>> &sample,
Eigen::Matrix3d *H) const {
// Collect point lines
std::vector<Eigen::Vector2d> ps1, ps2;
ps1.resize(sample[0].size());
ps2.resize(sample[0].size());
for (size_t k = 0; k < sample[0].size(); ++k) {
ps1[k] = pts1[sample[0][k]];
ps2[k] = pts2[sample[0][k]];
}
// Collect inlier lines
std::vector<LineSegment> ls1, ls2;
ls1.resize(sample[1].size());
ls2.resize(sample[1].size());
for (size_t k = 0; k < sample[1].size(); ++k) {
ls1[k] = line_segments1[sample[1][k]];
ls2[k] = line_segments2[sample[1][k]];
}
refineHomography(ps1, ps2, ls1, ls2, *H, pure_rotation);
}
void get_weights(std::vector<std::vector<double>> &dst_weights) const {
dst_weights.resize(2);
// Reserve memory
dst_weights[0].resize(pts1.size());
dst_weights[1].resize(line_segments1.size());
// Uniform distribution for the points
for (size_t i = 0; i < pts1.size(); i++) {
dst_weights[0][i] = 1;
}
// Distribution proportional to the Sqrt of the length for the lines
for (size_t i = 0; i < line_segments1.size(); i++) {
dst_weights[1][i] = std::sqrt((line_segments1[i].p2 - line_segments1[i].p1).norm());
}
}
protected:
const std::vector<Eigen::Vector2d> &pts1;
const std::vector<Eigen::Vector2d> &pts2;
const std::vector<LineSegment> &line_segments1;
const std::vector<LineSegment> &line_segments2;
const bool pure_rotation;
};
}
#endif //HOMOGRAPHY_EST_ESTIMATORS_H_