Skip to content

Commit 92f5747

Browse files
committed
Change switches for animation
Add ability to save a movie
1 parent 0534994 commit 92f5747

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

distancexform.m

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,31 @@
1414
% Options:
1515
% 'euclidean' Use Euclidean (L2) distance metric (default)
1616
% 'cityblock' Use cityblock or Manhattan (L1) distance metric
17-
% 'show',D Show the iterations of the computation, with a delay of D seconds
18-
% between frames.
17+
%
18+
% 'animate' Show the iterations of the computation
19+
% 'delay',D Delay of D seconds between animation frames (default 0.2s)
20+
% 'movie',M Save animation to a movie file or folder
21+
%
1922
% 'noipt' Don't use Image Processing Toolbox, even if available
2023
% 'novlfeat' Don't use VLFeat, even if available
2124
% 'nofast' Don't use IPT, VLFeat or imorph, even if available.
2225
%
26+
% 'delay'
27+
%
2328
% Notes::
2429
% - For the first case Image Processing Toolbox (IPT) or VLFeat will be used if
2530
% available, searched for in that order. They use a 2-pass rather than
2631
% iterative algorithm and are much faster.
2732
% - Options can be used to disable use of IPT or VLFeat.
2833
% - If IPT or VLFeat are not available, or disabled, then imorph is used.
2934
% - If IPT, VLFeat or imorph are not available a slower M-function is used.
30-
% - If the 'show' option is given then imorph is used.
31-
% - Using imorph requires iteration and is slow.
35+
% - If the 'animate' option is given then the MATLAB implementation is used.
36+
% - Using imorph requires iteration and is slow.
3237
% - For the second case the Machine Vision Toolbox function imorph is required.
3338
% - imorph is a mex file and must be compiled.
3439
% - The goal is given as [X,Y] not MATLAB [row,col] format.
3540
%
36-
% See also IMORPH, DXform.
41+
% See also IMORPH, DXform, Animate.
3742

3843

3944

@@ -58,15 +63,22 @@
5863

5964
function dx = distancexform(occgrid, varargin)
6065

61-
opt.show = 0;
66+
opt.delay = 0.2;
6267
opt.ipt = true;
6368
opt.vlfeat = true;
6469
opt.fast = true;
6570
opt.metric = {'euclidean', 'cityblock'};
71+
opt.animate = false;
72+
opt.movie = [];
6673
[opt,args] = tb_optparse(opt, varargin);
67-
if opt.show
74+
if opt.movie
75+
opt.animate = true;
76+
end
77+
if opt.animate
78+
opt.fast = false;
6879
opt.ipt = false;
6980
opt.vlfeat = false;
81+
clf
7082
end
7183
count = [];
7284
switch opt.metric
@@ -111,14 +123,14 @@
111123

112124
occgrid = imorph(occgrid, m, 'plusmin');
113125
count = count+1;
114-
if opt.show
126+
if opt.animate
115127
cmap = [1 0 0; gray(count)];
116128
colormap(cmap)
117129
image(occgrid+1, 'CDataMapping', 'direct');
118130
set(gca, 'Ydir', 'normal');
119131
xlabel('x');
120132
ylabel('y');
121-
pause(opt.show);
133+
pause(opt.delay);
122134
end
123135

124136
ninfnow = sum( isinf(occgrid(:)) ); % current number of Infs
@@ -152,20 +164,25 @@
152164

153165
count = 0;
154166
ninf = Inf; % number of infinities in the map
167+
anim = Animate(opt.movie);
155168
while 1
156169

157170
occgrid = dxstep(occgrid, m);
158171
occgrid(nans) = NaN;
159172

160173
count = count+1;
161-
if opt.show
174+
if opt.animate
162175
cmap = [1 0 0; gray(count)];
163176
colormap(cmap)
164177
image(occgrid+1, 'CDataMapping', 'direct');
165178
set(gca, 'Ydir', 'normal');
166179
xlabel('x');
167180
ylabel('y');
168-
pause(opt.show);
181+
if opt.animate
182+
anim.add();
183+
else
184+
pause(opt.delay);
185+
end
169186
end
170187

171188
ninfnow = sum( isinf(occgrid(:)) ); % current number of Infs
@@ -176,10 +193,11 @@
176193
end
177194
ninf = ninfnow;
178195
end
196+
anim.close();
179197
dx = occgrid;
180198
end
181199

182-
if opt.show && ~isempty(count)
200+
if opt.animate && ~isempty(count)
183201
fprintf('%d iterations, %d unreachable cells\n', count, ninf);
184202
end
185203

@@ -200,6 +218,7 @@
200218
occgrid(isfinite(occgrid)) = 0;
201219

202220
count = 0;
221+
anim = Animate(opt.movie);
203222
while 1
204223
occgrid = imorph(occgrid, m, 'plusmin');
205224
count = count+1;
@@ -210,7 +229,11 @@
210229
set(gca, 'Ydir', 'normal');
211230
xlabel('x');
212231
ylabel('y');
213-
pause(opt.show);
232+
if opt.animate
233+
anim.add();
234+
else
235+
pause(opt.delay);
236+
end
214237
end
215238

216239
ninfnow = sum( isinf(occgrid(:)) ); % current number of Infs
@@ -219,6 +242,7 @@
219242
break;
220243
end
221244
end
245+
anim.close();
222246
dx = occgrid;
223247
elseif exist('bwdist') && opt.ipt
224248
if opt.verbose

0 commit comments

Comments
 (0)