-
Notifications
You must be signed in to change notification settings - Fork 4
/
triangulate_positions.m
199 lines (157 loc) · 6.3 KB
/
triangulate_positions.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
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
% Triangulate 3D positions using SVD
clear all;
close all;
% camera 1
R1 = [9.6428667991264605e-01 -2.6484969138677328e-01 -2.4165916859785336e-03;
-8.9795446022112396e-02 -3.1832382771611223e-01 -9.4371961862719200e-01;
2.4917459103354755e-01 9.1023325674273947e-01 -3.3073772313234923e-01];
t1 = [1.3305621037591506e-01;
-2.5319578738559911e-01;
2.2444637695699150e+00];
t1 = -inv(R1) * t1;
K1 = [870.14531487461625 0 949.42001822880479;
0 870.14531487461625 487.20049852775117;
0 0 1];
% camera 2
R2 = [9.4962278945631540e-01 3.1338395965783683e-01 -2.6554800661627576e-03;
1.1546856489995427e-01 -3.5774736713426591e-01 -9.2665194751235791e-01;
-2.9134784753821596e-01 8.7966318277945221e-01 -3.7591104878304971e-01];
t2 = [-4.2633372670025989e-02;
-3.5441906393933242e-01;
2.2750378317324982e+00];
t2 = -inv(R2) * t2;
K2 = [893.34367240024267 0 949.96816131377727;
0 893.34367240024267 594.79562177577259;
0 0 1];
% camera 3
R3 = [-9.9541881789113029e-01 3.8473906154401757e-02 -8.7527912881817604e-02;
9.1201836523849486e-02 6.5687400820094410e-01 -7.4846426926387233e-01;
2.8698466908561492e-02 -7.5301812454631367e-01 -6.5737363964632056e-01];
t3 = [-6.0451734755080713e-02;
-3.9533167111966377e-01;
2.2979640654841407e+00];
t3 = -inv(R3) * t3;
K3 = [872.90852997159800 0 944.45161471037636;
0 872.90852997159800 564.47334036925656;
0 0 1];
for i = 0 : 9
figH = figure;
results_path = fullfile(pwd, 'results');
% Plot camera locations
scatter3(t1(1), t1(2), t1(3));
hold on;
scatter3(t2(1), t2(2), t2(3));
hold on;
scatter3(t3(1), t3(2), t3(3));
hold on;
% Plot camera 1 axes
plot3([t1(1) t1(1)+ R1(1, 1)], [t1(2) t1(2) + R1(1, 2)], [t1(3) t1(3) + R1(1, 3)], 'Color', [255, 0, 0] / 255);
plot3([t1(1) t1(1)+ R1(2, 1)], [t1(2) t1(2) + R1(2, 2)], [t1(3) t1(3) + R1(2, 3)], 'Color', [0, 255, 0] / 255);
plot3([t1(1) t1(1)+ R1(3, 1)], [t1(2) t1(2) + R1(3, 2)], [t1(3) t1(3) + R1(3, 3)], 'Color', [0, 0, 255] / 255);
hold on;
% Plot camera 2 axes
plot3([t2(1) t2(1)+ R2(1, 1)], [t2(2) t2(2) + R2(1, 2)], [t2(3) t2(3) + R2(1, 3)], 'Color', [255, 0, 0] / 255);
plot3([t2(1) t2(1)+ R2(2, 1)], [t2(2) t2(2) + R2(2, 2)], [t2(3) t2(3) + R2(2, 3)], 'Color', [0, 255, 0] / 255);
plot3([t2(1) t2(1)+ R2(3, 1)], [t2(2) t2(2) + R2(3, 2)], [t2(3) t2(3) + R2(3, 3)], 'Color', [0, 0, 255] / 255);
hold on;
% Plot camera 3 axes
plot3([t3(1) t3(1)+ R3(1, 1)], [t3(2) t3(2) + R3(1, 2)], [t3(3) t3(3) + R3(1, 3)], 'Color', [255, 0, 0] / 255);
plot3([t3(1) t3(1)+ R3(2, 1)], [t3(2) t3(2) + R3(2, 2)], [t3(3) t3(3) + R3(2, 3)], 'Color', [0, 255, 0] / 255);
plot3([t3(1) t3(1)+ R3(3, 1)], [t3(2) t3(2) + R3(3, 2)], [t3(3) t3(3) + R3(3, 3)], 'Color', [0, 0, 255] / 255);
hold on;
% Get 3D points
f = {strcat('CAM1-', num2str(i), '_cleaned.csv'), strcat('CAM2-', num2str(i), '_cleaned.csv'), strcat('CAM3-', num2str(i), '_cleaned.csv')};
res = triangulate_fn(f, R1, t1, K1, R2, t2, K2, R3, t3, K3);
disp(res);
% Plot trajectory scatter plot for 3D points
p = fullfile(results_path, strcat('3dpts_', num2str(i), '.csv'));
csvwrite(p, res);
scatter3(res(:, 1), res(:, 2), res(:, 3), 'MarkerEdgeColor', [0 0 0.5]);
hold on;
% Plot the table
[x, y] = meshgrid(-2:0.1:2);
tableHeight = 0.09;
s = surf(x, y, tableHeight + zeros(size(x)));
set(s,'EdgeColor','None', 'FaceColor', [0.8 0.8 0.8])
hold on;
% Set axes limits
xlim([-1 2])
ylim([-2.5 2.5])
zlim([-0.5 1.5])
title('Trajectory of ball');
figName = strcat('Trajectory of Ball for Sequence: ', num2str(i));
set(figH, 'NumberTitle', 'off', 'Name', figName);
% Save figure as .jpg
p = fullfile(results_path, strcat('traj_', num2str(i), '.jpg'));
print(figH, '-djpeg', p);
% Plot best fit line with deviation
plotErrorVisualization(i, res, results_path);
end
% Implement triangulation
% Precondition: size(data1, 1) = size(data2, 1) = size(data3, 1)
function points3D = triangulate_fn(f, R1, t1, K1, R2, t2, K2, R3, t3, K3)
% Read data from csv
data_path = fullfile(pwd, 'CleanData');
p = fullfile(data_path, f{1});
points_1 = csvread(p, 1, 4);
p = fullfile(data_path, f{2});
points_2 = csvread(p, 1, 4);
p = fullfile(data_path, f{3});
points_3 = csvread(p, 1, 4);
% Prepare the 2D points
points_1 = prepare_2d_pos(points_1);
points_2 = prepare_2d_pos(points_2);
points_3 = prepare_2d_pos(points_3);
% Triangulate for each point
npts = size(points_1, 1);
points3D = zeros(npts, 3);
% For each point
for i = 1 : npts
pt_cam1 = points_1(i, :);
pt_cam2 = points_2(i, :);
pt_cam3 = points_3(i, :);
[q1, k1] = construct_mat(pt_cam1, R1, t1, K1);
[q2, k2] = construct_mat(pt_cam2, R2, t2, K2);
[q3, k3] = construct_mat(pt_cam3, R3, t3, K3);
Q = [q1; q2; q3];
% Solving Q * res = zeros(6, 1), we get the solution
disp(Q);
x = inv(transpose(Q) * Q) * transpose(Q) * [k1; k2; k3];
disp("LEAST SQUARES OVERDETERMINED");
disp(x);
disp("MATLAB LINEAR LS");
x = lsqlin(Q, [k1; k2; k3]);
disp(x);
disp("Q\c");
disp(Q\[k1; k2; k3]);
disp("SVD");
Q = [q1 -k1; q2 -k2; q3 -k3];
[~, S, V] = svd(Q);
disp(S);
res = V(:, size(V, 2));
res = res / res(4);
res = res(1 : 3);
points3D(i, :) = res;
end
end
% Construct equation for each point
function [mat, k1k2] = construct_mat(pt, R, t, K)
fx = K(1, 1); % scalar
fy = K(2, 2); % scalar
If = R(1, :); % i row vector
Jf = R(2, :); % j row vector
Kf = R(3, :); % k row vector
x0 = K(1, 3) - 960; % scalar offset x
y0 = K(2, 3) - 540; % scalar offset y
x = pt(1); % scalar
y = pt(2); % scalar
A = fx * If - (x - x0) * Kf; % equation for x
B = fy * Jf - (y - y0) * Kf; % equation for y
k1k2 = [dot(A, t); dot(B, t)];
mat = [A; B];
end
% Prepares the 2D co-ordinates by adding a z column with value 1
function points2D = prepare_2d_pos(p)
[r, ~] = size(p);
points2D = [p(:, 1) - 960 p(:, 2) - 540 ones(r, 1)];
end