forked from pingwangsky/PnP_tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_noise_planar.m
132 lines (105 loc) · 3.76 KB
/
main_noise_planar.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
close all;
clc;
clear;
addpath others;
addpath epnp;
addpath lhm;
addpath RPnP;
addpath SRPnP;
addpath OPnP;
addpath dls_pnp_matlab;
warning off;
npts= 4:1:20;
num= 500;
nl=2;
% compared methods
A= zeros(size(npts));
B= zeros(num,1);
name= {'LHM', 'EPnP', 'RPnP', 'DLS', 'OPnP','SRPnP'};
f= {@LHM, @EPnP, @RPnP, @dls_pnp_all,@OPnP,@SRPnP};
marker= {'+', 'o', 'd', '^', 's', '*',};
color= {'m','g','b','c','k',[1,0.502,0]};
markerfacecolor= {'m','n','n','n','n',[1,0.502,0]};
linestyle= {'-','-','-','-','-','-'};
method_list= struct('name', name, 'f', f, 'mean_r', A, 'mean_t', A,...
'med_r', A, 'med_t', A, 'std_r', A, 'std_t', A, 'r', B, 't', B,...
'marker', marker, 'color', color, 'markerfacecolor', markerfacecolor, 'linestyle', linestyle);
% experiments
for i= 1:length(npts)
npt= npts(i);
fprintf('npt = %d: ',npt);
for k= 1:length(method_list)
method_list(k).r = zeros(1,num);
method_list(k).t = zeros(1,num);
end
index_fail = [];
for j= 1:num
% camera's parameters
width= 640;
height= 480;
f= 800;
% generate 3d coordinates in camera space
XXw= [xrand(2,npt,[-2 2]); zeros(1,npt)];
R= rodrigues(randn(3,1));
t= [rand-0.5;rand-0.5;rand*8+4];
Xc= R*XXw+repmat(t,1,npt);
% projection
xx= [Xc(1,:)./Xc(3,:); Xc(2,:)./Xc(3,:)]*f;
xxn= xx+randn(2,npt)*nl;
xxn=xxn./f;
% pose estimation
for k= 1:length(method_list)
try
[R1,t1]= method_list(k).f(XXw,xxn);
catch
fprintf([' The solver - ',method_list(k).name,' - encounters internal errors! \n']);
index_fail = [index_fail, j];
break;
end
%no solution
if size(t1,2) < 1
fprintf([' The solver - ',method_list(k).name,' - returns no solution! \n']);
index_fail = [index_fail, j];
break;
end
%choose the solution with smallest error
error = inf;
for jjj = 1:size(R1,3)
tempy = cal_pose_err([R1(:,:,jjj) t1(:,jjj)],[R t]);
if sum(tempy) < error
y = tempy;
error = sum(tempy);
end
end
method_list(k).r(j)= y(1);
method_list(k).t(j)= y(2);
end
showpercent(j,num);
end
fprintf('\n');
% save result
for k= 1:length(method_list)
method_list(k).r(index_fail) = [];
method_list(k).t(index_fail) = [];
method_list(k).mean_r(i)= mean(method_list(k).r);
method_list(k).mean_t(i)= mean(method_list(k).t);
method_list(k).med_r(i)= median(method_list(k).r);
method_list(k).med_t(i)= median(method_list(k).t);
method_list(k).std_r(i)= std(method_list(k).r);
method_list(k).std_t(i)= std(method_list(k).t);
end
end
yrange= [0 4];
i= 0; w= 400; h= 300;
figure('color','w','position',[w*i,100,w,h]);i=i+1;
xdrawgraph(npts,yrange,method_list,'mean_r','Mean Rotation Error',...
'Number of Points','Rotation Error (degrees)');
figure('color','w','position',[w*i,100,w,h]);i=i+1;
xdrawgraph(npts,yrange,method_list,'med_r','Median Rotation Error',...
'Number of Points','Rotation Error (degrees)');
figure('color','w','position',[w*i,100,w,h]);i=i+1;
xdrawgraph(npts,yrange,method_list,'mean_t','Mean Translation Error',...
'Number of Points','Translation Error (%)');
figure('color','w','position',[w*i,100,w,h]);i=i+1;
xdrawgraph(npts,yrange,method_list,'med_t','Median Translation Error',...
'Number of Points','Translation Error (%)');