/
CameraCalibration.cpp
308 lines (247 loc) · 8.3 KB
/
CameraCalibration.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
#include "CameraCalibration.h"
// STL
#include <iostream>
#include <fstream>
#include <stdexcept>
namespace CameraCalibration
{
Point2DVector LoadPoints2D(const std::string& filename)
{
std::cout << "LoadPoint2D " << filename << std::endl;
std::string line;
std::ifstream fin(filename.c_str());
Point2DVector points;
if(fin == NULL)
{
std::cout << "Cannot open file." << std::endl;
}
while(getline(fin, line))
{
std::stringstream ss;
ss << line;
double p[3];
ss >> p[0] >> p[1];
points.push_back(Eigen::Vector2d (p[0], p[1]));
}
return points;
}
Point3DVector LoadPoints3D(const std::string& filename)
{
std::cout << "LoadPoint3D " << filename << std::endl;
std::string line;
std::ifstream fin(filename.c_str());
Point3DVector points;
if(fin == NULL)
{
std::stringstream ss;
ss << "CameraCalibration:LoadPoints3D Cannot open file " << filename;
throw std::runtime_error(ss.str());
}
while(getline(fin, line))
{
std::stringstream ss;
ss << line;
double p[3];
ss >> p[0] >> p[1] >> p[2];
points.push_back(Eigen::Vector3d (p[0], p[1], p[2]));
}
return points;
}
Eigen::MatrixXd ComputeP_NormalizedDLT(const Point2DVector& points2D, const Point3DVector& points3D)
{
unsigned int numberOfPoints = points2D.size();
if(points3D.size() != numberOfPoints)
{
std::stringstream ss;
ss << "ComputeP_NormalizedDLT: The number of 2D points (" << points2D.size()
<< ") must match the number of 3D points (" << points3D.size() << ")!" << std::endl;
throw std::runtime_error(ss.str());
}
// std::cout << "ComputeP_NormalizedDLT: 2D points: " << std::endl;
// for(Point2DVector::const_iterator iter = points2D.begin(); iter != points2D.end(); ++iter)
// {
// Point2DVector::value_type p = *iter;
// std::cout << p[0] << " " << p[1] << std::endl;
// }
// std::cout << "ComputeP_NormalizedDLT: 3D points: " << std::endl;
// for(Point3DVector::const_iterator iter = points3D.begin(); iter != points3D.end(); ++iter)
// {
// Point3DVector::value_type p = *iter;
// std::cout << p[0] << " " << p[1] << " " << p[2] << std::endl;
// }
Eigen::MatrixXd similarityTransform2D = ComputeNormalizationTransform<Eigen::Vector2d>(points2D);
Eigen::MatrixXd similarityTransform3D = ComputeNormalizationTransform<Eigen::Vector3d>(points3D);
// std::cout << "Computed similarity transforms:" << std::endl;
// std::cout << "similarityTransform2D: " << similarityTransform2D << std::endl;
// std::cout << "similarityTransform3D: " << similarityTransform3D << std::endl;
// The (, Eigen::VectorXd()) below are only required when using gnu++0x, it seems to be a bug in Eigen
Point2DVector transformed2DPoints(numberOfPoints, Eigen::Vector2d());
Point3DVector transformed3DPoints(numberOfPoints, Eigen::Vector3d());
for(unsigned int i = 0; i < numberOfPoints; ++i)
{
Eigen::VectorXd point2Dhomogeneous = points2D[i].homogeneous();
Eigen::VectorXd point2Dtransformed = similarityTransform2D * point2Dhomogeneous;
transformed2DPoints[i] = point2Dtransformed.hnormalized();
Eigen::VectorXd point3Dhomogeneous = points3D[i].homogeneous();
Eigen::VectorXd point3Dtransformed = similarityTransform3D * point3Dhomogeneous;
transformed3DPoints[i] = point3Dtransformed.hnormalized();
//transformed2DPoints[i] = (similarityTransform2D * points2D[i].homogeneous()).hnormalized();
//transformed3DPoints[i] = (similarityTransform3D * points3D[i].homogeneous()).hnormalized();
}
// std::cout << "Transformed points." << std::endl;
// Compute the Camera Projection Matrix
Eigen::MatrixXd A(2*numberOfPoints,12);
for(unsigned int i = 0; i < numberOfPoints; ++i)
{
// First row/equation from the ith correspondence
unsigned int row = 2*i;
A(row, 0) = 0;
A(row, 1) = 0;
A(row, 2) = 0;
A(row, 3) = 0;
A(row, 4) = transformed3DPoints[i](0);
A(row, 5) = transformed3DPoints[i](1);
A(row, 6) = transformed3DPoints[i](2);
A(row, 7) = 1;
A(row, 8) = -transformed2DPoints[i](1) * transformed3DPoints[i](0);
A(row, 9) = -transformed2DPoints[i](1) * transformed3DPoints[i](1);
A(row, 10) = -transformed2DPoints[i](1) * transformed3DPoints[i](2);
A(row, 11) = -transformed2DPoints[i](1);
// Second row/equation from the ith correspondence
row = 2*i+1;
A(row, 0) = transformed3DPoints[i](0);
A(row, 1) = transformed3DPoints[i](1);
A(row, 2) = transformed3DPoints[i](2);
A(row, 3) = 1;
A(row, 4) = 0;
A(row, 5) = 0;
A(row, 6) = 0;
A(row, 7) = 0;
A(row, 8) = -transformed2DPoints[i](0) * transformed3DPoints[i](0);
A(row, 9) = -transformed2DPoints[i](0) * transformed3DPoints[i](1);
A(row, 10) = -transformed2DPoints[i](0) * transformed3DPoints[i](2);
A(row, 11) = -transformed2DPoints[i](0);
}
// std::cout << "A: " << A << std::endl;
Eigen::JacobiSVD<Eigen::MatrixXd> svd(A, Eigen::ComputeFullU | Eigen::ComputeFullV);
Eigen::MatrixXd V = svd.matrixV();
Eigen::MatrixXd lastColumnOfV = V.col(11);
Eigen::MatrixXd P = Reshape(lastColumnOfV, 3, 4);
// Denormalization
P = similarityTransform2D.inverse()*P*similarityTransform3D; // 3x3 * 3x4 * 4x4 = 4x4
return P;
}
Eigen::MatrixXd ComputeP_Nonlinear(const Point2DVector& points2d, const Point3DVector& points3d)
{
Eigen::MatrixXd linearP = ComputeP_NormalizedDLT(points2d, points3d);
return linearP;
}
Eigen::MatrixXd Reshape(const Eigen::VectorXd& vec, const unsigned int rows, const unsigned int cols)
{
if(static_cast<unsigned int>(vec.rows()) != rows*cols)
{
std::stringstream ss;
ss << "Cannot reshape a vector with " << vec.rows() << " to a "
<< rows << " x " << cols << " matrix!" << std::endl;
throw std::runtime_error(ss.str());
}
Eigen::MatrixXd P(rows,cols);
for(unsigned int row = 0; row < rows; ++row)
{
for(unsigned int col = 0; col < cols; ++col)
{
P(row, col) = vec(row*cols + col);
}
}
return P;
}
struct LMFunctor
{
int operator()(const Eigen::VectorXf &x, Eigen::VectorXf &fvec) const
{
// Implement y = (x-5)^2 (remember, operator() should return the value BEFORE it is squared.
fvec(0) = x(0) - 5.0;
return 0;
}
int df(const Eigen::VectorXf &x, Eigen::MatrixXf &fjac) const
{
Eigen::VectorXf epsilon(1);
epsilon(0) = 1e-5;
Eigen::VectorXf fvec1(1);
operator()(x + epsilon, fvec1);
Eigen::VectorXf fvec2(1);
operator()(x - epsilon, fvec2);
fjac = (fvec1 - fvec2)/2.0f;
return 0;
}
int inputs() const { return 1; }// inputs is the dimension of x.
int values() const { return 1; } // "values" is the number of f_i and
Point2DVector& points2d;
Point3DVector& points3d;
};
/*
float NonLinearProjectionError(Eigen::Vector2d& parameters)
{
// Parameters must be
// parameters = [r(0) r(1) r(2) t(0) t(1) t(2) K(0,0), K(1,1), K(0,2), K(1,2), d]
// [ 0 1 2 3 4 5 6 7 8 9 10
// where r() is the vector of rodrigues angles, t is the translation vector, K is the intrinsic camera matrix, and d is the ?
Eigen::MatrixXd R = rodrigues(k(1:3)');
Eigen::Vector3d t;
t(0) = parameters(3);
t(1) = parameters(4);
t(2) = parameters(5);
float d = parameters(10);
Eigen::MatrixXd K(3,3);
K(0,0) = parameters(6);
K(0,1) = 0;
K(0,2) = parameters(8);
K(1,0) = 0;
K(1,1) = parameters(7);
K(1,2) = parameters(9);
K(0,0) = 0;
K(2,1) = 0;
K(2,2) = 1;
for(unsigned int i = 0; i < points2d.size(); ++i)
{
Xw=X(1:3,i);
Xc=R*Xw+t;
xid=K*Xc;
xid=xid/xid(3);
xd=(xid(1)-K(1,3))/K(1,1);
yd=(xid(2)-K(2,3))/K(2,2);
r=xd^2+yd^2;
xu=xd*(1+d*r);
yu=yd*(1+d*r);
xi(1)=xu*K(1,1)+K(1,3);
xi(2)=yu*K(2,2)+K(2,3);
err(i)=norm(xi'-x(1:2,i));
}
}
*/
Eigen::VectorXd GetCameraCenter(const Eigen::MatrixXd& P)
{
Eigen::MatrixXd M(3,3);
for(unsigned int i = 0; i < 3; ++i)
{
for(unsigned int j = 0; j < 3; ++j)
{
M(i,j) = P(i,j);
}
}
Eigen::MatrixXd Minv = M.inverse();
for(unsigned int i = 0; i < 3; ++i)
{
for(unsigned int j = 0; j < 3; ++j)
{
Minv(i,j) = -1.0f * Minv(i,j);
}
}
Eigen::VectorXd p4(3);
p4[0] = P(0, 3);
p4[1] = P(1, 3);
p4[2] = P(2, 3);
Eigen::VectorXd C = Minv * p4;
return C;
}
} // end namespace