-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbal_g2o.cpp
242 lines (191 loc) · 7.09 KB
/
bal_g2o.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
#include <g2o/core/base_vertex.h>
#include <g2o/core/base_binary_edge.h>
#include <g2o/core/block_solver.h>
#include <g2o/core/optimization_algorithm_levenberg.h>
#include <g2o/solvers/csparse/linear_solver_csparse.h>
#include <g2o/core/robust_kernel_impl.h>
#include <iostream>
#include <sophus/se3.hpp>
#include "common.h"
using namespace Sophus;
using namespace Eigen;
using namespace std;
struct Pose
{
Pose() {}
explicit Pose(double *data)
{
rotation = SO3d::exp(Vector3d(data[0], data[1], data[2]));
translation = Vector3d(data[3], data[4], data[5]);
focus = data[6];
k1 = data[7];
k2 = data[8];
}
void set_to(double *data)
{
auto r = rotation.log();
for(int i = 0; i < 3; ++i) data[i] = r[i];
for(int i = 0; i < 3; ++i) data[i+3] = translation[i];
data[6] = focus;
data[7] = k1;
data[8] = k2;
}
SO3d rotation;
Vector3d translation = Vector3d::Zero();
double focus = 0;
double k1 = 0, k2 = 0;
};
class PoseVertex : public g2o::BaseVertex<9, Pose>
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
PoseVertex() {}
virtual void setToOriginImpl() override
{
_estimate = Pose();
}
virtual void oplusImpl(const double *update) override
{
_estimate.rotation = SO3d::exp(Vector3d(update[0], update[1], update[2])) * _estimate.rotation;
_estimate.translation += Vector3d(update[3], update[4], update[5]);
_estimate.focus += update[6];
_estimate.k1 += update[7];
_estimate.k2 += update[8];
}
Vector2d project(const Vector3d &point)
{
Vector3d pc = _estimate.rotation * point + _estimate.translation;
pc = -pc / pc[2];
double r2 = pc.squaredNorm();
double distortion = 1.0 + r2 * (_estimate.k1 + _estimate.k2 * r2);
return Vector2d(_estimate.focus * distortion * pc[0], _estimate.focus * distortion * pc[1]);
}
virtual bool read(istream &in) {}
virtual bool write(ostream &out) const {}
};
class PointVertex : public g2o::BaseVertex<3, Vector3d>
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
PointVertex() {}
virtual void setToOriginImpl() override
{
_estimate = Vector3d(0,0,0);
}
virtual void oplusImpl(const double *update) override
{
_estimate += Vector3d(update[0], update[1], update[2]);
}
virtual bool read(istream &in) {}
virtual bool write(ostream &out) const {}
};
class Edge : public g2o::BaseBinaryEdge<2, Vector2d, PoseVertex, PointVertex>
{
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
virtual void computeError() override
{
auto v0 = (PoseVertex *) _vertices[0];
auto v1 = (PointVertex *) _vertices[1];
auto proj = v0->project(v1->estimate());
_error = proj - _measurement;
}
virtual bool read(istream &in) {}
virtual bool write(ostream &out) const {}
};
void SolveBA(BALProblem &bal_problem);
int main(int argc, char **argv)
{
if(argc != 2)
{
std::cout << "Error" << std::endl;
return 1;
}
BALProblem bal_problem(argv[1]);
bal_problem.Normalize();
bal_problem.Perturb(0.1, 0.5, 0.5);
bal_problem.WriteToPLYFile("initial.ply");
SolveBA(bal_problem);
bal_problem.WriteToPLYFile("final.ply");
return 0;
}
void SolveBA(BALProblem &bal_problem)
{
const int point_block_size = bal_problem.point_block_size();
const int camera_block_size = bal_problem.camera_block_size();
double *points = bal_problem.mutable_points();
double *cameras = bal_problem.mutable_cameras();
typedef g2o::BlockSolver<g2o::BlockSolverTraits<9, 3>> BlockSolverType;
typedef g2o::LinearSolverCSparse<BlockSolverType::PoseMatrixType> LinearSolverType;
auto solver = new g2o::OptimizationAlgorithmLevenberg(g2o::make_unique<BlockSolverType>(g2o::make_unique<LinearSolverType>()));
g2o::SparseOptimizer optimizer;
optimizer.setAlgorithm(solver);
optimizer.setVerbose(true);
const double *observations = bal_problem.observations();
vector<PoseVertex *> pose_vertexs;
vector<PointVertex *> point_vertexs;
// std::cout << "num_cameras: " << bal_problem.num_cameras() << " " << "\tnum_points: " << bal_problem.num_points();
// std::cout << "\tnum_observations: " << bal_problem.num_observations() << std::endl;
std::cout << "***************************** Solved by G2O *****************************\n";
for(int i = 0; i < bal_problem.num_cameras(); ++i)
{
PoseVertex *v = new PoseVertex();
double *camera = cameras + camera_block_size * i;
v->setId(i);
v->setEstimate(Pose(camera));
optimizer.addVertex(v);
pose_vertexs.push_back(v);
}
for(int i = 0; i < bal_problem.num_points(); ++i)
{
PointVertex *v = new PointVertex();
double *point = points + point_block_size * i;
v->setId(i + bal_problem.num_cameras());
v->setEstimate(Vector3d(point[0], point[1], point[2]));
v->setMarginalized(true);
v->setFixed(true);
optimizer.addVertex(v);
point_vertexs.push_back(v);
}
for(int i = 0; i < bal_problem.num_observations(); ++i)
{
Edge *edge = new Edge;
edge->setVertex(0, pose_vertexs[bal_problem.camera_index()[i]]);
edge->setVertex(1, point_vertexs[bal_problem.point_index()[i]]);
edge->setMeasurement(Vector2d(observations[2*i+0], observations[2*i+1]));
edge->setInformation(Matrix2d::Identity());
edge->setRobustKernel(new g2o::RobustKernelHuber());
optimizer.addEdge(edge);
}
optimizer.initializeOptimization();
// std::cout << "optimize start" << std::endl;
optimizer.optimize(25);
// std::cout << "optimize finish" << std::endl;
double sum_rot_error = 0.0;
double sum_trans_error = 0.0;
for(size_t i = 0; i < bal_problem.num_cameras(); ++i)
{
auto vertex = pose_vertexs[i];
auto estimate = vertex->estimate();
const Sophus::SE3d& opt_pose = SE3d(estimate.rotation, estimate.translation);
double *camera = cameras + camera_block_size * i;
const Sophus::SE3d& org_pose = SE3d(SO3d::exp(Vector3d(camera[0], camera[1], camera[2])), Vector3d(camera[3], camera[4], camera[5]));
Sophus::SE3d pose_err = opt_pose * org_pose.inverse();
sum_rot_error += pose_err.so3().log().norm();
sum_trans_error += pose_err.translation().norm();
}
std::cout << "Mean rot error: " << sum_rot_error / (double)(bal_problem.num_cameras()) << "\tMean trans error: " << sum_trans_error / (double)(bal_problem.num_cameras()) << std::endl;
// for(int i = 0; i < bal_problem.num_cameras(); ++i)
// {
// double *camera = cameras + camera_block_size * i;
// auto vertex = pose_vertexs[i];
// auto estimate = vertex->estimate();
// estimate.set_to(camera);
// }
// for(int i = 0; i < bal_problem.num_points(); ++i)
// {
// double *point = points + point_block_size * i;
// auto vertex = point_vertexs[i];
// for(int k = 0; k < 3; ++k) point[k] = vertex->estimate()[k];
// }
}