-
Notifications
You must be signed in to change notification settings - Fork 326
/
compute_geodesic.m
501 lines (417 loc) · 11.2 KB
/
compute_geodesic.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
function path = compute_geodesic(D, x, options)
% compute_geodesic - extract a discrete geodesic in 2D and 3D
%
% p = compute_geodesic(D,x,options);
%
% D is the distance map.
% x is the starting point.
% p is the shortest path between x and the starting point y (wich should
% satisfy D(y)=0).
%
% Set options.method = 'discrete' if you want to use a pure discrete
% gradient descent.
%
% Copyright (c) 2007 Gabriel Peyre
options.null = 0;
method = getoptions(options, 'method', 'continuous');
if strcmp(method, 'discrete')
path = compute_discrete_geodesic(D,x);
return;
end
if size(x,1)>1 && size(x,2)>1
% several geodesics
if size(x,1)>3
x = x'; % probably user mistake of dimensions
end
path = {};
for i=1:size(x,2)
path{end+1} = compute_geodesic(D, x(:,i), options);
end
return;
end
if size(D,3)>1
% 3D
path = extract_path_3d(D,x,options);
else
% 2D
path = extract_path_2d(D,x,options);
end
if size(path,1)>size(path,2)
path = path';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function path = compute_discrete_geodesic(D,x)
% compute_discrete_geodesic - extract a discrete geodesic in 2D and 3D
%
% path = compute_discrete_geodesic(D,x);
%
% Same as extract_path_xd but less precise and more robust.
%
% Copyright (c) 2007 Gabriel Peyre
nd = 2;
if size(D,3)>1
nd = 3;
end
x = x(:);
path = round(x(1:nd));
% admissible moves
if nd==2
dx = [1 -1 0 0];
dy = [0 0 1 -1];
dz = [0 0 0 0];
d = cat(1,dx,dy);
vprev = D(x(1),x(2));
else
dx = [1 -1 0 0 0 0];
dy = [0 0 1 -1 0 0];
dz = [0 0 0 0 1 -1];
d = cat(1,dx,dy,dz);
vprev = D(x(1),x(2),x(3));
end
s = size(D);
while true
x0 = path(:,end);
x = repmat(x0,1,size(d,2))+d;
if nd==2
I = find(x(1,:)>0 & x(2,:)>0 & x(1,:)<=s(1) & x(2,:)<=s(2) );
else
I = find(x(1,:)>0 & x(2,:)>0 & x(3,:)>0 & x(1,:)<=s(1) & x(2,:)<=s(2) & x(3,:)<=s(3) );
end
x = x(:,I);
if nd==2
I = x(1,:) + (x(2,:)-1)*s(1);
else
I = x(1,:) + (x(2,:)-1)*s(1) + (x(3,:)-1)*s(1)*s(2);
end
[v,J] = min(D(I));
x = x(:,J);
if v>vprev
return;
end
vprev = v;
path(:,end+1) = x;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function path = extract_path_2d(A,end_points,options)
% extract_path_2d - extract the shortest path using
% a gradient descent.
%
% path = extract_path_2d(D,end_point,options);
%
% 'D' is the distance function.
% 'end_point' is ending point (should be integer).
%
% Copyright (c) 2004 Gabriel PeyrŽ
options.null = 0;
if isfield(options, 'trim_path')
trim_path = options.trim_path;
else
trim_path = 1;
end
if isfield(options, 'stepsize')
stepsize = options.stepsize;
else
stepsize = 0.1;
end
if isfield(options, 'maxverts')
maxverts = options.maxverts;
else
maxverts = 10000;
end
str_options = [stepsize maxverts];
if size(end_points,1)~=2
end_points = end_points';
end
if size(end_points,1)~=2
error('end_points should be of size 2xk');
end
% gradient computation
I = find(A==Inf);
J = find(A~=Inf);
A1 = A; A1(I) = mmax(A(J));
global grad;
grad = compute_grad(A1);
grad = -perform_vf_normalization(grad);
% path extraction
path = stream2(grad(:,:,2),grad(:,:,1),end_points(2,:),end_points(1,:), str_options);
for i=1:length(path)
path{i} = path{i}(:,2:-1:1);
end
if length(path)==1
path = path{1};
end
if isfield(options, 'start_points')
start_points = options.start_points;
else
start_points = path(end,:);
end
start_points = start_points(:);
if 0 % trim_path
% removing too verbose points
d = compute_distance_to_points(path', start_points);
% perform thresholding
T = mmax(d)/300^2;
I = find(d<T);
if not(isempty(I))
path = path(1:I(1), :);
path = [path; start_points'];
else
path = path';
end
end
% complete with a discrete extraction (nasty hack)
if size(path, 2)~=2 && size(path, 1)==2
path = path';
end
path = [path; compute_discrete_geodesic(A, round(path(end,:)))'];
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% OLD METHOD USING ODE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if isfield(options, 'Tmax')
Tmax = options.Tmax;
else
Tmax = sum(size(A));
end
global start_points;
if isfield(options, 'start_points')
start_points = options.start_points;
else
start_points = [];
end
x = x(:);
% path is empty
path = x;
% n * mmax(A(J));
% x = xv;
options = odeset('Events',@event_callback);
[T,path] = ode113( @get_gradient, [0,Tmax], x, options);
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% subfunction
function [value,isterminal,direction] = event_callback(t,y)
global start_points;
% compute distance to start points
d = compute_distance_to_points(y,start_points);
value = min(d);
if value<0.1
value = -1;
end
isterminal = 1;
direction = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% subfunction
function g = get_gradient( t, y )
global grad;
gx = grad(:,:,1);
gy = grad(:,:,2);
n = length(gx);
% down/left corner of current cell
p = floor(y(1));
q = floor(y(2));
p = clamp(p,1,n-1);
q = clamp(q,1,n-1);
% residual in [0,1]
xx = y(1)-p;
yy = y(2)-q;
xx = clamp(xx,0,1);
yy = clamp(yy,0,1);
% compute gradient
a = [gx(p,q), gy(p,q)];
b = [gx(p+1,q), gy(p+1,q)];
c = [gx(p,q+1), gy(p,q+1)];
d = [gx(p+1,q+1), gy(p+1,q+1)];
g = ( a*(1-xx)+b*xx )*(1-yy) + ( c*(1-xx)+d*xx )*yy;
g = -g';
function path = extract_path_3d(A,end_points,options)
% extract_path_3d - extract the shortest path using
% a gradient descent.
%
% path = extract_path_3d(A,x,options);
%
% 'A' is the distance function.
% 'x' is starting point (should be integer).
%
% Copyright (c) 2004 Gabriel PeyrŽ
options.null = 0;
if isfield(options, 'trim_path')
trim_path = options.trim_path;
else
trim_path = 1;
end
% gradient computation
I = find(A==Inf);
J = find(A~=Inf);
A1 = A; A1(I) = mmax(A(J));
global gx;
global gy;
global gz;
[gy,gx,gz] = gradient(A1);
% normalize the gradient field
d = sqrt( gx.^2 + gy.^2 + gz.^2 );
I = find(d<eps);
d(I) = 1;
gx = gx./d; gy = gy./d; gz = gz./d;
% path extraction
options = [0.2 20000];
path = stream3(-gy,-gx,-gz,end_points(2,:),end_points(1,:),end_points(3,:), options);
for i=1:length(path)
path{i} = path{i}(:,[2:-1:1 3]);
end
if length(path)==1
path = path{1};
end
% test if path is long enough
v = path(end,:);
v1 = max(round(v),ones(1,3));
if( 0 && A1(v1(1),v1(2),v1(3))>0.1 )
path1 = stream3(-gy,-gx,-gz,v(2),v(1),v(3), options);
for i=1:length(path1)
path1{i} = path1{i}(:,[2:-1:1 3]);
end
if length(path)>=1
path1 = path1{1};
end
path = [path; path1];
end
if isfield(options, 'start_points')
start_points = options.start_points;
else
start_points = path(end,:);
end
start_points = start_points(:);
if trim_path
% removing too verbose points
d = compute_distance_to_points(path', start_points);
% perform thresholding
T = mmax(d)/300^2;
I = find(d<T);
if ~isempty(I)
path = path(1:I(1), :);
path = [path; start_points'];
end
end
function v = mmax(x)
v = max(x(:));
function grad = compute_grad(M,options)
% compute_grad - compute the gradient of an image using central differences
%
% grad = compute_grad(M,options);
%
% 'options' is a structure:
% - options.h is the sampling step size on both direction (default 1).
% - options.h1 is the sampling step size on X direction (default 1).
% - options.h2 is the sampling step size on Y direction (default 1).
% - options.type is the kind of finite difference.
% type==2 is fwd differences, ie.
% y(i) = (x(i)-x(i-1))/h, with special
% care at boundaries.
% type==1 is forward differences bilinearly interpolated in the
% middle of each pixel (be aware that you have a shift of 1/2 on X and Y for
% the location of the gradient).
% type==1 is backward differences bilinearly interpolated in the
% middle of each pixel (be aware that you have a shift of -1/2 on X and Y for
% the location of the gradient).
%
% Copyright (c) 2004 Gabriel Peyré
if nargin<2
options.null = 0;
end
if ~isfield(options, 'h1')
options.h1 = 1;
end
h1 = options.h1;
if ~isfield(options, 'h2')
options.h2 = 1;
end
h2 = options.h2;
if isfield(options, 'h')
h1 = options.h;
h2 = options.h;
end
[n,p] = size(M);
if isfield(options, 'type')
type = options.type;
else
type = 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% new code, use faster 2D differences
if type==1
% central differences on X
D1 = [M(2:end,:);M(end,:)];
D2 = [M(1,:);M(1:end-1,:)];
grad(:,:,1) = (D1-D2)/(2*h1);
grad(1,:,1) = ( 4*M(2,:) - 3*M(1,:) - M(3,:) )/(2*h1);
grad(end,:,1) = -( 4*M(end-1,:) - 3*M(end,:) - M(end-2,:) )/(2*h1);
% central differences on Y
D1 = [M(:,2:end),M(:,end)];
D2 = [M(:,1),M(:,1:end-1)];
grad(:,:,2) = (D1-D2)/(2*h2);
grad(:,1,2) = ( 4*M(:,2) - 3*M(:,1) - M(:,3) )/(2*h2);
grad(:,end,2) = -( 4*M(:,end-1) - 3*M(:,end) - M(:,end-2) )/(2*h2);
elseif type==2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% accumulate on Y
MM = ( M + [M(:,2:end),M(:,end)] )/2;
% fwd differences on X
D1 = [MM(2:end,:);MM(end,:)];
D2 = MM;
grad(:,:,1) = (D1-D2)/h1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% accumulate on X
MM = ( M + [M(2:end,:);M(end,:)] )/2;
% fwd differences on Y
D1 = [MM(:,2:end),MM(:,end)];
D2 = MM;
grad(:,:,2) = (D1-D2)/h2;
elseif type==3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% accumulate on Y
MM = ( M + [M(:,1),M(:,1:end-1)] )/2;
% fwd differences on X
D1 = MM;
D2 = [MM(1,:);MM(1:end-1,:)];
grad(:,:,1) = (D1-D2)/h;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% accumulate on Y
MM = ( M + [M(1,:);M(1:end-1,:)] )/2;
% fwd differences on Y
D1 = MM;
D2 = [MM(:,1),MM(:,1:end-1)];
grad(:,:,2) = (D1-D2)/h;
else
error('This kind of differences is not supported.');
end
return;
if type~=1
% compute the difference in the center of each square
h = zeros(n,p,2);
for j=1:p-1
h(:,j,1) = ( grad(:,j,1)+grad(:,j+1,1) )/2;
end
for i=1:n-1
h(i,:,2) = ( grad(i,:,2)+grad(i+1,:,2) )/2;
end
if type==2 % fwd differences
grad(1:n-1,1:p-1,:) = h(1:n-1,1:p-1,:);
elseif type==3 % bwd differences
grad(2:n,2:p,:) = h(2:n,2:p,:);
end
end
function v2 = perform_vf_normalization(v1)
% perform_vf_normalization - renormalize a vf.
%
% v2 = perform_vf_normalization(v1);
%
% Copyright (c) 2004 Gabriel Peyré
n = sum(v1.^2,3);
I = find(n<eps);
n(I) = 1;
v2 = v1./repmat( sqrt(n), [1 1 2] );