-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbundleplc.m
More file actions
356 lines (331 loc) · 9.53 KB
/
Copy pathbundleplc.m
File metadata and controls
356 lines (331 loc) · 9.53 KB
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
function [s,m,residual,resnew,lambda] = bundleplc(s,m,imseq,option);
% function [s,m,redidual,resnew] = bundleplc(s,m,imseq,option);
% Bundle adjustment for combinations of points, lines and conics
% Input:
% s - structure object
% m - motion object
% imseq - cell list of imagedata objects
% option: (cell of strings)
% 'affine' - affine s & m
% 'iteration=X' - iterate X times
% 'lambda=X' - set lambda to X
% 'outputoff' - No results displayed
% 'autocalib=XXXXX' - Autocalibration
% specify for each of focal length, aspect ratio, skew,
% principal point x, principal point y,
% if the parameter is
% 1 known and nominal,
% 2 unknown but constant in the sequence,
% 3 unknown and varying.
% 'structure' - Only optimize structure
% 'motion' - Only optimize motion
% 'knownrotation' - Only optimize camera centres and structure
% 'focal=[f,sigma]' - Focal length at f with std sigma
% 'ar=[a,sigma]' - Aspect ratio at a with std sigma
% 'pp=[u,v,sigma]' - Principal point at (u,v) with std sigma
% 'lockcsystem' - Lock coordinate system. Locks first camera
% and one of second camera centre coordinates. NB: Works
% only in autocalibration mode.
% 'pflat' - 3D points are pflat'ed (default is psphere).
% 'stdlines=X' - standard deviation of line noise
% 'zplane=[index1,index2,...] - index of points on z=0 plane
% Output:
% s - updated structure
% m - updated motion
% residual - total reprojection error
% resnew - calculated residuals
% Default is projective bundle.
warningstate=warning('off');
if ~isa(s,'structure'),
s=structure(s);
end
if ~isa(m,'motion'),
m=motion(m);
end
caliboptions=[];
mode = 1;
modestr={'projective','affine','autocalibration','structure','motion','autocalibration+motion','knownrotation'};
iteration=10;
startlambda=10;
outputon=1;
focal=[];
ar=[];
pp=[];
lockcsystem=0;
lockstr={'coordinate system free','coordinate system locked'};
pflatpoints=0;
stdlines=1; % standard deviation for lines (endpoint distance)
zplane=[];
if nargin>3,
if strmatch('affine',option),
mode = 2;
end
if strmatch('outputoff',option),
outputon=0;
end
if strmatch('iteration=',option),
q=strmatch('iteration=',option);
striter = option{q}(11:length(option{q}));
iteration=str2num(striter);
end
if strmatch('lambda=',option),
q=strmatch('lambda=',option);
strlambda = option{q}(8:length(option{q}));
startlambda=str2num(strlambda);
end
if strmatch('autocalib=',option),
mode = 3;
q=strmatch('autocalib=',option);
strautocalib = option{q}(11:length(option{q}));
caliboptions=[str2num(strautocalib(1)) str2num(strautocalib(2)) str2num(strautocalib(3)) str2num(strautocalib(4)) str2num(strautocalib(5))];
end
if strmatch('structure',option),
mode = 4;
end
if strmatch('motion',option),
if mode==3,
mode=6; %autocalibration+motion
else
mode = 5; %motion (i.e. projective)
end
end
if strmatch('knownrotation',option),
mode=7;
end
if strmatch('focal=',option) & ~isempty(caliboptions),
if caliboptions(1)>1,
q=strmatch('focal=',option);
tmp=option{q};
tmp(end+1)=';';
eval(tmp);
focal(3)=caliboptions(1);
end
end
if strmatch('ar=',option) & ~isempty(caliboptions),
if caliboptions(2)>1,
q=strmatch('ar=',option);
tmp=option{q};
tmp(end+1)=';';
eval(tmp);
ar(3)=caliboptions(2);
end
end
if strmatch('pp=',option) & ~isempty(caliboptions),
if sum(caliboptions(4:5)>1)>0,
q=strmatch('pp=',option);
tmp=option{q};
tmp(end+1)=';';
eval(tmp);
pp(4:5)=caliboptions(4:5);
end
end
if strmatch('lockcsystem',option) & mode == 3,
lockcsystem=1;
end
if strmatch('pflat',option),
pflatpoints=1;
end
if strmatch('stdlines=',option),
q=strmatch('stdlines=',option);
'I accidently removed these lines....'
'redo them before use....'
keyboard;
end
if strmatch('zplane=',option),
q=strmatch('zplane=',option);
tmp=option{q};
tmp(end+1)=';';
eval(tmp);
end
end
% Extract points, lines and conics from imseq
antalbilder = length(imseq);
fs=size(imseq{1});
nbrpts=fs(1); nbrlines=fs(2); nbrconics=fs(3);
pointindex=zeros(1,nbrpts);
lineindex=zeros(1,nbrlines);
conicindex=zeros(1,nbrconics);
nbrfeatures = nbrpts + nbrlines + nbrconics;
mm=cell(antalbilder,1);
ss=cell(1,0);
im=cell(antalbilder,0);
if mode==3 | mode==6,
% P1=getcameras(m,1);
% K1=rq(P1);
% T=[inv(K1)*P1;0,0,0,1];
% m=changecsystem(m,T);
% s=changecsystem(s,T);
for i=1:antalbilder,
Pi=getcameras(m,i);
Ki=rq(Pi);
mm{i}=pmatrix(Pi/Ki(3,3));
end
else
for i=1:antalbilder,
temp=getcameras(m,i);if mode~=2, temp=temp/norm(temp);end
mm{i}=pmatrix(temp);
end
end
nbrpts0=0;nbrlines0=0;nbrconics0=0;
% Add points
for j=1:nbrpts,
temp=getpoints(s,j);
if ~isempty(find(j==zplane))>0,
temp(3)=0;
end
%check that it is visible
visible=0;
for i=1:antalbilder,
sl=getpoints(imseq{i},j);
visible=max(visible,~isnan(sl(1)));
end
if ~isnan(temp(1)) & visible,
nbrpts0=nbrpts0+1;
pointindex(j)=nbrpts0;
if pflatpoints==1,
ss{nbrpts0}=obpoint(pflat(temp));
else
ss{nbrpts0}=obpoint(psphere(temp));
end
for i=1:antalbilder,
[temp,L] = getpoints(imseq{i},j);
if ~isnan(temp(1)),
im{i,nbrpts0} = impoint(pflat(temp),L,[0;0;1]);
end;
end;
end
end;
joffset=nbrpts0;
% Add lines
for j=1:nbrlines,
temp=getlines(s,j);
if ~isnan(temp(1)),
nbrlines0=nbrlines0+1;
lineindex(j)=nbrlines0;
temp=pflat(reshape(temp,4,2));
ss{nbrlines0+joffset}=obline(temp);
for i=1:antalbilder,
% [temp,L] = gethomogeneouslines(imseq{i},j);
temp = gethomogeneouslines(imseq{i},j);
if ~isnan(temp(1)),
temp=temp/norm(temp(1:2));
% im{i,nbrlines0+joffset} = imline(temp,L,[temp(1:2,1);0]);
im{i,nbrlines0+joffset} = imline(temp,stdlines);
end;
end;
end;
end;
joffset=joffset+nbrlines0;
% Add conics
for j=1:nbrconics,
temp=getquadrics(s,j);
if ~isnan(temp(1)),
nbrconics0=nbrconics0+1;
conicindex(j)=nbrconics0;
temp=v2m(temp);
ss{nbrconics0+joffset}=obconic(temp);
for i=1:antalbilder,
[temp,L] = getconics(imseq{i},j);
if ~isnan(temp(1)),
temp=pflat(temp);
ntemp=[0,0,0,0,0,1]';
% temp=temp/norm(temp);
im{i,nbrconics0+joffset} = imconic(temp,L,ntemp);
end;
end;
end
end;
cont=1;
lambda=startlambda;
if outputon==0,
loops=iteration;
else
loops=0;
end;
if lockcsystem>0,
c1=pflat(null(pdp(mm{1})));c2=pflat(null(pdp(mm{2})));
[slask,lockcsystem]=max(abs(c1(1:3)-c2(1:3)));
end
%calculate residuals
[mi,si,imi,offsetmotion,offsetsm,offsetres]=calcindexplc(im,ss,mm,mode,zplane);
resnew=calcresplc(im,ss,mm,imi,si,mi,offsetres,offsetsm,pp,focal,ar);
if outputon,
disp(['Current error: ',num2str(norm(resnew)/sqrt(length(resnew)))]);
plot(resnew,'.');title('Residuals');
disp(['Mode: ',modestr{mode},', ',lockstr{(lockcsystem>0)+1}]);
end;
while cont,
%%%%%%%%%%%%%
% BUNDLE LOOOP
%%%%%%%%%%%%%
for i=loops:-1:1;
if outputon,
disp(['Iterations left: ' num2str(i)]);
end
[ss,mm,step,lambda,oldf,newf,resnew]=bundlestepplc(im,ss,mm,lambda,mode,caliboptions,lockcsystem,pp,focal,ar,zplane);
if outputon,
disp(['Old error: ',num2str(oldf/sqrt(length(resnew))),' New error: ',num2str(newf/sqrt(length(resnew)))]);
disp(['Lognorm of gradient step: ' num2str(log(norm(step)))]);
plot(resnew,'.');title('Residuals');drawnow;
end
end
%%%%%%%%%%%%%
% BUNDLE LOOOP - end
%%%%%%%%%%%%%
if outputon,
disp(['Current lambda: ',num2str(lambda),' Nbr of iterations: ',num2str(iteration)]);
askuser=1;
else
askuser=0;
cont=0;
end;
%%%%%%%%%%%%%
% ASK USER
%%%%%%%%%%%%%
while askuser,
disp('(c)ontinue, (s)et lambda, (i)terations, (k)eyboard');
opt=input('(h)istogram, (l)ock/unlock coordinate system, (q)uit? ','s');
switch opt
case 'c', askuser=0;
case 's', lambda=input('Lambda: ');
case 'i', iteration=input('Iterations: ');
case 'k', keyboard;
case 'h', hist(resnew,21);title('Histogram of residuals');estimestd=sqrt(resnew'*resnew/(size(resnew,1)-3*nbrpts0-4*nbrlines0-9*nbrconics0-15));disp(['Estimated standard deviation: ',num2str(estimestd)]);
case 'l', if lockcsystem==0, c1=pflat(null(pdp(mm{1})));c2=pflat(null(pdp(mm{2}))); [slask,lockcsystem]=max(abs(c1(1:3)-c2(1:3))); else lockcsystem=0;end;if outputon,disp(['Mode: ',modestr{mode},', ',lockstr{(lockcsystem>0)+1}]);end;
case 'q', askuser=0;cont=0;
end
end;
% END of askuser
loops=iteration;
end; %END of cont
% Update structure s and motion m
s=structure;
m=motion;
for j=1:nbrpts;
if pointindex(j)~=0,
s=addpoints(s,udu(ss{pointindex(j)}));
else
s=addpoints(s,NaN*ones(4,1));
end
end;
joffset=nbrpts0;
for j=1:nbrlines;
if lineindex(j)~=0,
s=addlines(s,reshape(udu(ss{lineindex(j)+joffset}),8,1));
else
s=addlines(s,NaN*ones(8,1));
end
end;
joffset=nbrpts0+nbrlines0;
for j=1:nbrconics;
if conicindex(j)~=0,
s=addquadrics(s,m2v(udu(ss{conicindex(j)+joffset})));
else
s=addquadrics(s,NaN*ones(10,1));
end
end;
for i=1:antalbilder;
m=addcameras(m,pdp(mm{i}));
end;
residual=norm(resnew);
warning(warningstate);