-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundleAdjustment.m
159 lines (101 loc) · 3.83 KB
/
bundleAdjustment.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
function [Rwi_c, Twi_c, Uw] = bundleAdjustment(Rwi_c, Twi_c, Uw, tracks_cur, K, p, nTracks, maxIt, nCam)
% if(length(Rwi_c)~=2 || length(Twi_c)~=2 || length(tracks_cur)~=2)
% error('only two-view-BA is implemented');
% end
lambda = 1e-3;
lambdaMin = 1e-5;
lambdaMax = 1e5;
%compute cost
c = compute_cost(Rwi_c, Twi_c, Uw, tracks_cur, K, p, nTracks, nCam);
fprintf('Iter:\t Error:\t lambda:\n');
fprintf('%d\t%f\t%f\n',0,c,lambda);
for i = 1:maxIt
%compute Jacobian and residuals
r = compute_residuals(Rwi_c, Twi_c, Uw, tracks_cur, K, p, nTracks, nCam);
J = compute_Jacobian(Rwi_c, Twi_c, Uw, tracks_cur, K, p, nTracks, nCam);
%update parameters
[Rwi_c, Twi_c, Uw, c, lambda, success] = updateParameters(Rwi_c, Twi_c, Uw, tracks_cur, K, p, nTracks, r, J, lambda, lambdaMax, lambdaMin, c, i, nCam);
if(~success)
break;
end
end
end
function c = compute_cost(Rwi_c, Twi_c, Uw, tracks_cur, K, p, nTracks,nCam)
r = compute_residuals(Rwi_c, Twi_c, Uw, tracks_cur, K, p, nTracks,nCam);
c = r'*r;
end
function r = compute_residuals(Rwi_c, Twi_c, Uw, tracks_cur, K, p, nTracks,nCam)
r = [];
for i = 1:nCam
p_vis = p{i}(:,tracks_cur{i}.point2D_ids);
Ui = Rwi_c{i}'*(Uw(:,tracks_cur{i}.point3D_ids) - Twi_c{i});
p_vis_pred_hom = K*Ui./repmat(Ui(3,:),3,1);
p_vis_pred = p_vis_pred_hom(1:2,:);
r = [r; p_vis(:) - p_vis_pred(:)];
end
end
function J = compute_Jacobian(Rwi_c, Twi_c, Uw, tracks_cur, K, p, nTracks, nCam)
nb_Pt_2D = 0;
for i = 1:2 %for each camera
nb_Pt_in_current_cam = length(tracks_cur{i}.point3D_ids);
nb_Pt_2D = nb_Pt_2D + nb_Pt_in_current_cam;
end
J = sparse(2*nb_Pt_2D, 6*nCam+3*nTracks);
[G1, G2, G3] = generators_SO3();
l=1;
for i = 1:nCam %for each camera
Ui = Rwi_c{i}'*(Uw(:,tracks_cur{i}.point3D_ids) - Twi_c{i});
nb_Pt_in_current_cam = length(tracks_cur{i}.point3D_ids);
for t = 1 :nb_Pt_in_current_cam %for each 2D point
A = [1/Ui(3,t) 0 -Ui(1,t)/(Ui(3,t)^2); 0 1/Ui(3,t) -Ui(2,t)/(Ui(3,t)^2); 0 0 0];
%3D point derivative
J(l:l+1, 1+ 3*(tracks_cur{i}.point3D_ids(t)-1):3*(tracks_cur{i}.point3D_ids(t))) = K(1:2,:)*A*Rwi_c{i}';
%rotation derivative
J(l:l+1,3*nTracks+1+(i-1)*6:3*nTracks+3+(i-1)*6) = [K(1:2,:)*A*G1'*Ui(:,t), K(1:2,:)*A*G2'*Ui(:,t), K(1:2,:)*A*G3'*Ui(:,t)];
%translation derivative
J(l:l+1,3*nTracks+4+(i-1)*6:3*nTracks+i*6) = -K(1:2,:)*A*Rwi_c{i}';
l = l+2;
end
end
end
function [G1, G2, G3] = generators_SO3()
G1 = [0 0 0; 0 0 -1; 0 1 0];
G2 = [0 0 1; 0 0 0; -1 0 0];
G3 = [0 -1 0; 1 0 0; 0 0 0];
end
function w_hat = HatSO3(w)
w_hat = [0 -w(3) w(2);...
w(3) 0 -w(1);...
-w(2) w(1) 0];
end
function [Rwi_new_c, Twi_new_c, Uw_new, c_new, lambda, success] = updateParameters(Rwi_c, Twi_c, Uw, tracks_cur, K, p, nTracks, r, J, lambda, lambdaMax, lambdaMin, c_prev, iter, nCam)
success = false;
while(lambda<lambdaMax)
%solve linear system
delta = mldivide(J'*J + lambda*eye(3*nTracks + 6*nCam), J'*r);
%update variables
Uw_new = Uw + reshape(delta(1:3*nTracks),3,nTracks);
for i=1:nCam
Rwi_new_c{i} = Rwi_c{i}*expm(HatSO3(delta(3*nTracks+1 + (i-1)*6:3*nTracks+3 + (i-1)*6)));
Twi_new_c{i} = Twi_c{i} + delta(3*nTracks+4+ (i-1)*6:3*nTracks+6 + (i-1)*6);
end
%compute cost
c_new = compute_cost(Rwi_new_c, Twi_new_c, Uw_new, tracks_cur, K, p, nTracks, nCam);
fprintf('%d\t%f\t%f\n',iter,c_new,lambda);
if((c_new+1e-5) < c_prev)
success = true;
if(lambda>lambdaMin)
lambda = lambda/2;
end
break;
else
lambda = lambda*2;
end
end
if(~success)
Rwi_new_c = Rwi_c;
Twi_new_c = Twi_c;
Uw_new = Uw;
c_new = c_prev;
end
end