summaryrefslogtreecommitdiffstats
path: root/all_pairs/tacleNames.txt
Commit message (Expand)AuthorAge
* Add pair benchmarking and isolation specification into run_bench.shJoshua Bakita2020-10-22
* Reorganize and commit all the modified TACLeBench code and run scriptsJoshua Bakita2019-10-07
_iter.m?id=b16e25d69337b4b2353715e83b615254110a5196'>blame)
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
%go_calib_optim_iter
%
%Main calibration function. Computes the intrinsic andextrinsic parameters.
%Runs as a script.
%
%INPUT: x_1,x_2,x_3,...: Feature locations on the images
%       X_1,X_2,X_3,...: Corresponding grid coordinates
%
%OUTPUT: fc: Camera focal length
%        cc: Principal point coordinates
%        kc: Distortion coefficients
%        KK: The camera matrix (containing fc and cc)
%        omc_1,omc_2,omc_3,...: 3D rotation vectors attached to the grid positions in space
%        Tc_1,Tc_2,Tc_3,...: 3D translation vectors attached to the grid positions in space
%        Rc_1,Rc_2,Rc_3,...: 3D rotation matrices corresponding to the omc vectors
%
%Method: Minimizes the pixel reprojection error in the least squares sense over the intrinsic
%        camera parameters, and the extrinsic parameters (3D locations of the grids in space)
%
%Note: If the intrinsic camera parameters (fc, cc, kc) do not exist before, they are initialized through
%      the function init_intrinsic_param.m. Otherwise, the variables in memory are used as initial guesses.
%
%Note: The row vector active_images consists of zeros and ones. To deactivate an image, set the
%      corresponding entry in the active_images vector to zero.
%
%VERY IMPORTANT: This function works for 2D and 3D calibration rigs, except for init_intrinsic_param.m
%that is so far implemented to work only with 2D rigs.
%In the future, a more general function will be there.
%For now, if using a 3D calibration rig, quick_init is set to 1 for an easy initialization of the focal length


if ~exist('center_optim'),
	center_optim = 1; %%% Set this variable to 0 if your do not want to estimate the principal point
end;

if ~exist('est_dist'),
   est_dist = [1;1;1;1];
end;

if ~exist('est_alpha'),
   est_alpha = 0; % by default, do not estimate skew
end;


% Little fix in case of stupid values in the binary variables:
center_optim = ~~center_optim;
est_alpha = ~~est_alpha;
est_dist = ~~est_dist;


if ~exist('nx')&~exist('ny'),
   fprintf(1,'WARNING: No image size (nx,ny) available. Setting nx=640 and ny=480\n');
   nx = 640;
   ny = 480;
end;


check_active_images;


quick_init = 0; % Set to 1 for using a quick init (necessary when using 3D rigs)


if ~center_optim, % In the case where the principal point is not estimated, keep it at the center of the image
   fprintf(1,'Principal point not optimized (center_optim=0). It is kept at the center of the image.\n');
   cc = [(nx-1)/2;(ny-1)/2];
end;


if ~prod(est_dist),
   fprintf(1,'\nDistortion not fully estimated. Check variable est_dist.\n');
end;

if ~est_alpha,
   fprintf(1,'Skew not optimized (est_alpha=0).\n');
   alpha_c = 0;
end;


% Check 3D-ness of the calibration rig:
rig3D = 0;
for kk = ind_active,
   eval(['X_kk = X_' num2str(kk) ';']);
   if is3D(X_kk),
      rig3D = 1;
   end;
end;

% If the rig is 3D, then no choice: the only valid initialization is manual!
if rig3D,
   quick_init = 1;
end;



alpha_smooth = 1; % set alpha_smooth = 1; for steepest gradient descent


% Conditioning threshold for view rejection
thresh_cond = 1e6;



%% Initialization of the intrinsic parameters (if necessary)

if ~exist('cc'),
   fprintf(1,'Initialization of the principal point at the center of the image.\n');
   cc = [(nx-1)/2;(ny-1)/2];
   alpha_smooth = 0.4; % slow convergence
end;


if ~exist('kc'),
   fprintf(1,'Initialization of the image distortion to zero.\n');
   kc = zeros(4,1);
   alpha_smooth = 0.4; % slow convergence
end;

if ~exist('alpha_c'),
   fprintf(1,'Initialization of the image skew to zero.\n');
   alpha_c = 0;
   alpha_smooth = 0.4; % slow convergence
end;

if ~exist('fc')& quick_init,
   FOV_angle = 35; % Initial camera field of view in degrees
   fprintf(1,['Initialization of the focal length to a FOV of ' num2str(FOV_angle) ' degrees.\n']);
   fc = (nx/2)/tan(pi*FOV_angle/360) * ones(2,1);
   alpha_smooth = 0.4; % slow 
end;


