-
Notifications
You must be signed in to change notification settings - Fork 54
/
ICP.m
319 lines (271 loc) · 7.82 KB
/
ICP.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
function [TR,TT,data] = ICP(model,data,maxIter,minIter,critFun,thres)
% ICP (iterative closest point) algorithm
% # point-to-point distance minimization
% # robust criterion function using IRLS (optional)
%
% Simple usage:
%
% [R,T,data2] = icp(model,data)
%
% ICP fits points in data to the points in model.
% (default) Fit with respect to minimize the sum of square
% errors with the closest model points and data points.
% (optional) Using a robust criterion function
%
% INPUT:
%
% model - matrix with model points, [ X_1 X_2 ... X_M ]
% data - matrix with data points, [ P_1 P_2 ... P_N ]
%
% OUTPUT:
%
% R - rotation matrix
% T - translation vector
% data2 - matrix with transformed data points, [ P_1 P_2 ... P_N ]
%
% data2 = R*data + T
%
%
% Usage:
%
% [R,T,data2] = icp(model,data,maxIter,minIter,critFun,thres)
%
% INPUT:
%
% maxIter - maximum number of iterations. Default = 100
%
% minIter - minimum number of iterations. Default = 5
%
% critFun - 0 Fit with respect to minimize the sum of square errors. (default)
% 1 Huber criterion function (robust)
% 2 Tukey's bi-weight criterion function (robust)
% 3 Cauchy criterion function (robust)
% 4 Welsch criterion function (robust)
%
% thres - error differens threshold to stop iterations. Default = 1e-5
%
% m-file can be downloaded for free at
% http://www.mathworks.com/matlabcentral/fileexchange/12627-iterative-closest-point-method
%
% icp version 1.5
%
% written by Per Bergström 2015-06-16
%
% Reference:
%
% Bergström, P. and Edlund, O. 2014, 'Robust registration of point sets using iteratively reweighted least squares'
% Computational Optimization and Applications, vol 58, no. 3, pp. 543-561., 10.1007/s10589-014-9643-2
%
% Check input arguments
if nargin<2
error('To few input arguments');
elseif nargin<6
thres=1e-5; % threshold to stop icp iterations
if nargin<5
critFun=0; % critFun method, LS
if nargin<4
minIter=5; % min number of icp iterations
if nargin<3
maxIter=100; % max number of icp iterations
end
end
end
end
if or(isempty(model),isempty(data))
error('Something is wrong with the model points and data points');
end
% Use default values
if isempty(maxIter)
maxIter=100;
end
if isempty(minIter)
minIter=5;
end
if isempty(critFun)
critFun=0;
end
if isempty(thres)
thres=1e-5;
end
% Size of model points and data points
if (size(model,2)<size(model,1))
mTranspose=true;
m=size(model,2);
M=size(model,1);
else
mTranspose=false;
m=size(model,1);
M=size(model,2);
end
if (size(data,2)<size(data,1))
data=data';
end
if m~=size(data,1)
error('The dimension of the model points and data points must be equal');
end
N=size(data,2);
% Create closest point search structure
if m<4
if mTranspose
DT=delaunayTriangulation(model);
else
DT=delaunayTriangulation(model');
end
else
DT=[];
resid=zeros(N,1);
vi=ones(N,1);
end
% Initiate weights (Only for robust criterion)
if critFun>0
wghs=ones(N,1);
end
% Initiate transformation
TR=eye(m);
TT=zeros(m,1);
% Start the ICP algorithm
res=9e99;
for iter=1:maxIter
oldres=res;
% Find closest model points to data points
if isempty(DT)
if mTranspose
for i=1:N
mival=9e99;
for j=1:M
val=norm(data(:,i)-model(j,:)');
if val<mival
mival=val;
vi(i)=j;
resid(i)=val;
end
end
end
else
for i=1:N
mival=9e99;
for j=1:M
val=norm(data(:,i)-model(:,j));
if val<mival
mival=val;
vi(i)=j;
resid(i)=val;
end
end
end
end
else
[vi,resid] = nearestNeighbor(DT,data');
end
% Find transformation
switch critFun
case 0
res=mean(resid.^2);
med=mean(data,2);
if mTranspose
mem=mean(model(vi,:),1);
C=data*model(vi,:)-(N*med)*mem;
[U,~,V]=svd(C);
Ri=V*U';
if det(Ri)<0
V(:,end)=-V(:,end);
Ri=V*U';
end
Ti=mem'-Ri*med;
else
mem=mean(model(:,vi),2);
C=data*model(:,vi)'-(N*med)*mem';
[U,~,V]=svd(C);
Ri=V*U';
if det(Ri)<0
V(:,end)=-V(:,end);
Ri=V*U';
end
Ti=mem-Ri*med;
end
otherwise
% Estimation of bound which 80% of data is less than
kRob = 1.9*median(resid);
maxResid=max(resid);
if kRob<1e-6*maxResid
kRob=0.3*maxResid;
elseif maxResid==0
kRob=1;
end
res=mean(resid(resid<1.5*kRob).^2);
switch critFun
case 1
% Huber
kRob=2.0138*kRob;
for i=1:N
if resid(i)<kRob
wghs(i)=1;
else
wghs(i)=kRob/resid(i);
end
end
case 2
% Tukey's bi-weight
kRob=7.0589*kRob;
for i=1:N
if resid(i)<kRob
wghs(i)=(1-(resid(i)/kRob)^2)^2;
else
wghs(i)=0;
end
end
case 3
% Cauchy
kRob=4.3040*kRob;
wghs=1./(1+(resid/kRob).^2);
case 4
% Welsch
kRob=4.7536*kRob;
wghs=exp(-(resid/kRob).^2);
otherwise
% Huber
kRob=2.0138*kRob;
for i=1:N
if resid(i)<kRob
wghs(i)=1;
else
wghs(i)=kRob/resid(i);
end
end
end
suWghs=sum(wghs);
med=(data*wghs)/suWghs;
if mTranspose
mem=(wghs'*model(vi,:))/suWghs;
C=data*(model(vi,:).*repmat(wghs,1,m))-(suWghs*med)*mem;
[U,~,V]=svd(C);
Ri=V*U';
if det(Ri)<0
V(:,end)=-V(:,end);
Ri=V*U';
end
Ti=mem'-Ri*med;
else
mem=(model(:,vi)*wghs)/suWghs;
C=(data.*repmat(wghs',m,1))*model(:,vi)'-(suWghs*med)*mem';
[U,~,V]=svd(C);
Ri=V*U';
if det(Ri)<0
V(:,end)=-V(:,end);
Ri=V*U';
end
Ti=mem-Ri*med;
end
end
data=Ri*data; % Apply transformation
for i=1:m
data(i,:)=data(i,:)+Ti(i); %
end
TR=Ri*TR; % Update transformation
TT=Ri*TT+Ti; %
if iter>=minIter
if abs(oldres-res) < thres
break
end
end
end