-
Notifications
You must be signed in to change notification settings - Fork 23
/
CalibrateCameras.cpp
241 lines (207 loc) · 6.37 KB
/
CalibrateCameras.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
/***********************************************************************
CalibrateCameras - Simple utility to read calibration tie points between
a depth camera and a color camera, and calculate the optimal projective
transformation mapping color to depth.
Copyright (c) 2010-2015 Oliver Kreylos
This file is part of the Kinect 3D Video Capture Project (Kinect).
The Kinect 3D Video Capture Project 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 2 of the License, or (at your option) any later version.
The Kinect 3D Video Capture Project 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 the Kinect 3D Video Capture Project; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
***********************************************************************/
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <IO/File.h>
#include <IO/OpenFile.h>
#include <IO/CSVSource.h>
#include <Math/Math.h>
#include <Math/Matrix.h>
int main(int argc,char* argv[])
{
/* Parse the command line: */
int imgSize[2]={640,480};
const char* tiePointFileName="CalibrationData.csv";
const char* matrixFileName="CameraCalibrationMatrices.dat";
int nameState=0;
for(int i=1;i<argc;++i)
{
if(argv[i][0]=='-')
{
if(strcasecmp(argv[i]+1,"size")==0)
{
for(int j=0;j<2;++j)
{
++i;
imgSize[j]=atoi(argv[i]);
}
}
}
else if(nameState==0)
{
tiePointFileName=argv[i];
++nameState;
}
else if(nameState==1)
{
matrixFileName=argv[i];
++nameState;
}
}
/* Create the linear system: */
Math::Matrix a(12,12,0.0);
{
/* Open the calibration data file: */
IO::CSVSource data(IO::openFile(tiePointFileName));
unsigned int numEntries=0;
while(!data.eof())
{
/* Read a calibration entry from the data file: */
double x=data.readField<double>();
double y=data.readField<double>();
double z=data.readField<double>();
double s=data.readField<double>()/double(imgSize[0]);
double t=data.readField<double>()/double(imgSize[1]);
// s=1.0-s;
t=1.0-t;
/* Insert the entry's two linear equations into the linear system: */
double eq[2][12];
eq[0][0]=x;
eq[0][1]=y;
eq[0][2]=z;
eq[0][3]=1.0;
eq[0][4]=0.0;
eq[0][5]=0.0;
eq[0][6]=0.0;
eq[0][7]=0.0;
eq[0][8]=-s*x;
eq[0][9]=-s*y;
eq[0][10]=-s*z;
eq[0][11]=-s;
eq[1][0]=0.0;
eq[1][1]=0.0;
eq[1][2]=0.0;
eq[1][3]=0.0;
eq[1][4]=x;
eq[1][5]=y;
eq[1][6]=z;
eq[1][7]=1.0;
eq[1][8]=-t*x;
eq[1][9]=-t*y;
eq[1][10]=-t*z;
eq[1][11]=-t;
for(int row=0;row<2;++row)
{
for(unsigned int i=0;i<12;++i)
for(unsigned int j=0;j<12;++j)
a.set(i,j,a(i,j)+eq[row][i]*eq[row][j]);
}
++numEntries;
}
std::cout<<numEntries<<" calibration data entries read from file"<<std::endl;
}
/* Find the linear system's smallest eigenvalue: */
std::pair<Math::Matrix,Math::Matrix> qe=a.jacobiIteration();
unsigned int minEIndex=0;
double minE=Math::abs(qe.second(0,0));
for(unsigned int i=1;i<12;++i)
{
if(minE>Math::abs(qe.second(i,0)))
{
minEIndex=i;
minE=Math::abs(qe.second(i,0));
}
}
/* Create the normalized homography: */
Math::Matrix hom(3,4);
double scale=qe.first(11,minEIndex);
for(int i=0;i<3;++i)
for(int j=0;j<4;++j)
hom.set(i,j,qe.first(i*4+j,minEIndex)/scale);
{
/* Open the calibration data file again: */
IO::CSVSource data(IO::openFile(tiePointFileName));
/* Test the homography on all calibration data entries: */
double rms=0.0;
size_t numTiePoints=0;
while(!data.eof())
{
/* Read a calibration entry from the data file: */
Math::Matrix world(4,1);
for(unsigned int i=0;i<3;++i)
world.set(i,data.readField<double>());
world.set(3,1.0);
/* Read s and t: */
double s=data.readField<double>();
double t=data.readField<double>();
/* Apply the homography: */
Math::Matrix str=hom*world;
double sp=str(0)/str(2);
double tp=str(1)/str(2);
// sp=1.0-sp;
tp=1.0-tp;
// std::cout<<"Result: s = "<<sp*double(imgSize[0])<<", t = "<<tp*double(imgSize[1])<<std::endl;
// std::cout<<world(0)<<", "<<world(1)<<", "<<world(2)<<", "<<sp*double(imgSize[0])<<", "<<tp*double(imgSize[1])<<std::endl;
rms+=Math::sqr(s-sp*double(imgSize[0]))+Math::sqr(t-tp*double(imgSize[1]));
++numTiePoints;
}
std::cout<<"Reprojection residual: "<<Math::sqrt(rms/double(numTiePoints))<<" pixel RMS"<<std::endl;
}
/* Open the calibration file: */
IO::FilePtr matrixFile(IO::openFile(matrixFileName,IO::File::WriteOnly));
matrixFile->setEndianness(Misc::LittleEndian);
#if 0
/* Create the depth projection matrix: */
Math::Matrix depthProjection(4,4,0.0);
double depthScale=34681.3;
double depthBias=1091.71;
double hScale=1.0/320.0/1.8530/1.0072;
double hCenter=320.0;
double vScale=1.0/320.0/1.8530/1.0072;
double vCenter=240.0;
depthProjection(0,0)=hScale;
depthProjection(0,3)=-hCenter*hScale;
depthProjection(1,1)=vScale;
depthProjection(1,3)=-vCenter*vScale;
depthProjection(2,3)=-1.0;
depthProjection(3,2)=-1.0/depthScale;
depthProjection(3,3)=depthBias/depthScale;
/* Save the depth projection matrix: */
for(unsigned int i=0;i<4;++i)
for(unsigned int j=0;j<4;++j)
matrixFile->write<double>(depthProjection(i,j));
#endif
/* Create the color projection matrix by extending the homography: */
Math::Matrix colorProjection(4,4);
for(unsigned int i=0;i<2;++i)
for(unsigned int j=0;j<4;++j)
colorProjection(i,j)=hom(i,j);
for(unsigned int j=0;j<4;++j)
colorProjection(2,j)=j==2?1.0:0.0;
for(unsigned int j=0;j<4;++j)
colorProjection(3,j)=hom(2,j);
/* Modify the color projection matrix by the depth projection matrix: */
// colorProjection*=depthProjection;
/* Print the color projection matrix: */
std::cout<<std::endl;
for(unsigned int i=0;i<4;++i)
{
std::cout<<colorProjection(i,0);
for(unsigned int j=1;j<4;++j)
std::cout<<", "<<colorProjection(i,j);
std::cout<<std::endl;
}
/* Save the color projection matrix: */
for(unsigned int i=0;i<4;++i)
for(unsigned int j=0;j<4;++j)
matrixFile->write<double>(colorProjection(i,j));
return 0;
}