source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/BlueCCal/MultiCamSelfCal/gocal.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 13.4 KB
Line 
1% The main script. Performes the self calibration
2% The point coordinates are expected to be known
3% see the directory FindingPoints
4%
5% $Author: svoboda $
6% $Revision: 2.7 $
7% $Id: gocal.m,v 2.7 2005/05/24 09:15:11 svoboda Exp $
8% $State: Exp $
9
10clear all
11
12% add necessary paths
13addpath ../CommonCfgAndIO
14addpath ../RadialDistortions
15addpath ./CoreFunctions
16addpath ./OutputFunctions
17addpath ./BlueCLocal
18addpath ./LocalAlignments
19addpath ../RansacM; % ./Ransac for mex functions (it is significantly faster for noisy data)
20% get the configuration                                         
21config = configdata(expname);
22disp('Multi-Camera Self-Calibration, Tomas Svoboda et al., 07/2003')
23disp('************************************************************')
24disp(sprintf('Experiment name: %s',expname))
25
26%%%
27% how many cameras to be filled
28% if 0 then only points visible in all cameras will be used for selfcal
29% the higher, the more points -> better against Gaussian noise
30% however the higher probability of wrong filling
31% Then the iterative search for outliers takes accordingly longer
32% However, typically no more than 5-7 iterations are needed
33% this number should correspond to the total number of the cameras
34NUM_CAMS_FILL  = config.cal.NUM_CAMS_FILL;     
35%%%
36% tolerance for inliers. The higher uncorrected radial distortion
37% the higher value. For BlueC cameras set to 2 for the ViRoom
38% plastic cams, set to 4 (see FINDINL)
39INL_TOL = config.cal.INL_TOL;                   
40%%%
41% Use Bundle Adjustment to refine the final (after removing outliers) results
42% It is often not needed at all
43DO_BA = config.cal.DO_BA;                               
44                                               
45UNDO_RADIAL = config.cal.UNDO_RADIAL;           % undo radial distortion, parameters are expected to be available
46SAVE_STEPHI = 1;                % save calibration parameters in Stephi's Carve/BlueC compatible form
47SAVE_PGUHA      = 1;            % save calib pars in Prithwijit's compatible form
48USED_MULTIPROC = 0;             % was the multipropcessing used?
49                                                % if yes then multiple IdMat.dat and points.dat have to be loaded
50                                                % setting to 1 it forces to read the multiprocessor data against the
51                                                % monoprocessor see the IM2POINTS, IM2PMULTIPROC.PL
52                                               
53%%%
54% Data structures
55% lin.* corrected values which obey linear model
56% in.* inliers, detected by a chain application of Ransac
57
58if findstr(expname,'oscar')
59  % add a projector idx to the cameras
60  % they are handled the same
61  config.files.cams2use = [config.files.idxcams,config.files.idxproj];
62end
63
64selfcal.par2estimate = config.cal.nonlinpar;
65selfcal.iterate = 1;
66selfcal.count   = 0;
67
68
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70% Main global cycle begins
71%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72while selfcal.iterate & selfcal.count < config.cal.GLOBAL_ITER_MAX,
73  % read the input data
74  loaded = loaddata(config);
75  linear = loaded;              % initalize the linear structure
76
77  CAMS = size(config.cal.cams2use,2);
78  FRAMES = size(loaded.IdMat,2);
79
80  if CAMS < 3 | FRAMES < 20
81        error('gocal: Not enough cameras or images -> Problem in loading data?')
82  end
83
84  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85  %%% correct the required amount of cameras to be filled
86  if CAMS-NUM_CAMS_FILL < 3
87        NUM_CAMS_FILL = CAMS-3;
88  end
89
90  config.cal.Res= loaded.Res;
91  config.cal.pp = reshape([loaded.Res./2,zeros(size(loaded.Res(:,1)))]',CAMS*3,1);
92  config.cal.pp = [loaded.Res./2,zeros(size(loaded.Res(:,1)))];
93
94  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95  % See the README how to compute data
96  % for undoing of the radial distortion
97  if UNDO_RADIAL
98        for i=1:CAMS,
99          [K,kc] = readradfile(sprintf(config.files.rad,config.cal.cams2use(i)));
100          xn     = undoradial(loaded.Ws(i*3-2:i*3,:),K,[kc,0]);
101          linear.Ws(i*3-2:i*3,:) = xn;
102        end
103        linear.Ws = linear.Ws - repmat(reshape(config.cal.pp',CAMS*3,1), 1, FRAMES);
104  elseif config.cal.UNDO_HEIKK,
105        for i=1:CAMS,
106          heikkpar = load(sprintf(config.files.heikkrad,config.cal.cams2use(i)),'-ASCII');
107          xn = undoheikk(heikkpar(1:4),heikkpar(5:end),loaded.Ws(i*3-2:i*3-1,:)');
108          linear.Ws(i*3-2:i*3-1,:) = xn';
109        end
110        linear.Ws = linear.Ws - repmat(reshape(config.cal.pp',CAMS*3,1), 1, FRAMES);
111  else
112        linear.Ws = loaded.Ws - repmat(reshape(config.cal.pp',CAMS*3,1), 1, FRAMES);
113  end
114
115  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116  % Detection of outliers
117  % RANSAC is pairwise applied
118  disp(' ')
119  disp(sprintf('********** After %d iteration *******************************************',selfcal.count))
120  %  disp('****************************************************************************')
121  disp(sprintf('RANSAC validation step running with tolerance threshold: %2.2f ...',INL_TOL));
122
123  inliers.IdMat = findinl(linear.Ws,linear.IdMat,INL_TOL);
124
125  addpath ./MartinecPajdla;
126  setpaths;             % set paths for M&P algorithms
127
128  % remove zero-columns or just 1 point columns
129  % create packed represenatation
130  % it is still a bit tricky, the number of the minimum number of cameras
131  % are specified here, may be some automatic method would be useful
132  packed.idx      = find(sum(inliers.IdMat)>=size(inliers.IdMat,1)-NUM_CAMS_FILL);
133  packed.IdMat  = inliers.IdMat(:,packed.idx);
134  packed.Ws       = linear.Ws(:,packed.idx);
135
136  if size(packed.Ws,2)<20
137        error(sprintf('Only %d points survived RANSAC validation and packing: probably not enough points for reliable selfcalibration',size(packed.Ws,2)));
138  end
139
140  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141  %%% fill cam(i) structures
142  for i=1:CAMS,
143        cam(i).camId  = config.cal.cams2use(i);
144        cam(i).idlin  = find(linear.IdMat(i,:)); % loaded structure
145        cam(i).idin       = find(inliers.IdMat(i,:));   % survived initial pairwise validation
146        cam(i).xdist  = loaded.Ws(3*i-2:3*i,cam(i).idlin);  % original distorted coordinates
147        cam(i).xgt        = linear.Ws(3*i-2:3*i,cam(i).idlin);
148        cam(i).xgtin  = linear.Ws(3*i-2:3*i,cam(i).idin);
149        % convert the ground truth coordinates by using the known principal point
150        cam(i).xgt    = cam(i).xgt + repmat(config.cal.pp(i,:)', 1, size(cam(i).xgt,2));
151        cam(i).xgtin  = cam(i).xgtin + repmat(config.cal.pp(i,:)', 1, size(cam(i).xgtin,2));
152  end
153
154
155  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156  %%% options for the Martinec-Pajdla filling procedure
157  options.verbose = 0;
158  options.no_BA = 1;
159  options.iter = 5;
160  options.detection_accuracy = 2;
161  options.consistent_number  = 9;
162  options.consistent_number_min = 6;
163  options.samples = 1000; %1000; %10000;
164  options.sequence = 0;
165  options.create_nullspace.trial_coef = 10; %20;
166
167  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168  %%% start of the *compute and remove outliers cycle*
169  outliers = 1;
170  inliers.idx = packed.idx;
171  inliers.reprerr = [9e9];
172  while outliers
173        disp(sprintf('%d points/frames have survived validations so far',size(inliers.idx,2)))
174        disp('Filling of missing points is running ...')
175        [P,X, u1,u2, info] = fill_mm_bundle(linear.Ws(:,inliers.idx),config.cal.pp(:,1:2)',options);
176        %
177        Rmat = P*X;
178        Lambda = Rmat(3:3:end,:);
179        %
180        [Pe,Xe,Ce,Re] = euclidize(Rmat,Lambda,P,X,config);   
181        disp('************************************************************')
182        %
183        % compute reprojection errors
184        cam = reprerror(cam,Pe,Xe,FRAMES,inliers);
185        %
186        % detect outliers in cameras
187        [outliers,inliers] = findoutl(cam,inliers,INL_TOL,NUM_CAMS_FILL);
188        %
189        disp(sprintf('Number of detected outliers: %3d',outliers))
190        disp('About cameras (Id, 2D reprojection error, #inliers):')
191        dispcamstats(cam,inliers);
192        % [[cam(:).camId]',[cam(:).std2Derr]',[cam(:).mean2Derr]', sum(inliers.IdMat')']
193        disp('***************************************************************')
194        %
195        % do BA after removing very bad outliers or if the process starts to diverge
196        % and only if required config.cal.START_BA
197        inliers.reprerr = [inliers.reprerr, mean([cam(:).mean2Derr])];
198        if inliers.reprerr(end)<5*INL_TOL | inliers.reprerr(end-1)<inliers.reprerr(end),
199           try, options.no_BA = ~config.cal.START_BA; catch, options.no_BA = 1; end % 1 0
200        end
201  end
202  %%% end of the cycle
203  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204
205
206  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207  %%% Do the final refinement through the BA if required and if not
208  %%% performed during the iteration steps
209  if (DO_BA & ~config.cal.START_BA) | (DO_BA & config.cal.START_BA & size(inliers.reprerr,2)<3)
210        disp('**************************************************************')
211        disp('Refinement by using Bundle Adjustment')
212        options.no_BA = 0;
213        [P,X, u1,u2, info] = fill_mm_bundle(linear.Ws(:,inliers.idx),config.cal.pp(:,1:2)',options);
214        Rmat = P*X;
215        Lambda = Rmat(3:3:end,:);
216        [in.Pe,in.Xe,in.Ce,in.Re] = euclidize(Rmat,Lambda,P,X,config);
217        cam = reprerror(cam,in.Pe,in.Xe,FRAMES,inliers);
218        % [outliers,inliers] = findoutl(cam,inliers,INL_TOL,NUM_CAMS_FILL);
219  else
220        in.Pe = Pe; in.Xe = Xe; in.Ce = Ce; in.Re = Re;
221  end
222  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223
224
225  if 1
226        % plot reconstructed cameras and points
227        drawscene(Xe,Ce,Re,3,'cloud','reconstructed points/camera setup');
228        drawscene(in.Xe,in.Ce,in.Re,4,'cloud','reconstructed points/camera setup only inliers are used',config.cal.cams2use);
229       
230        % plot measured and reprojected 2D points
231        for i=1:CAMS
232          in.xe         = in.Pe(((3*i)-2):(3*i),:)*in.Xe;
233          cam(i).inxe   = in.xe./repmat(in.xe(3,:),3,1);
234          figure(i+10)
235          clf   
236          plot(cam(i).xdist(1,:),cam(i).xdist(2,:),'go');
237          hold on, grid on
238          plot(cam(i).xgt(1,:),cam(i).xgt(2,:),'ro');
239          plot(cam(i).xgtin(1,:),cam(i).xgtin(2,:),'bo');
240          % plot(cam(i).xe(1,:),cam(i).xe(2,:),'r+')
241          plot(cam(i).inxe(1,:),cam(i).inxe(2,:),'k+','MarkerSize',7)
242          %plot(xe(1,:),xe(2,:),'r+','linewidth',3,'MarkerSize',10)
243          title(sprintf('measured, o, vs reprojected, +,  2D points (camera: %d)',config.cal.cams2use(i)));
244          for j=1:size(cam(i).visandrec,2); % plot the reprojection errors
245                line([cam(i).xgt(1,cam(i).visandrec(j)),cam(i).inxe(1,cam(i).recandvis(j))],[cam(i).xgt(2,cam(i).visandrec(j)),cam(i).inxe(2,cam(i).recandvis(j))],'Color','g');
246          end
247          % draw the image boarder
248          line([0 0 0 2*config.cal.pp(i,1) 2*config.cal.pp(i,1) 2*config.cal.pp(i,1) 2*config.cal.pp(i,1) 0],[0 2*config.cal.pp(i,2) 2*config.cal.pp(i,2) 2*config.cal.pp(i,2) 2*config.cal.pp(i,2) 0 0 0],'Color','k','LineWidth',2,'LineStyle','--')
249          axis('equal')
250          drawnow
251          eval(['print -depsc ', config.paths.data, sprintf('%s%d.reprojection.eps',config.files.basename,cam(i).camId)])
252        end
253  end
254
255  %%%
256  % SAVE camera matrices
257  P = in.Pe;
258  save(config.files.Pmats,'P','-ASCII');
259
260  % save normal data
261  if SAVE_STEPHI | SAVE_PGUHA
262         [in.Cst,in.Rot] = savecalpar(in.Pe,config);
263  end   
264
265  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266  % local routines for the BlueC installations
267  % The main functionality of these functions that
268  % they align the coordinate frame from the selfcalibration
269  % with the pre-defined world frame
270  % it is assumed the necessary informations are avialable
271  if findstr('BlueCRZ',expname)
272          [align] = bluecrz(in,config);
273  end
274  if findstr('Hoengg',expname)
275          [align] = bluechoengg(in,config);
276  end
277  if findstr('Erlangen',expname)
278          [align] = erlangen(in,config);
279          % [align] = planarmove(in,config);
280  end
281  if findstr('G9',expname)
282          [align] = g9(in,config);
283  end
284  % planar alignement if knowledge available
285  % [align,cam] = planarmove(in,cam,config);
286  % try, [align,cam] = planarcams(in,cam,config,config.cal.planarcams); end
287  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288  % Evaluate reprojection error
289  %%%
290  cam = evalreprerror(cam,config);
291
292  %%%%
293  % Save the 2D-3D correpondences for further processing
294  for i=1:CAMS,
295        xe = cam(i).xdist(1:3,cam(i).visandrec);        % save the original distorted coordinates
296        % save the reconstructed points (aligned if available)
297        try, Xe = align.X(1:4,cam(i).recandvis); catch, Xe = in.Xe(1:4,cam(i).recandvis); end;
298        % Xe = in.Xe(1:4,cam(i).recandvis);
299        corresp = [Xe',xe'];
300        save(sprintf(config.files.points4cal,config.cal.cams2use(i)),'corresp','-ASCII');
301  end
302
303%%%
304% TO-DO:
305% - find a suitable end condition for the global iteration.
306%   This threshold may very depend on local conditions
307%
308% - how to check meaningful number of iterations
309%   typically only few iterations are needed
310%
311% - The precision of the non-linear estimation should be somehow taken into account
312
313  selfcal.count = selfcal.count+1;
314
315  if max([cam.mean2Derr])>config.cal.GLOBAL_ITER_THR & config.cal.DO_GLOBAL_ITER & selfcal.count < config.cal.GLOBAL_ITER_MAX
316         % if the maximal reprojection error is still bigger
317         % than acceptable estimate radial distortion and
318         % iterate further
319         cd ../CalTechCal
320         selfcalib = goradf(config,selfcal.par2estimate,INL_TOL);
321         cd ../MultiCamSelfCal
322         selfcal.iterate = 1;
323         UNDO_RADIAL = 1;
324         if ~selfcalib.goradproblem;
325                % if all non-linear parameters estimated reliable
326                % we can reduce the tolerance threshold
327                INL_TOL = max([(2/3)*INL_TOL,config.cal.GLOBAL_ITER_THR]);
328                % add the second radial distortion parameter
329                if config.cal.NL_UPDATE(4), selfcal.par2estimate(4) = 1; end
330                % estimate also the principal point
331                if selfcal.count > 1 & config.cal.NL_UPDATE(2), selfcal.par2estimate(2) = 1; end
332                % estimate also the tangential distortion
333                if selfcal.count > 3 & all(config.cal.NL_UPDATE(5:6)), selfcal.par2estimate(5:6) = 1; end
334    else
335                INL_TOL = min([3/2*INL_TOL,config.cal.INL_TOL]);
336        end
337  else
338        % ends the iteration
339        % the last computed parameters will be taken as valid
340        selfcal.iterate = 0;
341  end
342end
343%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344% End of the main global cycle
345%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346
347
348 
Note: See TracBrowser for help on using the repository browser.