1
1
% ANIMATE Create an animation
2
2
%
3
- % Helper class for creating animations. Creates a movie file or saves snapshots
4
- % of a figure as individual PNG format frames numbered 0000.png, 0001.png and so
5
- % on.
3
+ % Helper class for creating animations as MP4, animated GIF or a folder of images.
6
4
%
7
5
% Example::
8
6
%
15
13
%
16
14
% will save the frames in an MP4 movie file using VideoWriter.
17
15
%
18
- % Alternatively::
16
+ % Alternatively, to createa of images in PNG format frames named 0000.png,
17
+ % 0001.png and so on in a folder called 'frames'
19
18
%
20
- % anim = Animate('movie ');
19
+ % anim = Animate('frames ');
21
20
% for i=1:100
22
21
% plot(...);
23
22
% anim.add();
24
23
% end
25
24
% anim.close();
26
25
%
27
26
% To convert the image files to a movie you could use a tool like ffmpeg
28
- % ffmpeg -r 10 -i movie/%04d.png out.mp4
27
+ % ffmpeg -r 10 -i frames/%04d.png out.mp4
28
+ %
29
+ % Notes::
30
+ % - MP4 movies cannot be generated under Linux, a limitation of MATLAB VideoWriter.
29
31
%
30
32
31
33
% Copyright (C) 1993-2019 Peter I. Corke
56
58
properties
57
59
frame
58
60
dir
59
- resolution
60
61
video % video writing object
62
+ opt
61
63
profile
62
- fps
63
- bgcolor
64
64
end
65
65
66
66
methods
67
67
function a = Animate(filename , varargin )
68
68
% ANIMATE.ANIMATE Create an animation class
69
69
%
70
- % A = ANIMATE(NAME, OPTIONS) initializes an animation, and creates
70
+ % ANIM = ANIMATE(NAME, OPTIONS) initializes an animation, and creates
71
71
% a movie file or a folder holding individual frames.
72
72
%
73
- % A = ANIMATE({NAME, OPTIONS}) as above but arguments are passed as a cell array,
73
+ % ANIM = ANIMATE({NAME, OPTIONS}) as above but arguments are passed as a cell array,
74
74
% which allows a single argument to a higher-level option like 'movie',M to express
75
75
% options as well as filename.
76
76
%
80
80
% 'fps',F Frame rate (default 30)
81
81
% 'bgcolor',C Set background color of axes, 3 vector or MATLAB
82
82
% color name.
83
+ % 'inner' inner frame of axes; no axes, labels, ticks.
83
84
%
84
85
% A profile can also be set by the file extension given:
85
86
%
86
- % none Create a folder full of frames
87
+ % none Create a folder full of frames in PNG format frames named
88
+ % 0000.png, 0001.png and so on
87
89
% .gif Create animated GIF
88
90
% .mp4 Create MP4 movie (not on Linux)
89
91
% .avi Create AVI movie
90
92
% .mj2 Create motion jpeg file
91
93
%
92
94
% Notes::
95
+ % - MP4 movies cannot be generated under Linux, a limitation of MATLAB VideoWriter.
96
+ % - if no extension or profile is given a folder full of frames is created.
93
97
% - if a profile is given a movie is created, see VideoWriter for allowable
94
98
% profiles.
95
- % - if the file has an extension a movie is created .
99
+ % - if the file has an extension it specifies the profile .
96
100
% - if an extension of '.gif' is given an animated GIF is created
97
- % - otherwise a folder full of frames is created.
101
+ % - if NAME is [] then an Animation object is created but the add() and close()
102
+ % methods do nothing.
98
103
%
99
104
% See also VideoWriter.
100
105
105
110
opt.resolution = [];
106
111
opt.profile = [];
107
112
opt.fps = 30 ;
108
- bgcolor = [];
113
+ opt.bgcolor = [];
114
+ opt.inner = false ;
109
115
110
116
if iscell(filename ) && length(varargin ) == 0
111
117
varargin = filename(2 : end );
112
118
filename = filename{1 };
113
119
end
114
120
[opt ,args ] = tb_optparse(opt , varargin );
115
- a.resolution = opt . resolution ;
121
+ a.opt = opt ;
116
122
a.frame = 0 ;
117
123
118
124
[p ,f ,e ] = fileparts(filename );
140
146
fprintf(' Animate: saving video --> %s with profile ''%s''\n ' , filename , profile );
141
147
if strcmp(profile , ' GIF' )
142
148
a.video = filename ;
143
- a.fps = opt .fps ;
144
149
else
145
150
a.video = VideoWriter(filename , profile , args{: });
146
151
a.video.FrameRate = opt .fps ;
165
170
function add(a , fh )
166
171
% ANIMATE.ADD Adds current plot to the animation
167
172
%
168
- % A.ADD() adds the current figure in PNG format to the animation
169
- % folder with a unique sequential filename.
173
+ % A.ADD() adds the current figure to the animation.
170
174
%
171
175
% A.ADD(FIG) as above but captures the figure FIG.
172
176
%
177
+ % Notes::
178
+ % - the frame is added to the output file or as a new sequentially
179
+ % numbered image in a folder.
180
+ % - if the filename was given as [] in the constructor then no
181
+ % action is taken.
182
+ %
173
183
% See also print.
174
184
175
185
if isempty(a .dir ) && isempty(a .video )
@@ -180,30 +190,37 @@ function add(a, fh)
180
190
fh = gcf ;
181
191
end
182
192
183
- if ~isempty(a .bgcolor )
184
- fh.Color = a .bgcolor ;
193
+ if ~isempty(a .opt . bgcolor )
194
+ fh.Color = a .opt . bgcolor ;
185
195
end
186
196
ax = gca ;
187
197
ax.Units = ' pixels' ;
188
198
switch a .profile
189
199
case ' FILES'
190
- if isempty(a .resolution )
200
+ if isempty(a .opt . resolution )
191
201
print(fh , ' -dpng' , fullfile(a .dir , sprintf(' %04d .png' , a .frame )));
192
202
else
193
- print(fh , ' -dpng' , sprintf(' -r%d ' , a .resolution ), fullfile(a .dir , sprintf(' %04d .png' , a .frame )));
203
+ print(fh , ' -dpng' , sprintf(' -r%d ' , a .opt . resolution ), fullfile(a .dir , sprintf(' %04d .png' , a .frame )));
194
204
end
195
205
case ' GIF'
196
- im = frame2im(getframe(fh )); % get the frame
206
+ if a .opt .inner
207
+ im = frame2im( getframe(fh , ax .Position ) ); % get the frame
208
+ else
209
+ im = frame2im(getframe(fh )); % get the frame
210
+ end
197
211
[A , map ] = rgb2ind(im , 256 );
198
212
if a .frame == 0
199
- imwrite(A , map , a .video , ' gif' , ' LoopCount' ,Inf , ' DelayTime' , 1 / a .fps );
213
+ imwrite(A , map , a .video , ' gif' , ' LoopCount' ,Inf , ' DelayTime' , 1 / a .opt . fps );
200
214
else
201
- imwrite(A , map , a .video , ' gif' , ' WriteMode' ,' append' , ' DelayTime' , 1 / a .fps );
215
+ imwrite(A , map , a .video , ' gif' , ' WriteMode' ,' append' , ' DelayTime' , 1 / a .opt . fps );
202
216
end
203
217
otherwise
204
218
205
- im = frame2im( getframe(fh , ax .Position ) ); % get the frame
206
-
219
+ if a .opt .inner
220
+ im = frame2im( getframe(fh , ax .Position ) ); % get the frame
221
+ else
222
+ im = frame2im(getframe(fh )); % get the frame
223
+ end
207
224
% crop so that height/width are multiples of two, by default MATLAB pads
208
225
% with black which gives lines at the edge
209
226
w = numcols(im ); h = numrows(im );
@@ -219,8 +236,11 @@ function add(a, fh)
219
236
function out = close(a )
220
237
% ANIMATE.CLOSE Closes the animation
221
238
%
222
- % A.CLOSE() closes the video file.
239
+ % A.CLOSE() ends the animation process and closes any output file.
223
240
%
241
+ % Notes::
242
+ % - if the filename was given as [] in the constructor then no
243
+ % action is taken.
224
244
%
225
245
if isempty(a .profile )
226
246
out = [];
0 commit comments