forked from kysucix/gipuma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cameraGeometryUtils.h
354 lines (292 loc) · 13.3 KB
/
cameraGeometryUtils.h
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* cameraGeometryUtils.h
*
* utility functions for camera geometry related stuff
* most of them from: "Multiple View Geometry in computer vision" by Hartley and Zisserman
*/
#pragma once
#include "mathUtils.h"
#include <limits>
#include <signal.h>
Mat_<float> getColSubMat ( Mat_<float> M, int* indices, int numCols ) {
Mat_<float> subMat = Mat::zeros ( M.rows,numCols,CV_32F );
for ( int i = 0; i < numCols; i++ ) {
M.col ( indices[i] ).copyTo ( subMat.col ( i ) );
}
return subMat;
}
// Multi View Geometry, page 163
Mat_<float> getCameraCenter ( Mat_<float> &P ) {
Mat_<float> C = Mat::zeros ( 4,1,CV_32F );
Mat_<float> M = Mat::zeros ( 3,3,CV_32F );
int xIndices[] = { 1, 2, 3 };
int yIndices[] = { 0, 2, 3 };
int zIndices[] = { 0, 1, 3 };
int tIndices[] = { 0, 1, 2 };
// x coordinate
M = getColSubMat ( P,xIndices,sizeof ( xIndices )/sizeof ( xIndices[0] ) );
C ( 0,0 ) = ( float )determinant ( M );
// y coordinate
M = getColSubMat ( P,yIndices,sizeof ( yIndices )/sizeof ( yIndices[0] ) );
C ( 1,0 ) = - ( float )determinant ( M );
// z coordinate
M = getColSubMat ( P,zIndices,sizeof ( zIndices )/sizeof ( zIndices[0] ) );
C ( 2,0 ) = ( float )determinant ( M );
// t coordinate
M = getColSubMat ( P,tIndices,sizeof ( tIndices )/sizeof ( tIndices[0] ) );
C ( 3,0 ) = - ( float )determinant ( M );
return C;
}
inline Vec3f get3Dpoint ( Camera &cam, float x, float y, float depth ) {
// in case camera matrix is not normalized: see page 162, then depth might not be the real depth but w and depth needs to be computed from that first
Mat_<float> pt = Mat::ones ( 3,1,CV_32F );
pt ( 0,0 ) = x;
pt ( 1,0 ) = y;
//formula taken from page 162 (alternative expression)
Mat_<float> ptX = cam.M_inv * ( depth*pt - cam.P.col ( 3 ) );
return Vec3f ( ptX ( 0 ),ptX ( 1 ),ptX ( 2 ) );
}
inline Vec3f get3Dpoint ( Camera &cam, int x, int y, float depth ){
return get3Dpoint(cam,(float)x,(float)y,depth);
}
// get the viewing ray for a pixel position of the camera
static inline Vec3f getViewVector ( Camera &cam, int x, int y) {
//get some point on the line (the other point on the line is the camera center)
Vec3f ptX = get3Dpoint ( cam,x,y,1.0f );
//get vector between camera center and other point on the line
Vec3f v = ptX - cam.C;
return normalize ( v );
}
/* get depth from 3D point
* page 162: w = P3T*X (P3T ... third (=last) row of projection matrix P)
*/
float getDepth ( Vec3f &X, Mat_<float> &P ) {
//assuming homogenous component of X being 1
float w = P ( 2,0 )*X ( 0 ) + P ( 2,1 ) * X ( 1 ) + P ( 2,2 ) * X ( 2 ) + P ( 2,3 );
return w;
}
Mat_<float> getTransformationMatrix ( Mat_<float> R, Mat_<float> t ) {
Mat_<float> transMat = Mat::eye ( 4,4, CV_32F );
//Mat_<float> Rt = - R * t;
R.copyTo ( transMat ( Range ( 0,3 ),Range ( 0,3 ) ) );
t.copyTo ( transMat ( Range ( 0,3 ),Range ( 3,4 ) ) );
return transMat;
}
/* compute depth value from disparity or disparity value from depth
* Input: f - focal length in pixel
* baseline - baseline between cameras (in meters)
* d - either disparity or depth value
* Output: either depth or disparity value
*/
float disparityDepthConversion ( float f, float baseline, float d ) {
/*if ( d == 0 )*/
/*return FLT_MAX;*/
return f * baseline / d;
}
Mat_<float> getTransformationReferenceToOrigin ( Mat_<float> R,Mat_<float> t ) {
// create rotation translation matrix
Mat_<float> transMat_original = getTransformationMatrix ( R,t );
// get transformation matrix for [R1|t1] = [I|0]
return transMat_original.inv ();
}
void transformCamera ( Mat_<float> R,Mat_<float> t, Mat_<float> transform, Camera &cam, Mat_<float> K ) {
// create rotation translation matrix
Mat_<float> transMat_original = getTransformationMatrix ( R,t );
//transform
Mat_<float> transMat_t = transMat_original * transform;
// compute translated P (only consider upper 3x4 matrix)
cam.P = K * transMat_t ( Range ( 0,3 ),Range ( 0,4 ) );
// set R and t
cam.R = transMat_t ( Range ( 0,3 ),Range ( 0,3 ) );
cam.t = transMat_t ( Range ( 0,3 ),Range ( 3,4 ) );
// set camera center C
Mat_<float> C = getCameraCenter ( cam.P );
C = C / C ( 3,0 );
cam.C = Vec3f ( C ( 0,0 ),C ( 1,0 ),C ( 2,0 ) );
}
Mat_<float> scaleK ( Mat_<float> K, float scaleFactor ) {
Mat_<float> K_scaled = K.clone();
//scale focal length
K_scaled ( 0,0 ) = K ( 0,0 ) / scaleFactor;
K_scaled ( 1,1 ) = K ( 1,1 ) / scaleFactor;
//scale center point
K_scaled ( 0,2 ) = K ( 0,2 ) / scaleFactor;
K_scaled ( 1,2 ) = K ( 1,2 ) / scaleFactor;
return K_scaled;
}
void copyOpencvVecToFloat4 ( Vec3f &v, float4 *a)
{
a->x = v(0);
a->y = v(1);
a->z = v(2);
}
void copyOpencvVecToFloatArray ( Vec3f &v, float *a)
{
a[0] = v(0);
a[1] = v(1);
a[2] = v(2);
}
void copyOpencvMatToFloatArray ( Mat_<float> &m, float **a)
{
for (int pj=0; pj<m.rows ; pj++)
for (int pi=0; pi<m.cols ; pi++)
{
(*a)[pi+pj*m.cols] = m(pj,pi);
}
}
/* get camera parameters (e.g. projection matrices) from file
* Input: inputFiles - pathes to calibration files
* scaleFactor - if image was rescaled we need to adapt calibration matrix K accordingly
* Output: camera parameters
*/
CameraParameters getCameraParameters ( CameraParameters_cu &cpc,
InputFiles inputFiles,
float scaleFactor = 1.0f,
bool transformP = true )
{
CameraParameters params;
size_t numCameras = 2;
params.cameras.resize ( numCameras );
//get projection matrices
//load projection matrix from file (e.g. for Kitti)
if ( !inputFiles.calib_filename.empty () ) {
//two view case
readCalibFileKitti ( inputFiles.calib_filename,params.cameras[0].P,params.cameras[1].P );
params.rectified = false; // for Kitti data is actually rectified, set this to true for computation in disparity space
}
Mat_<float> KMaros = Mat::eye ( 3, 3, CV_32F );
KMaros(0,0) = 8066.0;
KMaros(1,1) = 8066.0;
KMaros(0,2) = 2807.5;
KMaros(1,2) = 1871.5;
//load projection matrix from file (e.g. for Strecha)
// Load pmvs files
if ( !inputFiles.pmvs_folder.empty () ) {
numCameras = inputFiles.img_filenames.size ();
params.cameras.resize ( numCameras );
for ( size_t i = 0; i < numCameras; i++ ) {
int lastindex = inputFiles.img_filenames[i].find_last_of(".");
string filename_without_extension = inputFiles.img_filenames[i].substr(0, lastindex);
readPFileStrechaPmvs ( inputFiles.p_folder + filename_without_extension + ".txt",params.cameras[i].P );
unsigned found = inputFiles.img_filenames[i].find_last_of ( "." );
//params.cameras[i].id = atoi ( inputFiles.img_filenames[i].substr ( 0,found ).c_str () );
params.cameras[i].id = inputFiles.img_filenames[i].substr ( 0,found ).c_str ();
// params.cameras[i].P = KMaros * params.cameras[i].P;
//cout << params.cameras[i].P << endl;
}
}
// Load p files strecha style
else if ( !inputFiles.p_folder.empty () ) {
numCameras = inputFiles.img_filenames.size ();
params.cameras.resize ( numCameras );
for ( size_t i = 0; i < numCameras; i++ ) {
readPFileStrechaPmvs ( inputFiles.p_folder + inputFiles.img_filenames[i] + ".P",params.cameras[i].P );
unsigned found = inputFiles.img_filenames[i].find_last_of ( "." );
//params.cameras[i].id = atoi ( inputFiles.img_filenames[i].substr ( 0,found ).c_str () );
params.cameras[i].id = inputFiles.img_filenames[i].substr ( 0,found ).c_str ();
// params.cameras[i].P = KMaros * params.cameras[i].P;
//cout << params.cameras[i].P << endl;
}
}
// Load P matrix for middlebury format
if ( !inputFiles.krt_file.empty () ) {
numCameras = inputFiles.img_filenames.size ();
params.cameras.resize ( numCameras );
//cout << "Num Cameras " << numCameras << endl;
readKRtFileMiddlebury ( inputFiles.krt_file, params.cameras, inputFiles);
}
/*cout << "KMaros is" << endl;*/
/*cout << KMaros << endl;*/
// decompose projection matrices into K, R and t
vector<Mat_<float> > K ( numCameras );
vector<Mat_<float> > R ( numCameras );
vector<Mat_<float> > T ( numCameras );
vector<Mat_<float> > C ( numCameras );
vector<Mat_<float> > t ( numCameras );
for ( size_t i = 0; i < numCameras; i++ ) {
decomposeProjectionMatrix ( params.cameras[i].P,K[i],R[i],T[i] );
//cout << "K: " << K[i] << endl;
//cout << "R: " << R[i] << endl;
//cout << "T: " << T[i] << endl;
// get 3-dimensional translation vectors and camera center (divide by augmented component)
C[i] = T[i] ( Range ( 0,3 ),Range ( 0,1 ) ) / T[i] ( 3,0 );
t[i] = -R[i] * C[i];
//cout << "C: " << C[i] << endl;
//cout << "t: " << t[i] << endl;
}
// transform projection matrices (R and t part) so that P1 = K [I | 0]
//computeTranslatedProjectionMatrices(R1, R2, t1, t2, params);
Mat_<float> transform = Mat::eye ( 4,4 ,CV_32F);
if ( transformP )
transform = getTransformationReferenceToOrigin ( R[0],t[0] );
/*cout << "transform is " << transform << endl;*/
params.cameras[0].reference = true;
params.idRef = 0;
//cout << "K before scale is" << endl;
//cout << K[0] << endl;
//assuming K is the same for all cameras
params.K = scaleK ( K[0],scaleFactor );
params.K_inv = params.K.inv ();
// get focal length from calibration matrix
params.f = params.K ( 0,0 );
for ( size_t i = 0; i < numCameras; i++ ) {
params.cameras[i].K = scaleK(K[i],scaleFactor);
params.cameras[i].K_inv = params.cameras[i].K.inv ( );
//params.cameras[i].f = params.cameras[i].K(0,0);
if ( !inputFiles.bounding_folder.empty () ) {
Vec3f ptBL, ptTR;
readBoundingVolume ( inputFiles.bounding_folder + inputFiles.img_filenames[i] + ".bounding",ptBL,ptTR );
cout << "d1: " << getDepth ( ptBL,params.cameras[i].P ) <<endl;
cout << "d2: " << getDepth ( ptTR,params.cameras[i].P ) <<endl;
}
params.cameras[i].R_orig_inv = R[i].inv (DECOMP_SVD);
transformCamera ( R[i],t[i], transform, params.cameras[i],params.K );
params.cameras[i].P_inv = params.cameras[i].P.inv ( DECOMP_SVD );
params.cameras[i].M_inv = params.cameras[i].P.colRange ( 0,3 ).inv ();
// set camera baseline (if unknown we need to guess something)
//float b = (float)norm(t1,t2,NORM_L2);
params.cameras[i].baseline = 0.54f; //0.54 = Kitti baseline
// K
Mat_<float> tmpK = params.K.t ();
//copyOpencvMatToFloatArray ( params.K, &cpc.K);
//copyOpencvMatToFloatArray ( params.K_inv, &cpc.K_inv);
copyOpencvMatToFloatArray ( params.cameras[i].K, &cpc.cameras[i].K);
copyOpencvMatToFloatArray ( params.cameras[i].K_inv, &cpc.cameras[i].K_inv);
copyOpencvMatToFloatArray ( params.cameras[i].R_orig_inv, &cpc.cameras[i].R_orig_inv);
cpc.cameras[i].fy = params.K(1,1);
cpc.f = params.K(0,0);
cpc.cameras[i].f = params.K(0,0);
cpc.cameras[i].fx = params.K(0,0);
cpc.cameras[i].fy = params.K(1,1);
cpc.cameras[i].baseline = params.cameras[i].baseline;
cpc.cameras[i].reference = params.cameras[i].reference;
/*params.cameras[i].alpha = params.K ( 0,0 )/params.K(1,1);*/
cpc.cameras[i].alpha = params.K ( 0,0 )/params.K(1,1);
// Copy data to cuda structure
copyOpencvMatToFloatArray ( params.cameras[i].P, &cpc.cameras[i].P);
copyOpencvMatToFloatArray ( params.cameras[i].P_inv, &cpc.cameras[i].P_inv);
copyOpencvMatToFloatArray ( params.cameras[i].M_inv, &cpc.cameras[i].M_inv);
//copyOpencvMatToFloatArray ( params.K, &cpc.cameras[i].K);
//copyOpencvMatToFloatArray ( params.K_inv, &cpc.cameras[i].K_inv);
copyOpencvMatToFloatArray ( params.cameras[i].K, &cpc.cameras[i].K);
copyOpencvMatToFloatArray ( params.cameras[i].K_inv, &cpc.cameras[i].K_inv);
copyOpencvMatToFloatArray ( params.cameras[i].R, &cpc.cameras[i].R);
/*copyOpencvMatToFloatArray ( params.cameras[i].t, &cpc.cameras[i].t);*/
/*copyOpencvVecToFloatArray ( params.cameras[i].C, cpc.cameras[i].C);*/
copyOpencvVecToFloat4 ( params.cameras[i].C, &cpc.cameras[i].C4);
cpc.cameras[i].t4.x = params.cameras[i].t(0);
cpc.cameras[i].t4.y = params.cameras[i].t(1);
cpc.cameras[i].t4.z = params.cameras[i].t(2);
Mat_<float> tmp = params.cameras[i].P.col(3);
/*cpc.cameras[i].P_col3[0] = tmp(0,0);*/
/*cpc.cameras[i].P_col3[1] = tmp(1,0);*/
/*cpc.cameras[i].P_col3[2] = tmp(2,0);*/
cpc.cameras[i].P_col34.x = tmp(0,0);
cpc.cameras[i].P_col34.y = tmp(1,0);
cpc.cameras[i].P_col34.z = tmp(2,0);
//cout << params.cameras[i].P << endl;
//cout << endl;
Mat_<float> tmpKinv = params.K_inv.t ();
}
return params;
}