-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffs.patch
404 lines (400 loc) · 16.6 KB
/
diffs.patch
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
--- vision/+vision/+internal/+calibration/+tool/Session.m 2016-01-20 15:31:34.000000000 -0500
+++ vision/+vision/+internal/+calibration/+tool/Session.m 2023-01-21 19:07:25.015192300 -0500
@@ -123,3 +123,380 @@
%------------------------------------------------------------------
- function imagesUsed = calibrate(this)
+
+ end
+
+ methods (Static)
+ function res = hlpOrthoSingleCalib(camidx,camSess)
+ camParams = camSess.CameraParameters;
+ nCalIm = camParams.NumPatterns;
+ worldPts = camParams.WorldPoints;
+ bs = camSess.BoardSet;
+ imPtsUV = bs.BoardPoints;
+
+ p0 = OrthoCam.p0fromRsTs(...
+ permute(camParams.RotationMatrices,[2 1 3]),...
+ camParams.TranslationVectors);
+
+ fprintf(1,'NEW ADDITION June 2018. Never needed iterations on single-cam calib before.\n');
+ fprintf(1,'Calibrating camera %d...]n',camidx);
+ pause(1);
+ pOpt = p0;
+ while 1
+ pOpt = OrthoCam.calibrate1cam(nCalIm,worldPts,imPtsUV,pOpt);
+ STOP = 'Stop optimization, looks good';
+ RESTART = 'Restart optimization';
+ CANCEL = 'Cancel';
+ resp = questdlg('Restart optimization?','Optimization waypoint',...
+ STOP,RESTART,CANCEL,RESTART);
+ if isempty(resp)
+ resp = CANCEL;
+ end
+ switch resp
+ case STOP
+ break;
+ case RESTART
+ % none; while loop will proceed
+ case CANCEL
+ error('Session:cal','Calibration canceled.');
+ end
+ end
+ fprintf(1,'OK, done.\n');
+ pause(1);
+
+ res = struct();
+ res.p0 = p0;
+ res.pOpt = pOpt;
+ %res.oFcn = oFcn;
+ res.nPat = nCalIm;
+ res.worldPts = worldPts;
+ res.imPtsUV = imPtsUV;
+ res.boardSetFPNs = bs.FullPathNames;
+ res.ts = now();
+ end
+ function dRP = hlpAssessOrthoSingleCalib(camSess,pOpt)
+
+ camParams = camSess.CameraParameters;
+ nCalIm = camParams.NumPatterns;
+ worldPoints = camParams.WorldPoints;
+ nPts = size(worldPoints,1);
+ szassert(worldPoints,[nPts 2]);
+ worldPtsXYZ = [worldPoints zeros(nPts,1)]';
+
+ bs = camSess.BoardSet;
+ imPtsUV = bs.BoardPoints;
+ szassert(imPtsUV,[nPts 2 nCalIm]);
+ calibImPts = permute(imPtsUV,[2 1 3]);
+
+ oFcn = @(p)OrthoCam.oFcn(p,nCalIm,worldPtsXYZ,calibImPts);
+ dRP = oFcn(pOpt);
+ dRP = reshape(dRP,[nPts nCalIm]);
+ end
+ function [calRes,calResFile] = hlpLoadSingleOrthoCamCalRes(camidx)
+ orthopath = getpref('orthocam','lastpath',pwd);
+ str = sprintf('Select saved single-Orthocam calibration for Camera%d.',camidx);
+ [fname,pth] = uigetfile('*.mat',str,orthopath);
+ if isequal(fname,0)
+ calRes = [];
+ calResFile = '';
+ else
+ calResFile = fullfile(pth,fname);
+ calRes = load(calResFile,'-mat');
+ setpref('orthocam','lastpath',pth);
+ end
+ end
+ function calResFile = hlpSaveSingleOrthoCamCalRes(calres,camsessmatfile,camidx) %#ok<INUSL>
+ [sesspath,sessfile] = fileparts(camsessmatfile);
+ calResFileProposed = fullfile(sesspath,[sessfile '_orthocam_monocal.mat']);
+ str = sprintf('Save single-Orthocam calibration for Camera%d.',camidx);
+ [fname,pth] = uiputfile(calResFileProposed,str);
+ if isequal(fname,0)
+ calResFile = '';
+ else
+ calResFile = fullfile(pth,fname);
+ save(calResFile,'-struct','calres');
+ end
+ end
+ end
+ methods
+ function [sess,sessMatfile] = hlpLoadAndCheckSingleCamSession(this,camidx)
+ orthopath = getpref('orthocam','lastpath',pwd);
+ str = sprintf('Select Camera%d Session/MAT-file saved from MATLAB Camera Calibrator App',camidx);
+ [fname,pth] = uigetfile('*.mat',str,orthopath);
+ if isequal(fname,0)
+ sess = [];
+ sessMatfile = '';
+ else
+ sessMatfile = fullfile(pth,fname);
+ sess = load(sessMatfile,'-mat');
+ setpref('orthocam','lastpath',pth);
+ sess = sess.calibrationSession;
+ fpn1 = this.BoardSet.FullPathNames(camidx,:)';
+ fpn2 = sess.BoardSet.FullPathNames';
+ if ispc
+ fpn1 = lower(fpn1);
+ fpn2 = lower(fpn2);
+ end
+ tf = ismember(fpn1,fpn2);
+ if ~all(tf)
+ error('Session:cal','One or more %s calibration images are not present in single-camera session: %s\n',...
+ ['cam' num2str(camidx)],sessMatfile);
+ end
+ end
+ end
+ end
+ methods (Static)
+ function [r2vecs,t2vecs] = hlpSelSingleCamExtrinsics(iCam,res,bset)
+ pOpt = res.pOpt;
+ nCalIm = res.nPat;
+ fpnsMono = res.boardSetFPNs(:);
+ szassert(fpnsMono,[nCalIm 1]);
+ fpnsStro = bset.FullPathNames(iCam,:)';
+ if ispc
+ fpnsStro = lower(fpnsStro);
+ fpnsMono = lower(fpnsMono);
+ end
+ [tf,loc] = ismember(fpnsStro,fpnsMono);
+ assert(all(tf));
+ fprintf(1,'Selecting %d (rvec,tvec) extrinsic pairs out of %d from cam%d extrinsics:\n',...
+ numel(fpnsStro),nCalIm,iCam);
+
+ [~,~,~,~,~,~,r2vecs,t2vecs] = OrthoCam.unpack1cam(pOpt,nCalIm);
+ r2vecs = r2vecs(:,loc);
+ t2vecs = t2vecs(:,loc);
+ end
+ end
+ methods
+ function imagesUsed = calibrate(this)
+
+ if this.IsValidSingleCameraSession && ~this.IsValidStereoCameraSession
+ imagesUsed = this.calibrateOrig();
+ return;
+ end
+
+ mlVer = ver('matlab');
+ if ~strcmp(mlVer.Release,'(R2016b)')
+ error('Session:cal',...
+ 'MATLAB version R2016b is required for Orthocam calibration.');
+ end
+
+ info = struct();
+ LOAD = 'Load existing orthocam calibrations';
+ CALIB = 'Perform orthocam calibrations now';
+ CANCEL = 'Cancel';
+ resp = questdlg('Do you have existing single-camera OrthoCam calibrations for these two cameras? The calibrations must include all images included in this Stereo calibration project.',...
+ 'Load existing OrthoCam calibrations (single-cam)',...
+ LOAD,CALIB,CANCEL,CALIB);
+ if isempty(resp)
+ resp = CANCEL;
+ end
+ info.monoCalOrigin = 'none'; % {'none','loaded','run'}
+ switch resp
+ case LOAD
+ [info.cam1calres,info.cam1calresfile] = this.hlpLoadSingleOrthoCamCalRes(1);
+ [info.cam2calres,info.cam2calresfile] = this.hlpLoadSingleOrthoCamCalRes(2);
+ if isempty(info.cam1calres) || isempty(info.cam2calres)
+ resp2 = questdlg('Do you want to perform OrthoCam calibrations now?',...
+ 'Perform OrthoCam calibrations','Yes','No','Yes');
+ if isempty(resp2)
+ resp2 = 'No';
+ end
+ switch resp2
+ case 'Yes'
+ % none
+ case 'No'
+ error('Session:cal','Calibration canceled.');
+ end
+ info.monoCalOrigin = 'run';
+ else
+ info.monoCalOrigin = 'loaded';
+ end
+ case CALIB
+ info.monoCalOrigin = 'run';
+ case CANCEL
+ error('Session:cal','Calibration canceled.');
+ end
+ switch info.monoCalOrigin
+ case 'run'
+ % Run it
+ [info.cam1Sess,info.cam1SessMatfile] = this.hlpLoadAndCheckSingleCamSession(1);
+ [info.cam2Sess,info.cam2SessMatfile] = this.hlpLoadAndCheckSingleCamSession(2);
+ info.cam1calres = this.hlpOrthoSingleCalib(1,info.cam1Sess);
+ info.cam2calres = this.hlpOrthoSingleCalib(2,info.cam2Sess);
+ info.cam1calresfile = '';
+ info.cam2calresfile = '';
+ fprintf(1,'Done with single-cam calibrations. Results:\n');
+
+ dRP1 = this.hlpAssessOrthoSingleCalib(info.cam1Sess,info.cam1calres.pOpt);
+ dRP2 = this.hlpAssessOrthoSingleCalib(info.cam2Sess,info.cam2calres.pOpt);
+ mu1 = mean(dRP1(:));
+ mu2 = mean(dRP2(:));
+ hFig = figure('Name','OrthoCam: Reprojection Error');
+ ax = subplot(1,2,1);
+ OrthoCam.vizRPerr(ax,dRP1);
+ cprms1 = info.cam1Sess.CameraParameters;
+ tstr = sprintf('cam1 mono-calib. %dpats, %dpts. mean RPerr=%.3f px',...
+ cprms1.NumPatterns,size(cprms1.WorldPoints,1),mu1);
+ title(tstr,'fontweight','bold');
+ ylabel('count','fontweight','bold');
+ ax = subplot(1,2,2);
+ OrthoCam.vizRPerr(ax,dRP2);
+ cprms2 = info.cam2Sess.CameraParameters;
+ tstr = sprintf('cam2 mono-calib. %dpats, %dpts. mean RPerr=%.3f px',...
+ cprms2.NumPatterns,size(cprms2.WorldPoints,1),mu2);
+ title(tstr,'fontweight','bold');
+
+ % summarize intrinsics; and RP err
+ tblIntsMono = [ ...
+ OrthoCam.summarizeIntrinsics(info.cam1calres.pOpt,info.cam1calres.nPat); ...
+ OrthoCam.summarizeIntrinsics(info.cam2calres.pOpt,info.cam2calres.nPat) ];
+ tblIntsMono.Properties.RowNames = {'monocal-cam1' 'monocal-cam2'};
+ disp(tblIntsMono);
+ input('hit enter to continue');
+
+ % offer to save monocal res if we just ran
+ resp = questdlg('Save single-camera OrthoCam calibrations?',...
+ 'Save calibrations',...
+ 'Yes, save','No','Cancel','Yes, save');
+ if isempty(resp)
+ resp = 'Cancel';
+ end
+ switch resp
+ case 'Yes, save'
+ info.cam1calresfile = this.hlpSaveSingleOrthoCamCalRes(info.cam1calres,info.cam1Sess.Filename,1);
+ info.cam2calresfile = this.hlpSaveSingleOrthoCamCalRes(info.cam2calres,info.cam2Sess.Filename,2);
+ % files may be '' if user cancels
+ case 'No'
+ % none
+ case 'Cancel'
+ error('Session:cal','Calibration canceled.');
+ end
+ case 'loaded'
+ fprintf(1,'Loaded single-cam calibrations:\n');
+ tblIntsMono = [ ...
+ OrthoCam.summarizeIntrinsics(info.cam1calres.pOpt,info.cam1calres.nPat); ...
+ OrthoCam.summarizeIntrinsics(info.cam2calres.pOpt,info.cam2calres.nPat) ];
+ tblIntsMono.Properties.RowNames = {'monocal-cam1' 'monocal-cam2'};
+ disp(tblIntsMono);
+ input('hit enter to continue');
+ end
+
+ % SELECT WORLD COORDSYS/"first" pattern
+% fpnsStro1 = bset.FullPathNames(1,:)';
+ [sel,ok] = listdlg(...
+ 'PromptString','Select pattern to serve as common World Coordsys (for calibration/optimization only)',...
+ 'ListString',this.BoardSet.BoardLabels',...
+ 'SelectionMode','single',...
+ 'ListSize',[400 400]);
+ if ~ok
+ error('Session:cal','Calibration canceled.');
+ end
+
+ boardPerm = [sel 1:sel-1 sel+1:this.BoardSet.NumBoards];
+ boardSetUse = struct();
+ boardSetUse.FullPathNames = this.BoardSet.FullPathNames(:,boardPerm);
+ boardSetUse.BoardPoints = this.BoardSet.BoardPoints(:,:,boardPerm,:);
+ boardSetUse.WorldPoints = this.BoardSet.WorldPoints;
+ boardSetUse.NumBoards = this.BoardSet.NumBoards;
+
+ % pick out extrinsics
+ [r2vecs1,t2vecs1] = this.hlpSelSingleCamExtrinsics(1,info.cam1calres,boardSetUse);
+ [r2vecs2,t2vecs2] = this.hlpSelSingleCamExtrinsics(2,info.cam2calres,boardSetUse);
+
+ [r2veccam1,t2veccam1,r2veccam2,t2veccam2,rvecsPat,tvecsPat] = ...
+ OrthoCam.estimateStroExtrinsics(r2vecs1',t2vecs1',r2vecs2',t2vecs2');
+
+ tblInts1 = tblIntsMono(1,:);
+ tblInts2 = tblIntsMono(2,:);
+ p0 = OrthoCam.packParamsStro( ...
+ tblInts1.mx,tblInts1.my,tblInts1.u0,tblInts1.v0,tblInts1.k1,tblInts1.k2,...
+ tblInts2.mx,tblInts2.my,tblInts2.u0,tblInts2.v0,tblInts2.k1,tblInts2.k2,...
+ r2veccam1,t2veccam1,r2veccam2,t2veccam2,rvecsPat,tvecsPat);
+
+ bset = boardSetUse;
+ nPat = bset.NumBoards;
+ npts = size(bset.BoardPoints,1);
+
+ pOpt = p0;
+ while 1
+ [pOpt,oFcn] = OrthoCam.calibrateStro(nPat,bset.WorldPoints,...
+ bset.BoardPoints(:,:,:,1),bset.BoardPoints(:,:,:,2),pOpt);
+ STOP = 'Stop optimization, looks good';
+ RESTART = 'Restart optimization';
+ CANCEL = 'Cancel';
+ resp = questdlg('Restart optimization?','Optimization waypoint',...
+ STOP,RESTART,CANCEL,RESTART);
+ if isempty(resp)
+ resp = CANCEL;
+ end
+ switch resp
+ case STOP
+ break;
+ case RESTART
+ % none; while loop will proceed
+ case CANCEL
+ error('Session:cal','Calibration canceled.');
+ end
+ end
+
+% % RP err
+% dRP = oFcn(pOpt);
+% dRP = reshape(dRP,[npts nPat 2]);
+% hFig = figure('Name','OrthoCam: Reprojection Error');
+% dRP1 = dRP(:,:,1);
+% dRP2 = dRP(:,:,2);
+% mu1 = mean(dRP1(:));
+% mu2 = mean(dRP2(:));
+% ax = subplot(1,2,1);
+% OrthoCam.vizRPerr(ax,dRP1);
+% title(sprintf('Stereo calib, cam1. %dpats, %dpts. mean RPerr=%.3f px',nPat,npts,mu1),...
+% 'fontweight','bold');
+% ylabel('count','fontweight','bold');
+% ax = subplot(1,2,2);
+% OrthoCam.vizRPerr(ax,dRP2);
+% title(sprintf('Stereo calib, cam2. meanRP err=%.3f px',mu2),'fontweight','bold');
+
+ % Summarize
+ tblIntsStro = OrthoCam.summarizeIntrinsicsStro(pOpt,nPat);
+ tblInts = [tblIntsMono;tblIntsStro];
+ tblInts.Properties.RowNames = ...
+ {'cam1/monocal' 'cam2/monocal' 'cam1/strocal' 'cam2/strocal'};
+ tblInts = tblInts([1 3 2 4],:);
+ disp(tblInts);
+
+ patPtsXYZ = bset.WorldPoints';
+ patPtsXYZ = [patPtsXYZ; zeros(1,npts)];
+
+ calObj = OrthoCamCalPair(pOpt,nPat,npts,patPtsXYZ,...
+ permute(bset.BoardPoints,[2 1 3 4]),bset.FullPathNames');
+ calObj.xformWorldSys(calObj.ijkCamWorld1');
+
+ calObj.viewExtrinsics();
+
+ calObj.viewRPerr();
+
+ if ~isempty(this.Filename)
+ [stropath,strofile] = fileparts(this.Filename);
+ calResFileProposed = fullfile(stropath,[strofile '_orthocam_strocal.mat']);
+ elseif isfield(info,'cam1calresfile') && ~isempty(info.cam1calresfile)
+ stropath = fileparts(info.cam1calresfile);
+ calResFileProposed = fullfile(stropath,'orthocam_strocal.mat');
+ else
+ stropath = pwd;
+ calResFileProposed = fullfile(stropath,'orthocam_strocal.mat');
+ end
+ [fname,pth] = uiputfile('*.mat','Save stereo Orthocam calibration.',calResFileProposed);
+ if isequal(fname,0)
+ % none
+ else
+ calResFile = fullfile(pth,fname);
+ save(calResFile,'calObj');
+ fprintf(1,'Saved %s.\n',calResFile);
+ end
+
+ msgbox('Stereo Orthocam calibration complete.');
+ imagesUsed = [];
+ return;
+ end
+
+ %------------------------------------------------------------------
+ function imagesUsed = calibrateOrig(this)
if isempty(this.OptimizationOptions) || ...
@@ -156,2 +533,3 @@
end
+
--- vision/cameraParameters.m 2016-03-14 15:13:12.000000000 -0400
+++ vision/cameraParameters.m 2023-01-21 19:07:47.180887600 -0500
@@ -384,3 +384,3 @@
- options = optimset('Display', 'off', 'Jacobian', 'on');
+ options = optimset('Display', 'iter', 'Jacobian', 'on','TolFun',1e-8,'tolX',1e-10,'maxiter',2e4);
--- vision/stereoParameters.m 2015-10-30 14:49:36.000000000 -0400
+++ vision/stereoParameters.m 2023-01-21 19:07:51.933705900 -0500
@@ -168,3 +168,5 @@
ydata = [ydataLeft; ydataRight];
- options = optimset('Display', 'off');
+% options = optimset('Display', 'off');
+ options = optimset('Display', 'iter', 'TolFun',1e-9,'tolX',1e-9,'maxiter',2e4,'MaxFunEvals',1e6);
+