-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRandom3Plus1.m
More file actions
296 lines (236 loc) · 7.33 KB
/
Random3Plus1.m
File metadata and controls
296 lines (236 loc) · 7.33 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
% Random3Plus1 generate a random configuration for the 3+1 problem
%
% Construct a random set of point correspondences and a directional
% correspondence for the 3+1 problem.
%
% draw - 1 = visualize the scene, 0 = do not visualize
% nr_points - number of image correspondences to create (3 for 3+1)
% stddev_pix - pixel error in normalized camera coordinates
% fov_degrees - camera field of view
% translation_direction - 0 = random, 1 = [1,0,0]' (to the right), 2 =
% [0,0,1]' (forward)
% min_depth - minimum depth of 3D points
% max_depth - maximum depth of 3D points
% stdev_g - error in directional correspondence in normalized camera
% coordinates
%
% Uses global scale of the right pose (should be set to 1)
% global scale
%
% Output:
% Q - image points in the first camera
% Qp - corresponding image points in the second camera
% G - direction vector in the first camera
% Gp - corresponding direction vector in the second camera
% E - essential matrix
% P3D - 3D points from which the correspondences were generated
% P - first camera matrix (always [I,0]
% Pp - second camera matrix
%
% When the scene is plotted, the color scheme is as follows:
% Green line segments: directional correspondence
% Red XYZ coordinate frame: first camera
% Blue XYZ coordinate frame: second camera
% Yellow segments: image point vectors in first camera
% Magenta segments: image point vectors in second camera
% Blue stars: 3D points
%
% To test the algorithm use:
% [Q,Qp,G,Gp,E,P3D,P,Pp] = Random3Plus1(1,3);
% Pstack = Solve3Plus1Action(Q,Qp,G,Gp);
% The columns of Pstack columns correspond to solutions for Pp in
% row order.
%
% Author: Oleg Naroditsky
function [Q,Qp,G,Gp,E,P3D,P,Pp] = Random3Plus1(draw,nr_points,stdev_pix,fov_degrees,translation_direction,min_depth,max_depth,stdev_g)
if ~exist('stdev_pix','var')
% noise parameters:
stdev_pix = 0.0;
end
if ~exist('translation_direction','var')
% translation_direction: 0 - random
translation_direction = 0;
end
if ~exist('fov_degrees','var')
% camera field of view in degrees
fov_degrees = 30;
end
if ~exist('min_depth','var')
% minimum depth of 3D points from first camers
min_depth = 3;
end
if ~exist('max_depth','var')
% maximum depth of 3D points from first camers
max_depth = 10;
end
if ~exist('stdev_g','var')
% maximum depth of 3D points from first camers
stdev_g = 0;
end
im_w = 640;
nx = tan(fov_degrees/2*pi/180)*2;
stdev = nx/im_w*stdev_pix;
g = rand(3,1);
g = g/norm(g);
% generate a set of 3d points:
P3D = Make3DPoints(fov_degrees,min_depth,max_depth,nr_points);
Pp = MakeRandomPose(translation_direction);
% left pose:
R = eye(3);%orth(rand(3));
t = [0,0,0]';
P = [R, t];
if 0
% right pose:
in_front = 0; % how many points are in front of the camera:
while in_front ~= nr_points
Pp = MakeRandomPose(translation_direction);
Qp = Pp*P3D;
in_front = sum(Qp(3,:) > 0);
end
end;
% rotation in right cam
Rp = Pp(1:3,1:3);
tp = Pp(1:3,4);
% generate a random rotation with deviation stdev_r:
g_noise = expm(randn*stdev_g*pi/180*skew3((rand(3,1)-0.5)*2));
% direction in left coords
G = R*g;
% direction in the right camera:
Gp = Rp*g;
% contaminate direction with noise:
Gp = g_noise*Gp;
% left image points
Q = P*P3D;
Q = Q./repmat(Q(3,:), 3,1);
Q(1:2,:) = Q(1:2,:)+ randn(2,size(Q,2))*stdev;
% right image points
Qp = Pp*P3D;
Qp = Qp./repmat(Qp(3,:),3,1);
Qp(1:2,:) = Qp(1:2,:)+ randn(2,size(Q,2))*stdev;
Pinv = inv([P;[0,0,0,1]]);
Pinv = Pinv(1:3,:);
Ppinv = inv([Pp;[0,0,0,1]]);
Ppinv = Ppinv(1:3,:);
if draw
ViewPose(Pinv, 'r');ViewPose(Ppinv,'b');
PlotVector(G(1:3),Pinv, 'g',0);
PlotVector(Gp(1:3),Ppinv, 'g',0);
axis equal;
hold on;
% plot3(P3D(1,1:3),P3D(2,1:3),P3D(3,1:3),'b*');
plot3(P3D(1,1:end),P3D(2,1:end),P3D(3,1:end),'b*');
PlotVector(Q(:,1),Pinv,'y',1);
PlotVector(Q(:,2),Pinv,'y',1);
PlotVector(Q(:,3),Pinv,'y',1);
PlotVector(Qp(:,1),Ppinv,'m',1);
PlotVector(Qp(:,2),Ppinv,'m',1);
PlotVector(Qp(:,3),Ppinv,'m',1);
% calibration matrix for visualization only
K = [0.5,0,0.5;0,0.5,0.5;0,0,1];
cameraBox(1,1,K,P,0.3,'g');
cameraBox(1,1,K,Pp,0.3,'g');
hold off;
end;
% normalized the translation since it's recovered up to scale:
Pp(1:3,4) = Pp(1:3,4)/norm(Pp(1:3,4));
Rp = Pp(1:3,1:3);
tp = Pp(1:3,4);
% essential matrix
E = skew3(R'*tp)*(R'*Rp);
P = [P;0,0,0,1];
Pp = [Pp;0,0,0,1];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% translation_direction: 0 - random, 1 - [1,0,0]' (to the right), 2 = [0,0,1]' (forward)
function Pp = MakeRandomPose(translation_direction)
global scale;
scale = 1;
% rotation in right cam
Rp = orth(rand(3));
Rp = ang2orth((rand(1,3)-0.5).*[100,180,100].*pi/180);
% right pose:
if translation_direction == 0
tp = rand(3,1)*4-2;
tp = tp/norm(tp)*scale;
else
if translation_direction == 1
tp = -Rp*[1,0,0]'*scale;
else
if translation_direction == 2
tp = -Rp*[0,0,1]'*scale;
end;
end;
end;
Pp = [Rp,tp];
function [Qr] = ang2orth(theta)
% ANG2ORTH generate orthogonal matrix from given angles
Qr = eye(3);
n = 1;
for i = 2 : -1 : 1
for j = 3 : -1 : i + 1
t = theta(n);
s = sin(t); c = cos(t);
U = eye(3);
U(i,i) = c; U(i,j) = s;
U(j,i) = -s; U(j,j) = c;
Qr = U * Qr; n = n + 1;
end
end
% generate nr_points random 3D points for a camera with fov in degrees, with depth
% limits min_depth and max_depth
function P3D = Make3DPoints(fov,min_depth,depth,nr_points)
P3D = ones(4,nr_points);
c = tan(fov*pi/180/2);
P3D(3,:) = [min_depth+1+rand(1,nr_points)*(depth-min_depth)];
P3D(1:2,:) = [2*(rand(2,nr_points)-0.5)*c].*[P3D(3,:);P3D(3,:)];
function ViewPose(P, c)
unhold = 0;
if ~ishold
hold on;
unhold = true;
end
X = [1 0 0 0 0;
0 0 1 0 0;
0 0 0 0 1;
1 1 1 1 1];
Xc = P*X;
plot3(Xc(1,:), Xc(2,:), Xc(3,:), c);
text(Xc(1,1),Xc(2,1),Xc(3,1), 'x');
text(Xc(1,3),Xc(2,3),Xc(3,3), 'y');
text(Xc(1,5),Xc(2,5),Xc(3,5), 'z');
if unhold
hold off;
end
function wBounds=cameraBox(w,h,K,P, scale,col)
imBounds = [0, w, w 0;
0, 0, h, h;
1, 1, 1, 1;
1, 1, 1, 1];
K(1:2,1:2) = K(1:2,1:2)/scale;
P(4,1:4) = [0,0,0,1];
K(4,1:3) = [0,0,0];
K(1:4,4) = [0,0,0,1]';
wBounds = inv(P)*inv(K)*imBounds;
width = 2;
line([wBounds(1,1), wBounds(1,2)], [wBounds(2,1), wBounds(2,2)],[wBounds(3,1), wBounds(3,2)],'Color',col,'LineWidth',width);
line([wBounds(1,2), wBounds(1,3)], [wBounds(2,2), wBounds(2,3)],[wBounds(3,2), wBounds(3,3)],'Color',col,'LineWidth',width);
line([wBounds(1,3), wBounds(1,4)], [wBounds(2,3), wBounds(2,4)],[wBounds(3,3), wBounds(3,4)],'Color',col,'LineWidth',width);
line([wBounds(1,4), wBounds(1,1)], [wBounds(2,4), wBounds(2,1)],[wBounds(3,4), wBounds(3,1)],'Color',col,'LineWidth',width);
function PlotVector(V,P,c,normalize)
if normalize
for i = 1:size(V,2)
if norm(V(:,i)) ~= 0
V(:,i) = V(:,i)/norm(V(:,i));
end;
end;
end;
v = P*[[V;1],[0,0,0,1]'];
unhold = 0;
if ~ishold
hold on;
unhold = true;
end
%arrow3d(v(:,2)',v(:,1)', 15, 'cylinder',[0.1,0.1]);
plot3(v(1,:),v(2,:),v(3,:), c);
if unhold
hold off;
end