if ~exist('fc'),
   % Initialization of the intrinsic parameters:
   fprintf(1,'Initialization of the intrinsic parameters using the vanishing points of planar patterns.\n')
   init_intrinsic_param; % The right way to go (if quick_init is not active)!
   alpha_smooth = 0.4; % slow convergence
end;


if ~prod(est_dist),
   % If no distortion estimated, set to zero the variables that are not estimated
   kc = kc .* est_dist;
end;





%% Initialization of the extrinsic parameters for global minimization:


init_param = [fc;cc;alpha_c;kc;zeros(6,1)]; 



for kk = 1:n_ima,

	if exist(['x_' num2str(kk)]),
   
   	eval(['x_kk = x_' num2str(kk) ';']);
      eval(['X_kk = X_' num2str(kk) ';']);
      
   	if (isnan(x_kk(1,1))),
      	if active_images(kk),
         	fprintf(1,'Warning: Cannot calibrate with image %d. Need to extract grid corners first.\n',kk)
         	fprintf(1,'         Set active_images(%d)=1; and run Extract grid corners.\n',kk)
      	end;
      	active_images(kk) = 0;
      end;
      if active_images(kk),
         [omckk,Tckk] = compute_extrinsic_init(x_kk,X_kk,fc,cc,kc,alpha_c);
         [omckk,Tckk,Rckk,JJ_kk] = compute_extrinsic_refine(omckk,Tckk,x_kk,X_kk,fc,cc,kc,alpha_c,20,thresh_cond);
         if cond(JJ_kk)> thresh_cond,
            active_images(kk) = 0;
            omckk = NaN*ones(3,1);
            Tckk = NaN*ones(3,1);
            fprintf(1,'\nWarning: View #%d ill-conditioned. This image is now set inactive.\n',kk)
            desactivated_images = [desactivated_images kk];
         end;
         if isnan(omckk(1,1)),
            %fprintf(1,'\nWarning: Desactivating image %d. Re-activate it later by typing:\nactive_images(%d)=1;\nand re-run optimization\n',[kk kk])
            active_images(kk) = 0;
         end;
      else
         omckk = NaN*ones(3,1);
         Tckk = NaN*ones(3,1);
      end;
   
   else
      
      omckk = NaN*ones(3,1);
      Tckk = NaN*ones(3,1);
      
      if active_images(kk),
         fprintf(1,'Warning: Cannot calibrate with image %d. Need to extract grid corners first.\n',kk)
         fprintf(1,'         Set active_images(%d)=1; and run Extract grid corners.\n',kk)
      end;
      
      active_images(kk) = 0;
      
   end;
   
   eval(['omc_' num2str(kk) ' = omckk;']);
   eval(['Tc_' num2str(kk) ' = Tckk;']);
   
   init_param = [init_param; omckk ; Tckk];
   
end;


check_active_images;



%-------------------- Main Optimization:

fprintf(1,'\nMain calibration optimization procedure - Number of images: %d\n',length(ind_active));


param = init_param;
change = 1;

iter = 0;

fprintf(1,'Gradient descent iterations: ');

param_list = param;

MaxIter = 30;


while (change > 1e-6)&(iter < MaxIter),
   
   fprintf(1,'%d...',iter+1);
   
   
   %% The first step consists of updating the whole vector of knowns (intrinsic + extrinsic of active
   %% images) through a one step steepest gradient descent.
   
	JJ = [];
	ex = [];
	   
   f = param(1:2);
   c = param(3:4);
   alpha = param(5);
   k = param(6:9);

   
   for kk = 1:n_ima,
      
      if active_images(kk),
  	 
   		%omckk = param(4+6*(kk-1) + 3:6*kk + 3); 
   	
         %Tckk = param(6*kk+1 + 3:6*kk+3 + 3);  
         
         omckk = param(15+6*(kk-1) + 1:15+6*(kk-1) + 3); 
   	
         Tckk = param(15+6*(kk-1) + 4:15+6*(kk-1) + 6); 
         
         if isnan(omckk(1)),
            fprintf(1,'Intrinsic parameters at frame %d do not exist\n',kk);
            return;
         end;
         
   		eval(['X_kk = X_' num2str(kk) ';']);
   		eval(['x_kk = x_' num2str(kk) ';']);
   	
   		Np = size(X_kk,2);
   	
   		JJkk = zeros(2*Np,n_ima * 6 + 15);
   	
   		[x,dxdom,dxdT,dxdf,dxdc,dxdk,dxdalpha] = project_points2(X_kk,omckk,Tckk,f,c,k,alpha);
      
   		exkk = x_kk - x;
   
   		ex = [ex;exkk(:)];
         
         JJkk(:,1:2) = dxdf;
    		JJkk(:,3:4) = dxdc;
         JJkk(:,5) = dxdalpha;
         JJkk(:,6:9) = dxdk;