-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_BA.m
141 lines (124 loc) · 4.48 KB
/
test_BA.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
% Bundle Adjustment Problem using LSQNONLIN
% -----------------------------------------
% This code implements Bundle Adjustment using a user generated set
% of 3D point coordinates and camera 6D poses. All the parameters
% can be modified to implement whatever situation comes into the
% user's mind.
%
% Author: Riccardo Giubilato, Padova (Italy) 2016
% mail: riccardo.giubilato@gmail.com
% https://www.researchgate.net/profile/Riccardo_Giubilato
% -------------------------------------------------------
% Written and tested on Matlab 2016a
clearvars
close all
nPoints = 100;
nCamera = 8;
depth = 1000; %[mm]
fSx = 1000; %[px/mm*mm]
fSy = 1000;
cx = 1000; %[px]
cy = 500; %[px]
FOV = [80, 60]; %hor, vert [�]
depth_noise = depth/20;
angle_noise = 3*pi/180;
trans_noise = depth/20;
use_finite_diff = 0; % 1 for finite differences, 0 for levenberg-marquardt
%% Pose and Point Cloud Generation
Points = depth*[-0.5+rand(nPoints,2) ones(nPoints,1)];
Points(:,1) = nCamera/5*Points(:,1);
Camera = zeros(4,4,nCamera);
dist_factor = 1*depth; % adjust this parameter to set a correct spacing
% between the cameras
for i=1:nCamera
t = dist_factor*[(-0.5 + (i-1)/nCamera);0;0];
Camera(:,:,i) = [eye(3) t; 0 0 0 1];
end
%% Observation Generation (u,v) coordinates of "image features"
Obs = cell(1,nCamera); %[u,v,pointdIdx]
%Err = cell(1,nCamera);
K = [fSx 0 cx; 0 fSy cy; 0 0 1];
for n=1:nCamera
for i=1:nPoints
if pointIsVisible( Points(i,:)', Camera(1:3,:,n), FOV )
Obs{n}(end+1,:) = [proj( Points(i,:)', Camera(1:3,:,n), K ), i];
%Err{n}(end+1) = reproj (Obs{n}(end,1:2), Points(i,:)', Camera(1:3,:,n), K);
end
end
end
%% Adding noise to Points e Camera
Points = Points + depth_noise*randn(size(Points,1), size(Points, 2));
max_angular_error = angle_noise;
for i=1:nCamera
rotMatrix = eul2rotm(randn(1,3)*max_angular_error);
Camera(1:3,1:3,i) = rotMatrix * Camera(1:3,1:3,i);
Camera(1:3,4,i) = Camera(1:3,4,i) + randn(3,1) * trans_noise;
end
Err = cell(1,nCamera);
for n=1:nCamera
for i=1:size(Obs{n},1)
Err{n}(i) = reproj (Obs{n}(i,1:2), Points(Obs{n}(i,3),:)', Camera(1:3,:,n), K);
end
end
%% Plot before Bundle Adjustment
figure
scatter3(Points(:,1),Points(:,2),Points(:,3),'c')
hold on
axis equal
x=zeros(1,nCamera); for i=1:nCamera, x(i) = Camera(1,4,i); end
y=zeros(1,nCamera); for i=1:nCamera, y(i) = Camera(2,4,i); end
z=zeros(1,nCamera); for i=1:nCamera, z(i) = Camera(3,4,i); end
v=zeros(3,nCamera); for i=1:nCamera, v(:,i) = Camera(1:3,1:3,i)*[0;0;1]; end
quiver3(x,y,z, ...
depth/5*v(1,:),depth/5*v(2,:),depth/5*v(3,:))
title('Points and Camera Views - Before BA')
figure
for i=1:nCamera
subplot(4,nCamera/4,i)
scatter(Obs{i}(:,1),Obs{i}(:,2),'c')
projectons = zeros(nPoints,2);
for j=1:nPoints
if any(Obs{i}(:,3)==j)
projectons(j,:) = proj(Points(j,:)', Camera(1:3,:,i), K);
end
end
hold on
scatter(projectons(:,1),projectons(:,2),'.')
end
title('Camera image frames. Measures and reprojections. Before BA')
%% Bundle Adjustment - reprojection
[Points, Camera] = bundleAdj(Points, Camera, Obs, K, use_finite_diff);
ErrBA = cell(1,nCamera);
for n=1:nCamera
for i=1:size(Obs{n},1)
ErrBA{n}(i) = reproj (Obs{n}(i,1:2), Points(Obs{n}(i,3),:)', Camera(1:3,:,n), K);
end
end
%% Plot after Bundle Adjustment
fprintf('Mean reprojection error before BA: %02d \n', mean(cellfun(@mean,Err)))
fprintf('Mean reprojection error after BA: %02d \n', mean(cellfun(@mean,ErrBA)))
figure
scatter3(Points(:,1),Points(:,2),Points(:,3),'c')
hold on
axis equal
x=zeros(1,nCamera); for i=1:nCamera, x(i) = Camera(1,4,i); end
y=zeros(1,nCamera); for i=1:nCamera, y(i) = Camera(2,4,i); end
z=zeros(1,nCamera); for i=1:nCamera, z(i) = Camera(3,4,i); end
v=zeros(3,nCamera); for i=1:nCamera, v(:,i) = Camera(1:3,1:3,i)*[0;0;1]; end
quiver3(x,y,z, ...
depth/5*v(1,:),depth/5*v(2,:),depth/5*v(3,:))
title('Points and Camera Views - After BA')
figure
for i=1:nCamera
subplot(4,nCamera/4,i)
scatter(Obs{i}(:,1),Obs{i}(:,2),'c')
projections = zeros(nPoints,2);
for j=1:nPoints
if any(Obs{i}(:,3)==j)
projections(j,:) = proj(Points(j,:)', Camera(1:3,:,i), K);
end
end
hold on
scatter(projections(:,1),projections(:,2),'.')
end
title('Camera image frames. Measures and reprojections. After BA')