-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_synth.m
90 lines (76 loc) · 2.71 KB
/
test_synth.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
% -------------------------------------------------------------------------
% Program Introdution:
% This program is a benchmark simulation to verify the 8-point algorithm.
%
% Prgram flow
% Fix the first camera at the origin and randomly generate a relative pose
% for the second camera. Both cameras are assumed as calibrated, where the
% intrinsic matrix is an identify matrix. Then randomly generate 8 points
% in space and project them onto both cameras to obtain matched points in
% two views. 8-point algorithm is then carried out to compute the essential
% matrix and the result is justified.
%
% Key variables
% thetad - rotation angle in degrees
% theta - rotation angle in radians
% axis - the rotation axis in space, a 3-vector
% R - rotation matrix
% t - translation vector
% p1 - the projection matrix for the first camera
% p2 - the projection matrix for the second camera
% X - points in space, homogeneous coordinate
% x1 - coordinates of matched features in the first view
% x2 - coordinates of matched features in the second view
% eMatrix - the ground truth of essential matrix
% eMatrix8 - the essential matrix computed using 8-point algorithm
%
% Author: Frederic Zhang
% Last modified: 15 June 2017
% Version: 2.0
% -------------------------------------------------------------------------
close all;
clear;
clc;
% Intrinsic matrix
K = eye(3);
% Rotation angle
thetad = (rand(1) - 0.5) * 360; % random rotation angle -180~180
theta = thetad * pi / 180; % rotation angle in radians
% Rotation axis
axis = rand(1, 3);
% Normalize the vector
axis = axis / sqrt(axis(1) ^ 2 + axis(2) ^ 2 + axis(3) ^ 2);
% Corss product of rotaion vector
v = [0, -axis(3), axis(2); axis(3), 0, -axis(1); -axis(2), axis(1), 0];
% Rotation matrix according to Rodrigues' Formula
R = eye(3, 3) + sin(theta) * v + (1 - cos(theta)) * v * v;
% Translation vector
t = [(rand(2, 1) - 0.5) / 10; 1];
% Normalize the last component
t = t / t(3);
% Compute the essential matrix with R and t
tc = [0, -t(3), t(2); t(3), 0, -t(1); -t(2), t(1), 0];
eMatrix = tc * R;
% Normalize the last element
eMatrix = eMatrix / eMatrix(3, 3);
% Projection matrix
p1 = K * [eye(3, 3), [0; 0; 0]];
p2 = K * [R, t];
% 3D points generation
X = rand(4, 8);
% Projection
x1 = p1 * X;
x2 = p2 * X;
% Transformation into inhomogeneous coordinate
x1 = x1 ./ repmat(x1(3, :), [3, 1]);
x2 = x2 ./ repmat(x2(3, :), [3, 1]);
x1 = transpose(x1(1:2, :));
x2 = transpose(x2(1:2, :));
% Compute essential matrix using 8-point algorithm
eMatrix8 = eightPoint(x1, x2, K, K);
eMatrix8 = eMatrix8 / eMatrix8(3, 3);
% Varification
disp('The ground truth of essential matrix is:');
disp(eMatrix);
disp('The essential matrix computed using 8-point algorithm is:');
disp(eMatrix8);