-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBA_ceres.cpp
More file actions
172 lines (146 loc) · 4.99 KB
/
Copy pathBA_ceres.cpp
File metadata and controls
172 lines (146 loc) · 4.99 KB
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
#include <Eigen/Core>
#include <Eigen/Dense>
#include <iostream>
#include <ceres/ceres.h>
#include <ceres/rotation.h>
#include <fstream>
#include "helper.h"
using namespace Eigen;
using namespace ceres;
using namespace std;
vector<Vector3d> p3d;
vector<Vector2d> p2d;
class ReprojectionErrorAnalytic : public SizedCostFunction<2,6>
{
public:
ReprojectionErrorAnalytic(Vector3d point_, Vector2d observed_)
: point(point_), observed(observed_) {
}
virtual ~ReprojectionErrorAnalytic(){}
virtual bool Evaluate(double const* const* parameters, double* residuals, double** jacobians) const
{
Map<const Vector6d> se3(parameters[0]);
Matrix4d Tcw = expmap_se3(se3);
Matrix3d Rcw = Tcw.block(0,0,3,3);
Vector3d tcw = Tcw.block(0,3,3,1);
Vector3d pt2 = Rcw*point + tcw;
const double xp = double(K[0] * (pt2[0] / pt2[2]) + K[2]);
const double yp = double(K[1] * (pt2[1] / pt2[2]) + K[3]);
const double u = double(observed(0));
const double v = double(observed(1));
residuals[0] = u - xp;
residuals[1] = v - yp;
Eigen::Matrix<double, 2, 3, RowMajor> J_cam;
J_cam << K[0]/pt2[2], 0, -K[0]*pt2[0]/(pt2[2]*pt2[2]),
0, K[1]/pt2[2], -K[1]*pt2[1]/(pt2[2]*pt2[2]);
if(jacobians != NULL)
{
if(jacobians[0] != NULL)
{
Map<Eigen::Matrix<double, 2, 6, RowMajor>> J_se3(jacobians[0]);
J_se3.setZero();
J_se3.block(0,3,2,3) = - J_cam * skew(pt2);
J_se3.block(0,0,2,3) = J_cam;
}
}
return true;
}
private:
Vector3d point;
Vector2d observed;
// Camera intrinsics
double K[4] = {520.9, 521.0, 325.1, 249.7}; // fx,fy,cx,cy
};
class ReprojectionError6D {
public:
ReprojectionError6D(Vector3d point_, Vector2d observed_)
: point(point_), observed(observed_) {
}
bool operator()(const double *const camera, double *residuals) const {
Vector6d se3 ;
se3 << camera[0], camera[1], camera[2], camera[3], camera[4], camera[5];
Matrix4d Tcw = expmap_se3(se3);
Matrix3d Rcw = Tcw.block(0,0,3,3);
Vector3d tcw = Tcw.block(0,3,3,1);
Vector3d pt2 = Rcw*point + tcw;
const double xp = double(K[0] * (pt2[0] / pt2[2]) + K[2]);
const double yp = double(K[1] * (pt2[1] / pt2[2]) + K[3]);
const double u = double(observed(0));
const double v = double(observed(1));
residuals[0] = u - xp;
residuals[1] = v - yp;
return true;
}
static ceres::CostFunction *Create(Vector3d points, Vector2d observed) {
return (new ceres::NumericDiffCostFunction<ReprojectionError6D,FORWARD,2, 6>(
new ReprojectionError6D(points, observed)));
}
private:
Vector3d point;
Vector2d observed;
// Camera intrinsics
double K[4] = {520.9, 521.0, 325.1, 249.7}; // fx,fy,cx,cy
};
bool readData(string p3d_file, string p2d_file) {
ifstream f3d(p3d_file);
if(!f3d)
{
cout<<"No file for 3D points."<<endl;
}
else {
while(!f3d.eof())
{
double pt3[3] = {0};
f3d >> pt3[0] >> pt3[1] >> pt3[2];
Vector3d Point3d;
Point3d << pt3[0], pt3[1], pt3[2];
p3d.push_back(Point3d);
}
}
ifstream f2d(p2d_file);
if (!f2d) {
cout << "No file for 2D pixels." << endl;
return false;
} else {
while (!f2d.eof()) {
double pt2[2] = {0};
f2d >> pt2[0] >> pt2[1];
Vector2d Point2d;
Point2d << pt2[0], pt2[1];
p2d.push_back(Point2d);
}
}
assert(p3d.size() == p2d.size());
cout << "The test data contains " << p3d.size() << " 3D-2D pairs." << endl;
return true;
}
int main(int argc, char *argv[]) {
// Google log
google::InitGoogleLogging(argv[0]);
string p3d_file = string(argv[1]);
string p2d_file = string(argv[2]);
if (!readData(p3d_file, p2d_file)) {
cerr << "Read data failed." << endl;
return -1;
}
ceres::Problem problem;
ceres::LossFunction *lossfunction = NULL;
double camera[6] = {0,0,0,0,0,0};
for (uint i = 0; i < p3d.size(); i++) {
ceres::CostFunction *costfunction = ReprojectionError6D::Create(p3d[i], p2d[i]);
problem.AddResidualBlock(costfunction, lossfunction, camera);
}
ceres::Solver::Options options;
options.linear_solver_type = ceres::DENSE_NORMAL_CHOLESKY;
options.max_num_iterations = 100;
options.trust_region_strategy_type = ceres::LEVENBERG_MARQUARDT;
options.minimizer_progress_to_stdout = true;
ceres::Solver::Summary summary;
ceres::Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << std::endl;
Vector6d se3;
se3 << camera[0], camera[1], camera[2], camera[3], camera[4], camera[5];
Eigen::Matrix4d T = expmap_se3(se3);
std::cout << "T = \n" << T << std::endl;
return 0;
}