-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathframe.cc
221 lines (176 loc) · 5.5 KB
/
frame.cc
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
/*
* Copyright (C) 1997-2017 JdeRobot Developers Team
*
* This program is free software; you can redistribute it and/or modifdisty
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Authors : Eduardo Perdices <[email protected]>
*
*/
#include "./frame.h"
#include "./config.h"
#include "extra/fast_detector.h"
#include "extra/utils.h"
using std::shared_ptr;
using std::vector;
namespace sdvl {
int Frame::counter_ = 0;
Frame::Frame(Camera* camera, ORBDetector * detector, const cv::Mat& img, bool corners) {
id_ = counter_;
kf_id_ = 0;
camera_ = camera;
orb_detector_ = detector;
pyramid_levels_ = Config::PyramidLevels();
pose_ = SE3();
width_ = img.cols;
height_ = img.rows;
is_keyframe_ = false;
selected_ = false;
last_ba_ = -1;
delete_ = false;
// Create Image Pyramid
CreatePyramid(img);
// Create corners
if (corners)
CreateCorners(Config::MaxFastLevels(), Config::NumFeatures());
counter_ += 1;
}
Frame::~Frame() {
RemoveFeatures();
for (auto it=descriptors_.begin(); it != descriptors_.end(); it++)
it->clear();
descriptors_.clear();
}
void Frame::SetKeyframe() {
is_keyframe_ = true;
}
double Frame::GetSceneDepth() {
double z;
vector<double> depth_vec;
for (auto it=features_.begin(); it != features_.end(); it++) {
if (!(*it))
continue;
shared_ptr<Point> point = (*it)->GetPoint();
if (!point)
continue;
// Transform to frame relative coordinates
z = GetRelativePos(point->GetPosition())(2);
depth_vec.push_back(z);
}
if (depth_vec.empty())
return 0.0;
return GetMedianVector(&depth_vec);
}
bool Frame::Project(const Eigen::Vector3d &p3D, Eigen::Vector2d *p2D) {
Eigen::Vector3d rel_p;
rel_p = GetRelativePos(p3D);
if (rel_p(2) < 0.0)
return false; // Point behind camera
camera_->Project(rel_p, p2D);
return true;
}
bool Frame::IsPointVisible(const Eigen::Vector3d &p) {
Eigen::Vector3d rel_p = GetRelativePos(p);
if (rel_p(2) < 0.0)
return false; // Point is behind camera
Eigen::Vector2d image_p;
camera_->Project(rel_p, &image_p);
return camera_->IsInsideImage(image_p.cast<int>());
}
void Frame::CreatePyramid(const cv::Mat& img) {
pyramid_.resize(pyramid_levels_);
pyramid_[0] = img;
// Save each sublevel
for (int i=1; i < pyramid_levels_; i++)
cv::pyrDown(pyramid_[i-1], pyramid_[i], cv::Size(pyramid_[i-1].cols/2, pyramid_[i-1].rows/2));
}
void Frame::CreateCorners(int levels, int nfeatures) {
FastDetector detector(width_, height_, false);
// Get corners for each level
detector.DetectPyramid(pyramid_, &corners_, nfeatures);
// Reserve memory for ORB descriptors
if (Config::UseORB())
descriptors_.resize(corners_.size());
}
void Frame::FilterCorners() {
int index, level;
assert(filtered_corners_.empty());
FastDetector detector(width_, height_);
// Lock cells where we already have features
for (auto it=features_.begin(); it != features_.end(); it++) {
assert(*it != nullptr);
detector.LockCell((*it)->GetPosition());
}
detector.FilterCorners(pyramid_, corners_, &filtered_corners_);
if (Config::UseORB()) {
vector<uchar> desc(32);
// Calc ORB descriptors
for (auto it=filtered_corners_.begin(); it != filtered_corners_.end(); it++) {
index = *it;
Eigen::Vector3i corner = corners_[index];
level = corner(2);
if (descriptors_[index].empty()) {
descriptors_[index].resize(32);
orb_detector_->GetDescriptor(pyramid_[level], Eigen::Vector2i(corner(0), corner(1)), &descriptors_[index]);
}
}
}
}
int Frame::GetNumPoints() const {
int count = 0;
for (auto it=features_.begin(); it != features_.end(); it++) {
if (!(*it))
continue;
shared_ptr<Point> f = (*it)->GetPoint();
if (!f)
continue;
count++;
}
return count;
}
void Frame::AddConnection(const std::pair<std::shared_ptr<Frame>, int> kf) {
connections_.push_back(kf);
}
struct SharedPointsComparator {
bool operator()(const std::pair<shared_ptr<Frame>, int>& left, const std::pair<shared_ptr<Frame>, int>& right) {
return left.second > right.second;
}
};
void Frame::GetBestConnections(vector<shared_ptr<Frame>>* connections, int n) {
int count = 0;
connections->clear();
if (n == 0 || n >= static_cast<int>(connections_.size())) {
// Copy all
for (auto it=connections_.begin(); it != connections_.end(); it++) {
if (!it->first->ToDelete())
connections->push_back(it->first);
}
} else {
// Sort by num points shared
sort(connections_.begin(), connections_.end(), SharedPointsComparator());
for (auto it=connections_.begin(); it != connections_.end() && count < n; it++) {
if (!it->first->ToDelete()) {
connections->push_back(it->first);
count++;
}
}
}
}
void Frame::RemoveFeatures() {
for (auto it=features_.begin(); it != features_.end(); it++) {
*it = nullptr;
}
features_.clear();
}
} // namespace sdvl