-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatcher.cpp
405 lines (322 loc) · 11 KB
/
matcher.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
//
// 1. Store patches rather than reference images.
// 2. Only use projected point when it's reliable?
//
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <opencv2/opencv.hpp>
#include <glog/logging.h>
#include <gflags/gflags.h>
#include <memory>
#include "localmap.h"
#include "hessian.h"
#include "matcher.h"
DECLARE_bool(drawdebug);
typedef HessianTracker FeatureTracker;
typedef FeatureTracker::Pyramid Pyramid;
using namespace cv;
using namespace std;
static const int kWindowSize = 13;
extern int debug;
struct Matcher::Data {
Data() : next_fid(0) {}
struct View {
Frame* frame;
Pyramid pyramid;
Mat original;
};
struct Feature {
TrackedPoint* point;
map<View*, Point2f> matches;
};
struct FeatureCmp {
bool operator()(const unique_ptr<Feature>& a, const unique_ptr<Feature>& b) const {
return a->point->id() < b->point->id();
}
};
typedef set<unique_ptr<Feature>, FeatureCmp> FeatureSet;
// Set of live tracked features.
FeatureSet features;
// views, indexed by camera id.
deque<unique_ptr<View>> views;
int next_fid;
};
typedef Matcher::Data::Feature Feature;
typedef Matcher::Data::View View;
typedef Matcher::Data::Feature Feature;
typedef Matcher::Data::FeatureSet FeatureSet;
namespace {
map<int, deque<Mat>> patches;
} // namespace
const map<int, deque<Mat>>& GetPatches() {
return patches;
}
Matcher::~Matcher() { }
Matcher::Matcher() : data_(new Data) { }
namespace {
template<typename T>
void ShowPatches(
const vector<T>& p1,
const vector<T>& p2,
const vector<T>& p3) {
if (!FLAGS_drawdebug)
return;
const int scale = 15;
cv::Size s = p1[0].size;
Mat out(Size((s.width + 2) * 3, (s.height + 2) * p1.size()), CV_32F);
vector<const vector<T>*> pv;
pv.push_back(&p1);
pv.push_back(&p2);
pv.push_back(&p3);
out = Scalar(0,0,0);
for (int x = 0; x < 3; ++x) {
const auto& p = *pv[x];
for (unsigned int y = 0; y < p.size(); ++y) {
const auto& patch = p[y];
cv::Mat(patch.size.width, patch.size.height, CV_32F, (float*) &(patch.data[0]), patch.size.width * sizeof(float)).
copyTo(out(Rect(x * (s.width + 1), y * (s.height + 1), s.width, s.height)));
}
}
Mat out1;
resize(out, out1, out.size() * scale, 0, 0, INTER_NEAREST);
cv::namedWindow("Diff", CV_WINDOW_AUTOSIZE );// Create a window for display.
cv::moveWindow("Diff", 1440, 1500);
cv::imshow("Diff", out1);
cv::waitKey(0);
}
void AddNewFeatures(const Mat& img, const map<Feature*, Point2f>& matches, vector<Point2f>* result) {
vector<Point2f> corners;
goodFeaturesToTrack(img,
corners,
120, // Max corners.
0.01, // Max quality ratio.
20 // Minimum distance between features.
);
const int size = 30;
int grid[size + 2][size + 2] = {};
for (const auto& m : matches) {
const Point2f& pt = m.second;
int gx = (pt.x / img.size().width) * size + 1;
int gy = (pt.y / img.size().height) * size + 1;
CHECK_LT(0, gx) << pt.x << ", " << pt.y << " : [" << img.size().width << ", " << img.size().height << "\n";
CHECK_LT(0, gy) << pt.x << ", " << pt.y << " : [" << img.size().width << ", " << img.size().height << "\n";
CHECK_GT(size + 2, gx) << pt.x << ", " << pt.y << " : [" << img.size().width << ", " << img.size().height << "\n";
CHECK_GT(size + 2, gy) << pt.x << ", " << pt.y << " : [" << img.size().width << ", " << img.size().height << "\n";
grid[gx-1][gy-1] = 1;
grid[gx-0][gy-1] = 1;
grid[gx+1][gy-1] = 1;
grid[gx-1][gy-0] = 1;
grid[gx-0][gy-0] = 1;
grid[gx+1][gy-0] = 1;
grid[gx-1][gy+1] = 1;
grid[gx-0][gy+1] = 1;
grid[gx+1][gy+1] = 1;
}
int added = 0;
for (const auto& c : corners) {
int gx = (c.x / img.size().width) * size + 1;
int gy = (c.y / img.size().height) * size + 1;
CHECK_LT(0, gx);
CHECK_LT(0, gy);
CHECK_GT(size + 2, gx);
CHECK_GT(size + 2, gy);
if (grid[gx][gy])
continue;
result->push_back(c);
++added;
}
cout << "Added " << added << " new features\n";
}
} // namespace
bool TrackFeature(int id, FeatureTracker* tracker, const View& from, const Point2f& from_pt, const View& to, int lvls, Point2f* to_pt) {
// Forward match.
auto p1 = tracker->GetPatches(from.pyramid, from_pt, lvls);
auto s1 = tracker->TrackFeature(to.pyramid, p1, 0.001, 10, to_pt);
//auto tpt = *to_pt;
// Then reverse match to check.
auto p2 = tracker->GetPatches(to.pyramid, *to_pt, lvls);
Point2f back_pt = from_pt;
auto s2 = tracker->TrackFeature(from.pyramid, p2, 0.001, 10, &back_pt);
//bool good = (norm(from_pt - back_pt) < 0.3);
// Debugging
//printf("%3d: [%7.2f, %7.2f] => [%7.2f, %7.2f] => [%7.2f, %7.2f] => [%7.2f, %7.2f] (%d) %s %s\n",
// id, from_pt.x, from_pt.y, tpt.x, tpt.y, to_pt->x, to_pt->y, back_pt.x, back_pt.y, lvls,
// good ? "Good" : "Bad", (s1 || s2) ? "Lost" : "");
// TODO: Move this to be early exit after debugging finishes.
if (s1 || s2)
return false;
if (debug) {
auto p3 = tracker->GetPatches(from.pyramid, back_pt, lvls);
ShowPatches(p1, p3, p2);
}
// Reject if the reverse differs significantly from the original.
if (norm(from_pt - back_pt) > 0.3) {
return false;
}
return true;
}
void FindMatches(
const FeatureSet& features,
const View& view,
FeatureTracker* tracker,
std::map<Feature*, Point2f>* matches) {
// For each feature, try and propogate forward into this view (if not
// already done so.
// If we're on the second pass, we only looking at projectable points,
// and we're looking doing fine matching.
for (auto& f : features) {
if (matches->count(f.get()))
continue;
for (auto& m : f->matches) {
const auto& from_view = *m.first;
const auto& from_pt = m.second;
const auto& to_view = view;
auto to_pt = from_pt;
int levels = 3;
if (f->point->uncertainty() > 100)
levels = 6;
// Use the projected point location as a starting point in
// searching for a match.
Eigen::Vector2d p;
if (0 || f->point->uncertainty() < 100) {
if (to_view.frame->Project(f->point->location(), &p)) {
to_pt.x = p(0);
to_pt.y = p(1);
}
}
//debug = (f->point->id() == 40);
if (to_pt.x < 0 || to_pt.y < 0 || to_pt.x >= view.original.cols || to_pt.y > view.original.rows) {
continue; // OOBs
}
if (!TrackFeature(f->point->id(), tracker, from_view, from_pt, to_view, levels, &to_pt)) {
if (levels == 6 || !TrackFeature(f->point->id(), tracker, from_view, from_pt, to_view, 6, &to_pt)) {
continue;
}
}
(*matches)[f.get()] = to_pt;
// Add the new observations to the LocalMap.
Vector2d frame_point(to_pt.x, to_pt.y);
to_view.frame->AddObservation(frame_point, f->point);
// Debugging.
cv::Mat patch;
cv::getRectSubPix(to_view.original, Size(kWindowSize, kWindowSize), to_pt, patch);
int id = f->point->id();
patches[id].push_front(patch.clone());
if (patches[id].size() > 30)
patches[id].pop_back();
// TODO: Think about multiple matching here.
break;
}
}
}
// Find points that are duplicates and stop tracking the most recent.
void CleanDuplicates(
std::map<Feature*, Point2f>* matches
) {
std::set<std::pair<int, int>> mask;
for (auto& m : *matches) {
auto p = make_pair(m.second.x/2, m.second.y/2);
if (mask.count(p)) {
m.first->point->set_flag(TrackedPoint::Flags::MISMATCHED);
printf("Removed duplicated point %3d\n", m.first->point->id());
} else {
mask.insert(p);
}
}
}
// BuildView.
// MatchView.
// do slam in here.
// MatchView.
// AddKeyFrame (which may add new features).
// Track features from the previous two frames.
// (There may be an assumption that the previous frames are from alternating
// cameras).
//
// Adds matched features to the localmap.
bool Matcher::Track(const Mat& img, Frame* frame, int camera, LocalMap* map, std::function<bool ()> update_frames) {
auto& d = *data_;
FeatureTracker tracker(Size(kWindowSize, kWindowSize));
CHECK_NE(img.size().width, 0);
CHECK_NE(img.size().height, 0);
CHECK_NOTNULL(map);
CHECK_NOTNULL(frame);
// Build an image pyamid for the new image.
Mat grey;
cvtColor(img, grey, CV_RGB2GRAY);
View* view = new View;
view->frame = frame;
view->pyramid = tracker.MakePyramid(img, 6);
view->original = img.clone();
// For each live tracked point, run over previous matches attempting to
// propogate forward into this view. For a given tracked point, if there
// are multiple matches, we take the best.
//
// We occasionally remove views and all the associated matches.
// Remove bad matches.
for (auto& f : d.features) {
if (!f->point->feature_usable())
d.features.erase(f);
}
// matches propogated forward into the current view.
std::map<Feature*, Point2f> matches;
FindMatches(d.features, *view, &tracker, &matches);
int before = matches.size();
if (0 || matches.size() < 40) {
if (update_frames != nullptr) {
if (update_frames()) {
// Inferred position of Frame* has been updated, so try again for matching points.
FindMatches(d.features, *view, &tracker, &matches);
}
}
}
printf("Started with %d, grew to %d after additional matching\n", before, (int) matches.size());
//CleanDuplicates(&matches);
// If there are insufficient trackable features, add new features from the
// current image that aren't too close to an existing feature.
// TODO: Lift out constant.
if (matches.size() >= 40)
return true;
// New keyframe, so add the matches permanantly.
view->frame->is_keyframe_ = true;
for (auto& m : matches) {
Feature* f = m.first;
const auto& pt = m.second;
f->matches[view] = pt;
}
d.views.push_back(unique_ptr<View>(view));
// Possibly add new features.
vector<Point2f> added;
AddNewFeatures(grey, matches, &added);
printf("Adding new keyframe for camera %d on frame %d (added %d)\n", camera, frame->id(), (int)added.size());
for (auto& pt : added) {
Vector2d frame_point(pt.x, pt.y);
// Add a new TrackedPoint to the local map.
// Use Frame::Unproject to initialize the world space location of the tracked point.
Feature* f = new Feature;
Vector2d plane_pt = view->frame->camera()->PixelToPlane(frame_point);
// 2500 == Initial guess at distance.
auto location = view->frame->Unproject(plane_pt, 2000);
f->point = map->AddPoint(d.next_fid++, location);
view->frame->AddObservation(frame_point, f->point);
f->matches[view] = pt;
d.features.insert(unique_ptr<Feature>(f));
// Debugging.
cv::Mat patch;
cv::getRectSubPix(view->original, Size(kWindowSize, kWindowSize), pt, patch);
int id = f->point->id();
patches[id].push_front(patch.clone());
if (patches[id].size() > 30)
patches[id].pop_back();
}
// Potentially remove an old view.
if (d.views.size() > 4) {
View* view = d.views.front().get();
for (auto& f : d.features)
f->matches.erase(view);
d.views.pop_front();
}
return true;
}