-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalib_stereo.m
executable file
·541 lines (358 loc) · 18 KB
/
calib_stereo.m
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
% calib_stereo
% Script for Calibrating a stereo rig (two cameras, internal and external calibration):
%
% It is assumed that the two cameras (left and right) have been calibrated with the pattern at the same 3D locations, and the same points
% on the pattern (select the same grid points). Therefore, in particular, the same number of images were used to calibrate both cameras.
% The two calibration result files must have been saved under Calib_Results_left.mat and Calib_Results_right.mat prior to running this script.
% This script is not fully documented, therefore use it at your own risks. However, it should be rather straighforward to run.
%
% INPUT: Calib_Results_left.mat, Calib_Results_right.mat -> Generated by the standard calibration toolbox on the two cameras individually)
% OUTPUT: Calib_Results_stereo.mat -> The saved result after global stereo calibration
%
% Main result variables stored in Calib_Results_stereo.mat:
% om, R, T: relative rotation and translation of the right camera wrt the left camera
% fc_left, cc_left, kc_left, alpha_c_left, KK_left: New intrinsic parameters of the left camera
% fc_right, cc_right, kc_right, alpha_c_right, KK_right: New intrinsic parameters of the right camera
%
% Both sets of intrinsic parameters are equivalent to the classical {fc,cc,kc,alpha_c,KK} described online at:
% http://www.vision.caltech.edu/bouguetj/calib_doc/parameters.html
%
% Note: If you do not want to recompute the intinsic parameters, through stereo calibration you may want to set
% recompute_intrinsic_right and recompute_intrinsic_left to zero. Default: 1
%
% Definition of the extrinsic parameters: R and om are related through the rodrigues formula (R=rodrigues(om)).
% Consider a point P of coordinates XL and XR in the left and right camera reference frames respectively.
% XL and XR are related to each other through the following rigid motion transformation:
% XR = R * XL + T
% R and T (or equivalently om and T) fully describe the relative displacement of the two cameras.
%
%
% If the Warning message "Disabling view kk - Reason: the left and right images are found inconsistent" is encountered, that probably
% means that for the kkth pair of images, the left and right images are found to have captured the calibration pattern at two
% different locations in space. That means that the two views are not consistent, and therefore cannot be used for stereo calibration.
% When capturing your images, make sure that you do not move the calibration pattern between capturing the left and the right images.
% The pattwern can (and should) be moved in space only between two sets of (left,right) images.
% Another reason for inconsistency is that you selected a different set of points on the pattern when running the separate calibrations
% (leading to the two files Calib_Results_left.mat and Calib_Results_left.mat). Make sure that the same points are selected in the
% two separate calibration. In other words, the points need to correspond.
% (c) Jean-Yves Bouguet - Intel Corporation
% October 25, 2001 -- Last updated April 12, 2002
fprintf(1,'\n\nStereo Calibration script (for more info, try help calib_stereo)\n\n');
if (exist('Calib_Results_left.mat')~=2)|(exist('Calib_Results_right.mat')~=2),
fprintf(1,'Error: Need the left and right calibration files Calib_Results_left.mat and Calib_Results_right.mat to run stereo calibration\n');
return;
end;
if ~exist('recompute_intrinsic_right'),
recompute_intrinsic_right = 1;
end;
if ~exist('recompute_intrinsic_left'),
recompute_intrinsic_left = 1;
end;
fprintf(1,'Loading the left camera calibration result file Calib_Results_left.mat...\n');
clear calib_name
load Calib_Results_left;
fc_left = fc;
cc_left = cc;
kc_left = kc;
alpha_c_left = alpha_c;
KK_left = KK;
if exist('calib_name'),
calib_name_left = calib_name;
format_image_left = format_image;
type_numbering_left = type_numbering;
image_numbers_left = image_numbers;
N_slots_left = N_slots;
else
calib_name_left = '';
format_image_left = '';
type_numbering_left = '';
image_numbers_left = '';
N_slots_left = '';
end;
X_left = [];
om_left_list = [];
T_left_list = [];
for kk = 1:n_ima,
if active_images(kk),
eval(['Xkk = X_' num2str(kk) ';']);
eval(['omckk = omc_' num2str(kk) ';']);
eval(['Rckk = Rc_' num2str(kk) ';']);
eval(['Tckk = Tc_' num2str(kk) ';']);
N = size(Xkk,2);
Xckk = Rckk * Xkk + Tckk*ones(1,N);
X_left = [X_left Xckk];
om_left_list = [om_left_list omckk];
T_left_list = [T_left_list Tckk];
end;
end;
fprintf(1,'Loading the right camera calibration result file Calib_Results_right.mat...\n');
clear calib_name
load Calib_Results_right;
fc_right = fc;
cc_right = cc;
kc_right = kc;
alpha_c_right = alpha_c;
KK_right = KK;
if exist('calib_name'),
calib_name_right = calib_name;
format_image_right = format_image;
type_numbering_right = type_numbering;
image_numbers_right = image_numbers;
N_slots_right = N_slots;
else
calib_name_right = '';
format_image_right = '';
type_numbering_right = '';
image_numbers_right = '';
N_slots_right = '';
end;
X_right = [];
om_right_list = [];
T_right_list = [];
for kk = 1:n_ima,
if active_images(kk),
eval(['Xkk = X_' num2str(kk) ';']);
eval(['omckk = omc_' num2str(kk) ';']);
eval(['Rckk = Rc_' num2str(kk) ';']);
eval(['Tckk = Tc_' num2str(kk) ';']);
N = size(Xkk,2);
Xckk = Rckk * Xkk + Tckk*ones(1,N);
X_right = [X_right Xckk];
om_right_list = [om_right_list omckk];
T_right_list = [T_right_list Tckk];
end;
end;
om_ref_list = [];
T_ref_list = [];
for ii = 1:length(om_left_list),
% Align the structure from the first view:
R_ref = rodrigues(om_right_list(:,ii)) * rodrigues(om_left_list(:,ii))';
T_ref = T_right_list(:,ii) - R_ref * T_left_list(:,ii);
om_ref = rodrigues(R_ref);
om_ref_list = [om_ref_list om_ref];
T_ref_list = [T_ref_list T_ref];
end;
% Robust estimate of the initial value for rotation and translation between the two views:
om = median(om_ref_list')';
T = median(T_ref_list')';
if 0,
figure(10);
plot3(X_right(1,:),X_right(2,:),X_right(3,:),'bo');
hold on;
[Xr2] = rigid_motion(X_left,om,T);
plot3(Xr2(1,:),Xr2(2,:),Xr2(3,:),'r+');
hold off;
drawnow;
end;
R = rodrigues(om);
% Re-optimize now over all the set of extrinsic unknows (global optimization) and intrinsic parameters:
load Calib_Results_left;
for kk = 1:n_ima,
if active_images(kk),
eval(['X_left_' num2str(kk) ' = X_' num2str(kk) ';']);
eval(['x_left_' num2str(kk) ' = x_' num2str(kk) ';']);
eval(['omc_left_' num2str(kk) ' = omc_' num2str(kk) ';']);
eval(['Rc_left_' num2str(kk) ' = Rc_' num2str(kk) ';']);
eval(['Tc_left_' num2str(kk) ' = Tc_' num2str(kk) ';']);
end;
end;
center_optim_left = center_optim;
est_alpha_left = est_alpha;
est_dist_left = est_dist;
est_fc_left = est_fc;
est_aspect_ratio_left = est_aspect_ratio;
active_images_left = active_images;
load Calib_Results_right;
for kk = 1:n_ima,
if active_images(kk),
eval(['X_right_' num2str(kk) ' = X_' num2str(kk) ';']);
eval(['x_right_' num2str(kk) ' = x_' num2str(kk) ';']);
end;
end;
center_optim_right = center_optim;
est_alpha_right = est_alpha;
est_dist_right = est_dist;
est_fc_right = est_fc;
est_aspect_ratio_right = est_aspect_ratio;
active_images_right = active_images;
if ~recompute_intrinsic_left,
fprintf(1,'No recomputation of the intrinsic parameters of the left camera (recompute_intrinsic_left = 0)\n');
center_optim_left = 0;
est_alpha_left = 0;
est_dist_left = zeros(5,1);
est_fc_left = [0;0];
est_aspect_ratio_left = 1; % just to fix conflicts
end;
if ~recompute_intrinsic_right,
fprintf(1,'No recomputation of the intrinsic parameters of the right camera (recompute_intrinsic_left = 0)\n');
center_optim_right = 0;
est_alpha_right = 0;
est_dist_right = zeros(5,1);
est_fc_right = [0;0];
est_aspect_ratio_right = 1; % just to fix conflicts
end;
threshold = 50; %1e10; %50;
active_images = active_images_left & active_images_right;
history = [];
fprintf(1,'\nMain stereo calibration optimization procedure - Number of pairs of images: %d\n',length(find(active_images)));
fprintf(1,'Gradient descent iteration: ');
for iter = 1:12;
fprintf(1,'%d...',iter);
% Jacobian:
J = [];
e = [];
param = [fc_left;cc_left;alpha_c_left;kc_left;fc_right;cc_right;alpha_c_right;kc_right;om;T];
for kk = 1:n_ima,
if active_images(kk),
% Project the structure onto the left view:
eval(['Xckk = X_left_' num2str(kk) ';']);
eval(['omckk = omc_left_' num2str(kk) ';']);
eval(['Tckk = Tc_left_' num2str(kk) ';']);
eval(['xlkk = x_left_' num2str(kk) ';']);
eval(['xrkk = x_right_' num2str(kk) ';']);
param = [param;omckk;Tckk];
% number of points:
Nckk = size(Xckk,2);
Jkk = sparse(4*Nckk,20+(1+n_ima)*6);
ekk = zeros(4*Nckk,1);
if ~est_aspect_ratio_left,
[xl,dxldomckk,dxldTckk,dxldfl,dxldcl,dxldkl,dxldalphal] = project_points2(Xckk,omckk,Tckk,fc_left(1),cc_left,kc_left,alpha_c_left);
dxldfl = repmat(dxldfl,[1 2]);
else
[xl,dxldomckk,dxldTckk,dxldfl,dxldcl,dxldkl,dxldalphal] = project_points2(Xckk,omckk,Tckk,fc_left,cc_left,kc_left,alpha_c_left);
end;
ekk(1:2*Nckk) = xlkk(:) - xl(:);
Jkk(1:2*Nckk,6*(kk-1)+7+20:6*(kk-1)+7+2+20) = sparse(dxldomckk);
Jkk(1:2*Nckk,6*(kk-1)+7+3+20:6*(kk-1)+7+5+20) = sparse(dxldTckk);
Jkk(1:2*Nckk,1:2) = sparse(dxldfl);
Jkk(1:2*Nckk,3:4) = sparse(dxldcl);
Jkk(1:2*Nckk,5) = sparse(dxldalphal);
Jkk(1:2*Nckk,6:10) = sparse(dxldkl);
% Project the structure onto the right view:
[omr,Tr,domrdomckk,domrdTckk,domrdom,domrdT,dTrdomckk,dTrdTckk,dTrdom,dTrdT] = compose_motion(omckk,Tckk,om,T);
if ~est_aspect_ratio_right,
[xr,dxrdomr,dxrdTr,dxrdfr,dxrdcr,dxrdkr,dxrdalphar] = project_points2(Xckk,omr,Tr,fc_right(1),cc_right,kc_right,alpha_c_right);
dxrdfr = repmat(dxrdfr,[1 2]);
else
[xr,dxrdomr,dxrdTr,dxrdfr,dxrdcr,dxrdkr,dxrdalphar] = project_points2(Xckk,omr,Tr,fc_right,cc_right,kc_right,alpha_c_right);
end;
ekk(2*Nckk+1:end) = xrkk(:) - xr(:);
dxrdom = dxrdomr * domrdom + dxrdTr * dTrdom;
dxrdT = dxrdomr * domrdT + dxrdTr * dTrdT;
dxrdomckk = dxrdomr * domrdomckk + dxrdTr * dTrdomckk;
dxrdTckk = dxrdomr * domrdTckk + dxrdTr * dTrdTckk;
Jkk(2*Nckk+1:end,1+20:3+20) = sparse(dxrdom);
Jkk(2*Nckk+1:end,4+20:6+20) = sparse(dxrdT);
Jkk(2*Nckk+1:end,6*(kk-1)+7+20:6*(kk-1)+7+2+20) = sparse(dxrdomckk);
Jkk(2*Nckk+1:end,6*(kk-1)+7+3+20:6*(kk-1)+7+5+20) = sparse(dxrdTckk);
Jkk(2*Nckk+1:end,11:12) = sparse(dxrdfr);
Jkk(2*Nckk+1:end,13:14) = sparse(dxrdcr);
Jkk(2*Nckk+1:end,15) = sparse(dxrdalphar);
Jkk(2*Nckk+1:end,16:20) = sparse(dxrdkr);
emax = max(abs(ekk));
if emax < threshold,
J = [J;Jkk];
e = [e;ekk];
else
fprintf(1,'Disabling view %d - Reason: the left and right images are found inconsistent (try help calib_stereo for more information)\n',kk);
active_images(kk) = 0;
end;
else
param = [param;NaN*ones(6,1)];
end;
end;
history = [history param];
ind_Jac = find([est_fc_left & [1;est_aspect_ratio_left];center_optim_left*ones(2,1);est_alpha_left;est_dist_left;est_fc_right & [1;est_aspect_ratio_right];center_optim_right*ones(2,1);est_alpha_right;est_dist_right;ones(6,1);reshape(ones(6,1)*active_images,6*n_ima,1)]);
ind_active = find(active_images);
J = J(:,ind_Jac);
J2 = J'*J;
J2_inv = inv(J2);
param_update = J2_inv*J'*e;
param(ind_Jac) = param(ind_Jac) + param_update;
fc_left = param(1:2);
cc_left = param(3:4);
alpha_c_left = param(5);
kc_left = param(6:10);
fc_right = param(11:12);
cc_right = param(13:14);
alpha_c_right = param(15);
kc_right = param(16:20);
if ~est_aspect_ratio_left,
fc_left(2) = fc_left(1);
end;
if ~est_aspect_ratio_right,
fc_right(2) = fc_right(1);
end;
om = param(1+20:3+20);
T = param(4+20:6+20);
for kk = 1:n_ima;
if active_images(kk),
omckk = param(6*(kk-1)+7+20:6*(kk-1)+7+2+20);
Tckk = param(6*(kk-1)+7+3+20:6*(kk-1)+7+5+20);
eval(['omc_left_' num2str(kk) ' = omckk;']);
eval(['Tc_left_' num2str(kk) ' = Tckk;']);
end;
end;
end;
fprintf(1,'done\n');
history = [history param];
inconsistent_images = ~active_images & (active_images_left & active_images_right);
sigma_x = std(e(:));
param_error = zeros(20 + (1+n_ima)*6,1);
param_error(ind_Jac) = 3*sqrt(full(diag(J2_inv)))*sigma_x;
for kk = 1:n_ima;
if active_images(kk),
omckk_error = param_error(6*(kk-1)+7+20:6*(kk-1)+7+2+20);
Tckk = param_error(6*(kk-1)+7+3+20:6*(kk-1)+7+5+20);
eval(['omc_left_error_' num2str(kk) ' = omckk;']);
eval(['Tc_left_error_' num2str(kk) ' = Tckk;']);
else
eval(['omc_left_' num2str(kk) ' = NaN*ones(3,1);']);
eval(['Tc_left_' num2str(kk) ' = NaN*ones(3,1);']);
eval(['omc_left_error_' num2str(kk) ' = NaN*ones(3,1);']);
eval(['Tc_left_error_' num2str(kk) ' = NaN*ones(3,1);']);
end;
end;
fc_left_error = param_error(1:2);
cc_left_error = param_error(3:4);
alpha_c_left_error = param_error(5);
kc_left_error = param_error(6:10);
fc_right_error = param_error(11:12);
cc_right_error = param_error(13:14);
alpha_c_right_error = param_error(15);
kc_right_error = param_error(16:20);
if ~est_aspect_ratio_left,
fc_left_error(2) = fc_left_error(1);
end;
if ~est_aspect_ratio_right,
fc_right_error(2) = fc_right_error(1);
end;
om_error = param_error(1+20:3+20);
T_error = param_error(4+20:6+20);
KK_left = [fc_left(1) fc_left(1)*alpha_c_left cc_left(1);0 fc_left(2) cc_left(2); 0 0 1];
KK_right = [fc_right(1) fc_right(1)*alpha_c_right cc_right(1);0 fc_right(2) cc_right(2); 0 0 1];
R = rodrigues(om);
fprintf(1,'\n\nIntrinsic parameters of left camera:\n\n');
fprintf(1,'Focal Length: fc_left = [ %3.5f %3.5f ] ± [ %3.5f %3.5f ]\n',[fc_left;fc_left_error]);
fprintf(1,'Principal point: cc_left = [ %3.5f %3.5f ] ± [ %3.5f %3.5f ]\n',[cc_left;cc_left_error]);
fprintf(1,'Skew: alpha_c_left = [ %3.5f ] ± [ %3.5f ] => angle of pixel axes = %3.5f ± %3.5f degrees\n',[alpha_c_left;alpha_c_left_error],90 - atan(alpha_c_left)*180/pi,atan(alpha_c_left_error)*180/pi);
fprintf(1,'Distortion: kc_left = [ %3.5f %3.5f %3.5f %3.5f %5.5f ] ± [ %3.5f %3.5f %3.5f %3.5f %5.5f ]\n',[kc_left;kc_left_error]);
fprintf(1,'\n\nIntrinsic parameters of right camera:\n\n');
fprintf(1,'Focal Length: fc_right = [ %3.5f %3.5f ] ± [ %3.5f %3.5f ]\n',[fc_right;fc_right_error]);
fprintf(1,'Principal point: cc_right = [ %3.5f %3.5f ] ± [ %3.5f %3.5f ]\n',[cc_right;cc_right_error]);
fprintf(1,'Skew: alpha_c_right = [ %3.5f ] ± [ %3.5f ] => angle of pixel axes = %3.5f ± %3.5f degrees\n',[alpha_c_right;alpha_c_right_error],90 - atan(alpha_c_right)*180/pi,atan(alpha_c_right_error)*180/pi);
fprintf(1,'Distortion: kc_right = [ %3.5f %3.5f %3.5f %3.5f %5.5f ] ± [ %3.5f %3.5f %3.5f %3.5f %5.5f ]\n',[kc_right;kc_right_error]);
fprintf(1,'\n\nExtrinsic parameters (position of right camera wrt left camera):\n\n');
fprintf(1,'Rotation vector: om = [ %3.5f %3.5f %3.5f ] ± [ %3.5f %3.5f %3.5f ]\n',[om;om_error]);
fprintf(1,'Translation vector: T = [ %3.5f %3.5f %3.5f ] ± [ %3.5f %3.5f %3.5f ]\n',[T;T_error]);
fprintf(1,'\n\nNote: The numerical errors are approximately three times the standard deviations (for reference).\n\n')
fprintf(1,'Saving the stereo calibration results in Calib_Results_stereo.mat\n\n');
string_save = 'save Calib_Results_stereo om R T recompute_intrinsic_right recompute_intrinsic_left calib_name_left format_image_left type_numbering_left image_numbers_left N_slots_left calib_name_right format_image_right type_numbering_right image_numbers_right N_slots_right fc_left cc_left kc_left alpha_c_left KK_left fc_right cc_right kc_right alpha_c_right KK_right active_images dX dY nx ny n_ima active_images_right active_images_left inconsistent_images center_optim_left est_alpha_left est_dist_left est_fc_left est_aspect_ratio_left center_optim_right est_alpha_right est_dist_right est_fc_right est_aspect_ratio_right history param param_error sigma_x om_error T_error fc_left_error cc_left_error kc_left_error alpha_c_left_error fc_right_error cc_right_error kc_right_error alpha_c_right_error';
for kk = 1:n_ima,
if active_images(kk),
string_save = [string_save ' X_left_' num2str(kk) ' omc_left_' num2str(kk) ' Tc_left_' num2str(kk) ' omc_left_error_' num2str(kk) ' Tc_left_error_' num2str(kk) ' n_sq_x_' num2str(kk) ' n_sq_y_' num2str(kk)];
end;
end;
eval(string_save);
% Plot the extrinsic parameters:
ext_calib_stereo;