-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTracker.cpp
477 lines (384 loc) · 15.6 KB
/
Tracker.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include <g2o/core/block_solver.h>
#include <g2o/solvers/dense/linear_solver_dense.h>
#include <g2o/core/optimization_algorithm_levenberg.h>
#include <opencv2/opencv.hpp>
#include <glog/logging.h>
#include "Tracker.h"
#include "Config.h"
#include "Mappoint.h"
#include "g2o_types.h"
namespace stereo_vo {
Tracker::Tracker():
state_(NOT_INITIALIZED){
// Read parameters
num_features_ = static_cast<size_t>(Config::Read<int>("num_features"));
min_num_features_init_ = static_cast<size_t>(Config::Read<int>("min_num_features_init"));
min_num_features_tracking_ = static_cast<size_t>(Config::Read<int>("num_features_tracking"));
num_features_tracking_bad_ = static_cast<size_t>(Config::Read<int>("num_features_tracking_bad"));
num_features_for_new_keyframe_ = static_cast<size_t>(Config::Read<int>("num_features_for_new_keyframe"));
// Create feature extractor
gftt_ = cv::GFTTDetector::create(num_features_, 0.01, 20);
}
bool Tracker::AddStereoFrame(Frame::Ptr frame) {
current_frame_ = frame;
bool success = false;
if (state_ == NOT_INITIALIZED) {
success = StereoInit();
}
else if (state_ == GOOD || state_ == BAD) {
success = Track();
}
else {
LOG(INFO) << "Resetting...\n";
Reset();
success = false;
}
last_frame_ = current_frame_;
return success;
}
bool Tracker::StereoInit() {
// Extract features in the left image
size_t num_features = ExtractFeatures();
LOG(INFO) << "Extract " << num_features << " new features\n";
// Find correspondences in the right image
size_t num_correspondences = FindCorrespondences();
if (num_correspondences < min_num_features_init_) {
LOG(WARNING) << "Not enough correspondences!\n";
return false;
}
// Initialize the map
if (MapInit()) {
LOG(INFO) << "Map is initialized\n";
state_ = GOOD;
if (visualizer_) {
LOG(INFO) << "Starting visualizer...\n";
visualizer_->AddCurrentFrame(current_frame_);
visualizer_->UpdateMap();
}
return true;
}
return false;
}
size_t Tracker::ExtractFeatures() {
// Extract keypoints in the left image
std::vector<cv::KeyPoint> keypoints;
gftt_->detect(current_frame_->image_left_, keypoints);
// Create features
for (cv::KeyPoint &kp : keypoints) {
current_frame_->features_left_.push_back(
Feature::Ptr(new Feature(current_frame_, kp, true)));
}
return current_frame_->features_left_.size();
}
size_t Tracker::FindCorrespondences() {
std::vector<cv::Point2f> pxs_left, pxs_right;
int num_features = current_frame_->features_left_.size();
pxs_left.reserve(num_features);
pxs_right.reserve(num_features);
// Get keypoints in the left and right images
for (const Feature::Ptr feature: current_frame_->features_left_) {
pxs_left.push_back(feature->kp_.pt);
Mappoint::Ptr mappoint = feature->mappoint_.lock();
if (mappoint) {
// Use projected pixel as the initial guess
Eigen::Vector2f px = camera_right_->world2pixel(mappoint->GetPosition(), current_frame_->GetPose());
pxs_right.push_back(cv::Point2f(px[0], px[1]));
}
else {
// Same pixel in the left image
pxs_right.push_back(feature->kp_.pt);
}
}
// Use optical flow to compute correspondences
std::vector<uchar> status;
cv::Mat error;
cv::calcOpticalFlowPyrLK(
current_frame_->image_left_, current_frame_->image_right_, pxs_left,
pxs_right, status, error, cv::Size(11, 11), 3,
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 30, 0.01),
cv::OPTFLOW_USE_INITIAL_FLOW);
// LOG(INFO) << "# candidate correspondences: " << pxs_right.size() << '\n';
// Compute the number of correspondence inliers
size_t num_inliers = 0;
for (size_t i = 0; i < status.size(); ++i) {
// correspondence
if (status[i]) {
Feature::Ptr feature_right (new Feature(current_frame_, cv::KeyPoint(pxs_right[i], 7), false));
current_frame_->features_right_.push_back(feature_right);
++num_inliers;
}
// no correspondence
else {
current_frame_->features_right_.push_back(nullptr);
}
}
LOG(INFO) << "Find " << num_inliers << " correspondences in the right image\n";
return num_inliers;
}
bool Tracker::MapInit() {
std::vector<Sophus::SE3f> poses{camera_left_->GetPose(), camera_right_->GetPose()};
size_t num_initial_points = 0;
for (size_t i = 0; i < current_frame_->features_left_.size(); ++i) {
Feature::Ptr feature_left = current_frame_->features_left_[i];
Feature::Ptr feature_right = current_frame_->features_right_[i];
// no correspondences
if (feature_right == nullptr) {
continue;
}
// Create mappoints using triangulation
Eigen::Vector2f px (feature_left->kp_.pt.x,
feature_left->kp_.pt.y);
Eigen::Vector2f px2 (feature_right->kp_.pt.x,
feature_right->kp_.pt.y);
std::vector<Eigen::Vector3f> points;
points.push_back(camera_left_->pixel2cam(px));
points.push_back(camera_right_->pixel2cam(px2));
Eigen::Vector3f p_w = Eigen::Vector3f::Zero();
if (Triangulate(poses, points, p_w) && p_w[2] > 0) {
// Create a mappoint
Mappoint::Ptr point (new Mappoint(p_w.cast<float>()));
point->AddObservation(feature_left);
point->AddObservation(feature_right);
feature_left->mappoint_ = point;
feature_right->mappoint_ = point;
++num_initial_points;
// Add the mappoint to the map
map_->InsertMappoint(point);
}
}
current_frame_->SetKeyframe();
map_->InsertKeyframe(current_frame_);
optimizer_->UpdateMap();
// LOG(INFO) << "# initial mappoints: " << num_initial_points << '\n';
// LOG(INFO) << "# initial keyframes: " << map_->GetAllKeyframes().size() <<'\n';
return true;
}
bool Tracker::Triangulate(const std::vector<Sophus::SE3f> &poses,
const std::vector<Eigen::Vector3f> points, Eigen::Vector3f &pt_world) {
Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> A(2 * poses.size(), 4);
Eigen::Matrix<float, Eigen::Dynamic, 1> b(2 * poses.size());
b.setZero();
// Construct Ax = b
for (size_t i = 0; i < poses.size(); ++i) {
Eigen::Matrix<float, 3, 4> m = poses[i].matrix3x4();
A.block<1, 4>(2 * i, 0) = points[i][0] * m.row(2) - m.row(0);
A.block<1, 4>(2 * i + 1, 0) = points[i][1] * m.row(2) - m.row(1);
}
auto svd = A.bdcSvd(Eigen::ComputeThinU | Eigen::ComputeThinV);
pt_world = (svd.matrixV().col(3) / svd.matrixV()(3, 3)).head<3>();
if (svd.singularValues()[3] / svd.singularValues()[2] < 1e-2) {
return true;
}
// Solution is not good enough
return false;
}
bool Tracker::Track() {
if (last_frame_) {
current_frame_->SetPose(relative_motion_ * last_frame_->GetPose());
}
size_t num_features_tracked = TrackLastFrame();
num_tracking_inliers_ = EstimatePose();
LOG(INFO) << "# inliers: " << num_tracking_inliers_ << '\n';
if (num_tracking_inliers_ > min_num_features_tracking_) {
// tracking good
state_ = GOOD;
} else if (num_tracking_inliers_ > num_features_tracking_bad_) {
// tracking bad
state_ = BAD;
} else {
// lost
state_ = LOST;
}
if (visualizer_) {
visualizer_->AddCurrentFrame(current_frame_);
}
InsertKeyframe();
relative_motion_ = current_frame_->GetPose() * last_frame_->GetPose().inverse();
return true;
}
size_t Tracker::TrackLastFrame() {
std::vector<cv::Point2f> pxs1, pxs2;
int num_features = last_frame_->features_left_.size();
pxs1.reserve(num_features);
pxs2.reserve(num_features);
// LOG(INFO) << "Getting features...\n";
// Get keypoints in the last and current frames
size_t i = 0;
for (const Feature::Ptr feature: last_frame_->features_left_) {
pxs1.push_back(feature->kp_.pt);
Mappoint::Ptr mappoint = feature->mappoint_.lock();
if (mappoint) {
// Use projected pixel as the initial guess
Eigen::Vector2f px = camera_left_->world2pixel(mappoint->GetPosition(), current_frame_->GetPose());
pxs2.push_back(cv::Point2f(px[0], px[1]));
}
else {
// Same pixel in the left image
pxs2.push_back(feature->kp_.pt);
}
++i;
}
// LOG(INFO) << "Using optical flow...\n";
// Use optical flow to compute correspondences
std::vector<uchar> status;
cv::Mat error;
cv::calcOpticalFlowPyrLK(
last_frame_->image_left_, current_frame_->image_left_, pxs1,
pxs2, status, error, cv::Size(11, 11), 3,
cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 30, 0.01),
cv::OPTFLOW_USE_INITIAL_FLOW);
// LOG(INFO) << "# candidate correspondences in the current frame: " << pxs2.size() << '\n';
// Compute the number of correspondence inliers
size_t num_inliers = 0;
for (size_t i = 0; i < status.size(); ++i) {
// correspondence
if (status[i]) {
cv::KeyPoint kp(pxs2[i], 7);
Feature::Ptr feature_current (new Feature(current_frame_, kp, false));
feature_current->mappoint_ = last_frame_->features_left_[i]->mappoint_;
current_frame_->features_left_.push_back(feature_current);
++num_inliers;
}
}
LOG(INFO) << "Find " << num_inliers << " correspondences in the current image.\n";
return num_inliers;
}
size_t Tracker::EstimatePose() {
// Setup g2o optimizer
typedef g2o::BlockSolver_6_3 BlockSolverType;
typedef g2o::LinearSolverDense<BlockSolverType::PoseMatrixType> LinearSolverType;
g2o::OptimizationAlgorithmLevenberg *solver = new g2o::OptimizationAlgorithmLevenberg (
g2o::make_unique<BlockSolverType>(
g2o::make_unique<LinearSolverType>()
)
);
g2o::SparseOptimizer optimizer;
optimizer.setAlgorithm(solver);
// LOG(INFO) << "Initial pose:\n" << current_frame_->GetPose().matrix() << '\n';
// Set up the pose vertex
VertexPose *vertex_pose = new VertexPose();
vertex_pose->setId(0);
vertex_pose->setEstimate(current_frame_->GetPose().cast<double>());
optimizer.addVertex(vertex_pose);
// Set up unary edges
std::vector<EdgeProjectionPoseOnly*> edges;
Eigen::Matrix3d K = camera_left_->GetK().cast<double>();
std::vector<Feature::Ptr> features;
for (size_t i = 0; i < current_frame_->features_left_.size(); ++i) {
Mappoint::Ptr mappoint = current_frame_->features_left_[i]->mappoint_.lock();
if (mappoint) {
features.push_back(current_frame_->features_left_[i]);
EdgeProjectionPoseOnly *edge = new EdgeProjectionPoseOnly(
(mappoint->GetPosition()).cast<double>(), K
);
edge->setId(i);
edge->setVertex(0, vertex_pose);
edge->setMeasurement(
Eigen::Vector2d(current_frame_->features_left_[i]->kp_.pt.x,
current_frame_->features_left_[i]->kp_.pt.y)
);
edge->setInformation(Eigen::Matrix2d::Identity());
edge->setRobustKernel(new g2o::RobustKernelHuber());
optimizer.addEdge(edge);
edges.push_back(edge);
}
}
// LOG(INFO) << "# features: " << features.size() << '\n';
// LOG(INFO) << "Counting outliers...\n";
// Count the number of inliers
const double chi2_th = 5.991; // dof = 2 alpha = 0.05
size_t num_outliers = 0;
for (int iter = 0; iter < 4; ++iter) {
vertex_pose->setEstimate(current_frame_->GetPose().cast<double>());
optimizer.initializeOptimization();
optimizer.optimize(10);
num_outliers = 0;
for (size_t i = 0; i < edges.size(); ++i) {
EdgeProjectionPoseOnly *e = edges[i];
if (features[i]->is_outlier_) {
e->computeError();
}
// outlier
if (e->chi2() > chi2_th) {
features[i]->is_outlier_ = true;
e->setLevel(1); // not optimized
++num_outliers;
}
// inlier
else {
features[i]->is_outlier_ = false;
e->setLevel(0); // optimized
}
if (iter == 2) {
e->setRobustKernel(nullptr);
}
}
}
LOG(INFO) << "Outlier/Inlier in pose estimation: " << num_outliers << "/" << features.size() - num_outliers;
current_frame_->SetPose(vertex_pose->estimate().cast<float>());
LOG(INFO) << "Current pose:\n" << current_frame_->GetPose().matrix() << '\n';
for (Feature::Ptr feat: features) {
if (feat->is_outlier_) {
feat->mappoint_.reset();
feat->is_outlier_ = false; // maybe we can still use it in future
}
}
assert(num_outliers < features.size());
return features.size() - num_outliers;
}
bool Tracker::InsertKeyframe() {
if (num_tracking_inliers_ >= num_features_for_new_keyframe_) {
// Enough inliers
return false;
}
// Add the current frame as the new keyframe
current_frame_->SetKeyframe();
map_->InsertKeyframe(current_frame_);
LOG(INFO) << "Setting frame " << current_frame_->GetId() << " as new keyframe "
<< current_frame_->GetKeyframeId() << '\n';
// Track in the right image to create more mappoints
ExtractFeatures();
FindCorrespondences();
TriangulateNewPoints();
optimizer_->UpdateMap();
if (visualizer_) {
visualizer_->UpdateMap();
}
return true;
}
size_t Tracker::TriangulateNewPoints() {
std::vector<Sophus::SE3f> poses {camera_left_->GetPose(), camera_right_->GetPose()};
Sophus::SE3f T_wc = current_frame_->GetPose().inverse();
size_t num_new_mappoints = 0;
for (size_t i = 0; i < current_frame_->features_left_.size(); ++i) {
Feature::Ptr feature_left = current_frame_->features_left_[i];
Feature::Ptr feature_right = current_frame_->features_right_[i];
if (feature_left->mappoint_.expired() && feature_right != nullptr) {
// No matching mappoint but has a correspondence
// Triangulate to create a mappoint
std::vector<Eigen::Vector3f> points {
camera_left_->pixel2cam(Eigen::Vector2f(feature_left->kp_.pt.x, feature_left->kp_.pt.y)),
camera_right_->pixel2cam(Eigen::Vector2f(feature_right->kp_.pt.x, feature_right->kp_.pt.y))
};
Eigen::Vector3f p_w = Eigen::Vector3f::Zero();
if (Triangulate(poses, points, p_w) && p_w[2] > 0) {
// Transform to world frame
p_w = T_wc * p_w;
// Create a mappoint
Mappoint::Ptr mappoint (new Mappoint(p_w));
mappoint->AddObservation(feature_left);
mappoint->AddObservation(feature_right);
feature_left->mappoint_ = mappoint;
feature_right->mappoint_ = mappoint;
map_->InsertMappoint(mappoint);
++num_new_mappoints;
}
}
}
LOG(INFO) << "# new triangulated mappoints: " << num_new_mappoints << '\n';
return num_new_mappoints;
}
void Tracker::Reset() {
// not implemented
}
} // namespace stereo_vo