-
Notifications
You must be signed in to change notification settings - Fork 2
/
fitPdmIcp.m
307 lines (262 loc) · 9.12 KB
/
fitPdmIcp.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
%
% Function for fitting a PDM to points using regularised ICP.
%
% Input:
% pdm Point distribution model (PDM) struct with the
% following fields
%
% .meanShape: vector of dimension 3*N, containing the mean
% of all shapes. The first N elements correspond
% to the x components, followed by N elements
% for the y components, and N elements for the
% z components
% .eigVec: 3*N x M matrix, containing in each column one
% mode of variation
% .eigVal: vector of dimension M, containing the variance
% of each mode of variation
% .faces: nFaces x 3 matrix, containing nFaces triangular
% faces, where each row contains three integers
% between 1 and N.
% sparsePoints nPts x 3 matrix containing the points that we want
% to fit the PDM to
% params Structure containing the following parameters (optional)
%
% .paramTolerance [1e-5] tolerance for convergence
% .regulariseFlag [1] toggle regularisation (0 or 1)
% .visualise [1] toggle visualisation (0 or 1)
% .maxIt [100] maximum number of iterations
% .verbose [1] toggle verbose (0 or 1)
% .makeNewFigure [1] toggle creating a new figure
% (0 or 1)
% .constraints [ones(N,nPts)] constraints(i,j) indicates
% whether it is allowed that
% the i-th PDM point is matched
% with the j-th point (0 or 1)
% .alphaInit [zeros(M,1)] initial value of alpha
%
%
% Output:
% alpha: PDM parameter that best fits the PDM to the
% sparsePoints
% alphaHistory: alphaHistory(:,i) denotes alpha at iteration (i-1)
% objectiveValue: objectiveValue(i) denotes the value of the objective at iteration (i-1)
% (for the probabilistic fitting procedure it is the
% value of the Q function, and for icp it is squared error)
% processingTime: sum of total processing time in seconds (per iteration)
%
% Author & Copyright (C) 2017: Florian Bernard (f.bernardpi[at]gmail[dot]com)
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU Affero General Public License as published
% by the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Affero General Public License for more details.
% You should have received a copy of the GNU Affero General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%
function [alpha, alphaHistory, objectiveValue, processingTime] = ...
fitPdmIcp(pdm, sparsePoints, params)
nPts = size(sparsePoints,1);
D = size(sparsePoints,2);
if ( ~exist('params', 'var') )
params = [];
end
if ( ~isfield(params, 'verbose') )
verbose = true;
else
verbose = params.verbose;
end
if ( ~isfield(params, 'makeNewFigure') )
makeNewFigure = 1;
else
makeNewFigure = params.makeNewFigure;
end
if ( ~isfield(params, 'maxIt') )
maxIt = 100;
else
maxIt = params.maxIt;
end
if ( ~isfield(params, 'regulariseFlag') )
regulariseFlag = 1;
else
regulariseFlag = params.regulariseFlag;
end
if ( ~isfield(params, 'paramTolerance') )
paramTolerance = 1e-5;
else
paramTolerance = params.paramTolerance;
end
if ( ~isfield(params, 'visualise') )
visualise = 1;
else
visualise = params.visualise;
end
if ( nargout > 2 || verbose )
computeObjective = 1;
else
computeObjective = 0;
end
invCov = diag(1./pdm.eigVal);
meanShape = pdm.meanShape;
if ( size(meanShape,2) == 1 )
meanShape = reshape(meanShape, numel(meanShape)/D, D);
end
eigVec = pdm.eigVec;
N = size(meanShape,1);
if ( ~isfield(pdm, 'relevantPdmIndices') )
relevantIndices = true(N,1);
else
relevantIndices = pdm.relevantPdmIndices;
end
meanShapeRel = meanShape(relevantIndices,:);
eigVecRel = eigVec(repmat(relevantIndices,D,1),:);
Nrel = size(meanShapeRel,1);
constraints = ones(Nrel,nPts);
if ( isfield(params, 'constraints') )
constraints = params.constraints(relevantIndices,:);
end
if ( isfield(params, 'alphaInit') )
alpha = params.alphaInit;
else
alpha = zeros(size(eigVec,2),1);
end
alphaOld = inf(size(alpha));
alphaHistory = nan(size(eigVec,2), maxIt+1);
objectiveValue = nan(maxIt+1,1);
processingTime = nan(maxIt+1,1);
if ( visualise )
if ( D == 3 )
fvP.vertices = meanShape + reshape(eigVec*alpha,N,3);
fvP.faces = pdm.faces;
if ( makeNewFigure )
figure('Color', 'k','Position', [900 0 900 700]);
else
figure(1);
clf;
set(gcf, 'Color', 'k'); %,'Position', [900 0 900 700]);
end
ax = axes('Color', 'k', 'Position', [0 0 1 1]);
axis off;
axis equal;
hold on;
patch(fvP, 'FaceAlpha', 0.5, 'FaceColor', 'g', 'Tag', 'PDM', 'EdgeColor', 'w', 'LineWidth', 2);
view([pi, -90])
plot3(sparsePoints(:,1),sparsePoints(:,2),sparsePoints(:,3), '.r', 'MarkerSize', 32),
axis tight;
cameratoolbar('SetMode','orbit')
elseif ( D == 2 )
vertices = meanShape + reshape(eigVec*alpha,N,2);
markerSize2d = 24;
lineWidth2d = 2;
if ( makeNewFigure )
figure('Color', 'k','Position', [900 0 900 467]);
else
figure(1);
clf;
set(gcf, 'Color', 'k'); %,'Position', [900 0 900 700]);
end
ax = axes('Color', 'k', 'Position', [0 0 1 1]);
axis off;
axis equal;
hold on;
hold on;
plot(sparsePoints(:,1),sparsePoints(:,2), '.r', ...
'MarkerSize', markerSize2d),
minpts = min(vertices);
maxpts = max(vertices);
xlim([minpts(1)-.4 maxpts(1)+.4]);
ylim([minpts(2)-.4 maxpts(2)+.4]);
end
end
iterTic = tic();
iter=0;
disp('Running ICP Fitting...');
alphaHistory(:,1) = alpha;
if ( computeObjective )
YtransformedRel = reshape(meanShapeRel(:) + eigVecRel*alpha, Nrel, D);
% find correspondences (considering constraints)
pd = pdist2(YtransformedRel, sparsePoints);
pd(~constraints) = inf;
[~,idxXyz] = min(pd);
idxXyz = idxXyz';
if ( D == 3 )
idxCol = [idxXyz, idxXyz + Nrel,idxXyz + 2*Nrel];
elseif ( D == 2 )
idxCol = [idxXyz, idxXyz + Nrel];
end
objectiveValue(1) = ...
norm(vec(sparsePoints - meanShapeRel(idxXyz,:)) - ...
(eigVecRel(idxCol,:)*alpha),2).^2;
processingTime(1) = 0;
if ( verbose )
disp(['error = ' num2str(objectiveValue(1)) ' iter = ' num2str(iter) ' |delta-alpha| = ' ...
num2str(norm(alpha-alphaOld))]);
end
end
%% ICP method
while ( iter < maxIt && ...
norm(alpha-alphaOld) > paramTolerance )
alphaOld = alpha;
YtransformedRel = reshape(meanShapeRel(:) + eigVecRel*alpha, Nrel, D);
% find correspondences (considering constraints)
pd = pdist2(YtransformedRel, sparsePoints);
pd(~constraints) = inf;
[~,idxXyz] = min(pd);
idxXyz = idxXyz';
if ( D == 3 )
idxCol = [idxXyz, idxXyz + Nrel,idxXyz + 2*Nrel];
elseif ( D == 2 )
idxCol = [idxXyz, idxXyz + Nrel];
end
if ( visualise )
delete(findall(gcf,'Tag', 'PDM'));
if ( D == 3 )
fvP.vertices = meanShape + reshape(eigVec*alpha,N,3);
fvP.faces = pdm.faces;
patch(fvP, 'FaceAlpha', 0.5, 'FaceColor', 'g', 'Tag', ...
'PDM', 'EdgeColor', 'w', 'LineWidth', 2);
elseif ( D == 2)
vertices = meanShape + reshape(eigVec*alpha,N,2);
hold on;
plot([vertices(:,1); vertices(1,1)], [vertices(:,2); vertices(1,2)], ...
'g', 'LineWidth', lineWidth2d, 'Tag', 'PDM');
hold on;
plot(vertices(:,1), vertices(:,2), 'g.', 'MarkerSize', markerSize2d,'Tag', 'PDM');
plot(sparsePoints(:,1),sparsePoints(:,2), '.r', 'MarkerSize', markerSize2d),
end
drawnow;
end
% find alpha
b = sparsePoints - meanShapeRel(idxXyz,:);
if ( regulariseFlag )
% use tikhonov regularisation
A = eigVecRel(idxCol,:);
alpha = (A'*A + invCov)\A'*b(:);
else
A = eigVecRel(idxCol,:);
alpha = A\b(:);
end
iter = iter + 1;
alphaHistory(:,iter+1) = alpha;
if ( computeObjective )
objectiveValue(iter+1) = ...
norm(vec(sparsePoints - meanShapeRel(idxXyz,:)) - ...
(eigVecRel(idxCol,:)*alpha),2).^2;
processingTime(iter+1) = toc(iterTic);
if ( verbose )
disp(['error = ' num2str(objectiveValue(iter+1)) ' iter = ' num2str(iter) ' |delta-alpha| = ' ...
num2str(norm(alpha-alphaOld))]);
end
end
end
if ( visualise )
hold off;
end
if ( any( diff(objectiveValue(~isnan(objectiveValue))) > 0 ) )
iters = find(diff(objectiveValue(~isnan(objectiveValue))) > 0);
warning(['error has increased at iterations ' num2str((iters)')]);
end
end