-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathcMultiInitializer.cpp
309 lines (263 loc) · 9.09 KB
/
cMultiInitializer.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
/**
* This file is part of MultiCol-SLAM
*
* Copyright (C) 2015-2016 Steffen Urban <urbste at googlemail.com>
* For more information see <https://github.com/urbste/MultiCol-SLAM>
*
* MultiCol-SLAM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MultiCol-SLAM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultiCol-SLAM . If not, see <http://www.gnu.org/licenses/>.
*/
/*
* MultiCol-SLAM is based on ORB-SLAM2 which was also released under GPLv3
* For more information see <https://github.com/raulmur/ORB_SLAM2>
* Raúl Mur-Artal <raulmur at unizar dot es> (University of Zaragoza)
*/
#include "cMultiInitializer.h"
#include "cOptimizer.h"
#include "cConverter.h"
#include <thread>
#include <random>
#include "DBoW2/DUtils/Random.h"
#include "misc.h"
namespace MultiColSLAM
{
cMultiInitializer::cMultiInitializer(const cMultiFrame &ReferenceFrame,
double sigma, int iterations)
{
camSystem = ReferenceFrame.camSystem;
mvKeys1 = ReferenceFrame.mvKeys;
mvKeysRays1 = ReferenceFrame.mvKeysRays;
referenceFrame = ReferenceFrame;
mSigma = sigma;
mSigma2 = sigma * sigma;
mMaxIterations = iterations;
}
bool cMultiInitializer::Initialize(cMultiFrame ¤tFrame,
const std::vector<int> &vMatches12,
cv::Matx33d &R21,
cv::Vec3d &t21,
std::vector<cv::Vec3d> &vP3D,
std::vector<bool> &vbTriangulated,
int& bestCam)
{
// Fill structures with current keypoints and matches with reference frame
// Reference Frame: 1, Current Frame: 2
mvKeys2 = currentFrame.mvKeys;
mvKeysRays2 = currentFrame.mvKeysRays;
mvMatches12.clear();
mvMatches12.reserve(mvKeys2.size());
mvbMatched1.resize(mvKeys1.size());
for (int i = 0, iend = vMatches12.size(); i < iend; i++)
{
if (vMatches12[i] >= 0)
{
mvMatches12.push_back(std::make_pair(i, vMatches12[i]));
mvbMatched1[i] = true;
}
else
mvbMatched1[i] = false;
}
const int N = mvMatches12.size();
// Indices for minimum set selection
std::vector<size_t> vAllIndices;
vAllIndices.reserve(N);
std::vector<size_t> vAvailableIndices;
for (int i = 0; i < N; i++)
vAllIndices.push_back(i);
// pretty easy
// eightpt ransac the problem, then test the median norm of the result
// if it is big enough -> init the system
int nrCams = currentFrame.camSystem.GetNrCams();
std::vector<opengv::bearingVectors_t> bear1(nrCams);
std::vector<opengv::bearingVectors_t> bear2(nrCams);
std::vector<std::vector<int> > bear1_cont_indices(nrCams);
std::vector<std::vector<int> > bear2_cont_indices(nrCams);
// assign matches/bearing vectors to each camera
for (int i = 0; i < N; ++i)
{
int idx1 = mvMatches12[i].first;
int idx2 = mvMatches12[i].second;
// get indices for the corresponding camera
int camidx1 = referenceFrame.keypoint_to_cam.find(idx1)->second;
int camidx2 = currentFrame.keypoint_to_cam.find(idx2)->second;
// save which index belongs to which bearing vector, so that we can recover the
// observations later
bear1_cont_indices[camidx1].push_back(idx1);
bear1[camidx1].push_back(opengv::bearingVector_t(mvKeysRays1[idx1](0),
mvKeysRays1[idx1](1), mvKeysRays1[idx1](2)));
bear2_cont_indices[camidx2].push_back(idx2);
bear2[camidx2].push_back(opengv::bearingVector_t(mvKeysRays2[idx2](0),
mvKeysRays2[idx2](1), mvKeysRays2[idx2](2)));
}
vector<int> nr_recon(nrCams);
vector<cv::Matx33d> Rel_Rs(nrCams);
vector<cv::Vec3d> Rel_ts(nrCams);
vector<vector<bool> > triangulated(nrCams);
vector<vector<cv::Vec3d> > vP3Ds(nrCams);
vector<double> normsAll(nrCams);
// calculate an essential matrix for each camera separately
// ge, 17pt and 6pt are just too slow
// then take the one with the most
// inliers? biggest norm? most reconstructed pts?
for (int c = 0; c < nrCams; ++c)
{
nr_recon[c] = 0;
normsAll[c] = 0;
opengv::relative_pose::CentralRelativeAdapter adapter(bear1[c], bear2[c]);
opengv::sac::Ransac<
opengv::sac_problems::relative_pose::CentralRelativePoseSacProblem > ransac;
std::shared_ptr<
opengv::sac_problems::relative_pose::CentralRelativePoseSacProblem> relposeproblem_ptr(
new opengv::sac_problems::relative_pose::CentralRelativePoseSacProblem(
adapter,
opengv::sac_problems::relative_pose::CentralRelativePoseSacProblem::STEWENIUS));
ransac.sac_model_ = relposeproblem_ptr;
ransac.threshold_ = 0.0001;
ransac.max_iterations_ = 200;
ransac.computeModel();
Eigen::Matrix3d R = ransac.model_coefficients_.block<3, 3>(0, 0);
Eigen::Vector3d t = ransac.model_coefficients_.block<3, 1>(0, 3);
Rel_Rs[c] = cConverter::toCvMat(R);
Rel_ts[c] = cConverter::toCvVec3d(t);
vector<double> norms;
vector<Match> vInlierMatches12;
// recover inlier measurements
for (int i = 0; i < ransac.inliers_.size(); ++i)
{
int idx = ransac.inliers_[i];
int cam_matchidx1 = bear1_cont_indices[c][idx];
int cam_matchidx2 = bear2_cont_indices[c][idx];
vInlierMatches12.push_back(make_pair(cam_matchidx1,
cam_matchidx2));
cv::Vec3d bear1 = mvKeysRays1[cam_matchidx1];
cv::Vec3d bear2 = mvKeysRays2[cam_matchidx2];
cv::Vec3d res = bear1.cross(cConverter::toCvMat(R)*bear2);
norms.push_back(cv::norm(res));
}
if (ransac.inliers_.size() <= 0)
continue;
normsAll[c] = median(norms);
//if (normsAll[c] < 0.02)
// continue;
nr_recon[c] = CheckRT(currentFrame, Rel_Rs[c], Rel_ts[c],
mvKeys1, mvKeys2,
mvKeysRays1, mvKeysRays2, vInlierMatches12,
vP3Ds[c], 5, triangulated[c], 1.0, c);
}
// find cam with most reconstructed points
bool init = false;
for (int c = 0; c < nrCams; ++c)
{
if (nr_recon[c] > 60 &&
normsAll[c] > 0.06)
{
init = true;
bestCam = c;
if (c > 0)
if (normsAll[c] > normsAll[c - 1])
bestCam = c;
}
}
vbTriangulated = triangulated[bestCam];
vP3D = vP3Ds[bestCam];
R21 = Rel_Rs[bestCam];
t21 = Rel_ts[bestCam];
return init;
}
int cMultiInitializer::CheckRT(const cMultiFrame& CurrentFrame,
const cv::Matx33d& R,
const cv::Vec3d& t,
const std::vector<cv::KeyPoint>& vKeys1,
const std::vector<cv::KeyPoint>& vKeys2,
const std::vector<cv::Vec3d>& vKeysRays1,
const std::vector<cv::Vec3d>& vKeysRays2,
const std::vector<Match>& vMatches12,
std::vector<cv::Vec3d>& vP3D,
double th2,
std::vector<bool>& vbGood,
double parallax,
int currCam)
{
double cosThresh = cos(parallax / RHOd);
vbGood = std::vector<bool>(vKeys1.size(), false);
vP3D.resize(vKeys1.size());
std::vector<double> vCosParallax;
vCosParallax.reserve(vKeys1.size());
cv::Vec3d O1(0, 0, 0);
cv::Vec3d O2 = -R.t()*t;
int nGood = 0;
for (size_t i = 0, iend = vMatches12.size(); i < iend; ++i)
{
int currCam1 = CurrentFrame.keypoint_to_cam.find(vMatches12[i].second)->second;
if (currCam1 != currCam)
continue;
const cv::Vec3d &kpRay1 = vKeysRays1[vMatches12[i].first];
const cv::Vec3d &kpRay2 = vKeysRays2[vMatches12[i].second];
const cv::KeyPoint &kp1 = vKeys1[vMatches12[i].first];
const cv::KeyPoint &kp2 = vKeys2[vMatches12[i].second];
cv::Vec3d p3dC1;
p3dC1 = triangulate_point(t, R, kpRay1, kpRay2);
if (!isfinite(p3dC1(0)) || !isfinite(p3dC1(1)) || !isfinite(p3dC1(2)))
{
vbGood[vMatches12[i].first] = false;
continue;
}
// Check parallax
cv::Vec3d normal1 = p3dC1 - O1;
double dist1 = cv::norm(normal1);
cv::Vec3d normal2 = p3dC1 - O2;
double dist2 = cv::norm(normal2);
double cosParallax = normal1.dot(normal2) / (dist1*dist2);
if (p3dC1(2) <= 0 && cosParallax > cosThresh)
continue;
cv::Vec3d p3dC2 = R.t()*(p3dC1 - t);
if (p3dC2(2) <= 0 && cosParallax > cosThresh)
continue;
// Check reprojection error in first image
double u = 0.0;
double v = 0.0;
camSystem.GetCamModelObj(currCam).WorldToImg(
p3dC1(0), p3dC1(1), p3dC1(2), u, v);
double squareError1 = cv::pow(u - cv::saturate_cast<double>(kp1.pt.x), 2) +
cv::pow(v - cv::saturate_cast<double>(kp1.pt.y), 2);
if (squareError1 > th2)
continue;
// Check reprojection error in second image
camSystem.GetCamModelObj(currCam).WorldToImg(
p3dC2(0), p3dC2(1), p3dC2(2), u, v);
double squareError2 = cv::pow(u - cv::saturate_cast<double>(kp2.pt.x), 2) +
cv::pow(v - cv::saturate_cast<double>(kp2.pt.y), 2);
if (squareError2 > th2)
continue;
if (cosParallax < cosThresh)
{
vbGood[vMatches12[i].first] = true;
vCosParallax.push_back(cosParallax);
vP3D[vMatches12[i].first] = p3dC1;
++nGood;
}
}
double minParallax = 0.0;
if (nGood > 0)
{
std::vector<double>::iterator result =
std::min_element(std::begin(vCosParallax), std::end(vCosParallax));
minParallax = acos(*result) * 180 / CV_PI;
}
else
parallax = 0;
if (minParallax < parallax)
nGood = 0;
return nGood;
}
}