-
Notifications
You must be signed in to change notification settings - Fork 68
/
Animate.m
259 lines (242 loc) · 10 KB
/
Animate.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
%ANIMATE Create an animation
%
% Helper class for creating animations as MP4, animated GIF or a folder of images.
%
% Example::
%
% anim = Animate('movie.mp4');
% for i=1:100
% plot(...);
% anim.add();
% end
% anim.close();
%
% will save the frames in an MP4 movie file using VideoWriter.
%
% Alternatively, to createa of images in PNG format frames named 0000.png,
% 0001.png and so on in a folder called 'frames'
%
% anim = Animate('frames');
% for i=1:100
% plot(...);
% anim.add();
% end
% anim.close();
%
% To convert the image files to a movie you could use a tool like ffmpeg
% ffmpeg -r 10 -i frames/%04d.png out.mp4
%
% Notes::
% - MP4 movies cannot be generated under Linux, a limitation of MATLAB VideoWriter.
%
% Copyright (C) 1993-2019 Peter I. Corke
%
% This file is part of The Spatial Math Toolbox for MATLAB (SMTB).
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
% of the Software, and to permit persons to whom the Software is furnished to do
% so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
% FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
% COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
% IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
% CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
% https://github.com/petercorke/spatial-math
classdef Animate < handle
properties
frame
dir
video % video writing object
opt
profile
end
methods
function a = Animate(filename, varargin)
%ANIMATE.ANIMATE Create an animation class
%
% ANIM = ANIMATE(NAME, OPTIONS) initializes an animation, and creates
% a movie file or a folder holding individual frames.
%
% ANIM = ANIMATE({NAME, OPTIONS}) as above but arguments are passed as a cell array,
% which allows a single argument to a higher-level option like 'movie',M to express
% options as well as filename.
%
% Options::
% 'resolution',R Set the resolution of the saved image to R pixels per inch.
% 'profile',P See VideoWriter for details
% 'fps',F Frame rate (default 30)
% 'bgcolor',C Set background color of axes, 3 vector or MATLAB
% color name.
% 'inner' inner frame of axes; no axes, labels, ticks.
%
% A profile can also be set by the file extension given:
%
% none Create a folder full of frames in PNG format frames named
% 0000.png, 0001.png and so on
% .gif Create animated GIF
% .mp4 Create MP4 movie (not on Linux)
% .avi Create AVI movie
% .mj2 Create motion jpeg file
%
% Notes::
% - MP4 movies cannot be generated under Linux, a limitation of MATLAB VideoWriter.
% - if no extension or profile is given a folder full of frames is created.
% - if a profile is given a movie is created, see VideoWriter for allowable
% profiles.
% - if the file has an extension it specifies the profile.
% - if an extension of '.gif' is given an animated GIF is created
% - if NAME is [] then an Animation object is created but the add() and close()
% methods do nothing.
%
% See also VideoWriter.
if isempty(filename)
% we're not animating
a.dir = [];
else
opt.resolution = [];
opt.profile = [];
opt.fps = 30;
opt.bgcolor = [];
opt.inner = false;
if iscell(filename) && length(varargin) == 0
varargin = filename(2:end);
filename = filename{1};
end
[opt,args] = tb_optparse(opt, varargin);
a.opt = opt;
a.frame = 0;
[p,f,e] = fileparts(filename);
if ~isempty(opt.profile)
% create a video with this profile
a.video = VideoWriter(filename, a.profile, args{:});
fprintf('saving video --> %s with profile ''%s''\n', filename, a.profile);
a.profile = opt.profile
elseif ~isempty(e)
% an extension was given
switch (e)
case {'.mp4', '.m4v'}
if ~(ismac || ispc)
error('SMTB:Animate:nosupported', 'MP4 creation not supported by MATLAB on this platform')
end
profile = 'MPEG-4';
case '.mj2'
profile = 'Motion JPEG 2000';
case '.avi'
profile = 'Motion JPEG AVI';
case {'.gif','.GIF'}
profile = 'GIF';
end
fprintf('Animate: saving video --> %s with profile ''%s''\n', filename, profile);
if strcmp(profile, 'GIF')
a.video = filename;
else
a.video = VideoWriter(filename, profile, args{:});
a.video.FrameRate = opt.fps;
a.video.Quality = 95;
open(a.video);
end
a.profile = profile;
else
% create a folder to hold the frames
a.dir = filename;
mkdir(filename);
% clear out old frames
delete( fullfile(filename, '*.png') );
fprintf('saving frames --> %s\n', filename);
a.profile = 'FILES';
end
end
end
function add(a, fh)
%ANIMATE.ADD Adds current plot to the animation
%
% A.ADD() adds the current figure to the animation.
%
% A.ADD(FIG) as above but captures the figure FIG.
%
% Notes::
% - the frame is added to the output file or as a new sequentially
% numbered image in a folder.
% - if the filename was given as [] in the constructor then no
% action is taken.
%
% See also print.
if isempty(a.dir) && isempty(a.video)
return;
end
if nargin < 2
fh = gcf;
end
if ~isempty(a.opt.bgcolor)
fh.Color = a.opt.bgcolor;
end
ax = gca;
ax.Units = 'pixels';
switch a.profile
case 'FILES'
if isempty(a.opt.resolution)
print(fh, '-dpng', fullfile(a.dir, sprintf('%04d.png', a.frame)));
else
print(fh, '-dpng', sprintf('-r%d', a.opt.resolution), fullfile(a.dir, sprintf('%04d.png', a.frame)));
end
case 'GIF'
if a.opt.inner
im = frame2im( getframe(fh, ax.Position) ); % get the frame
else
im = frame2im(getframe(fh)); % get the frame
end
[A, map] = rgb2ind(im, 256);
if a.frame == 0
imwrite(A, map, a.video, 'gif', 'LoopCount',Inf, 'DelayTime', 1/a.opt.fps);
else
imwrite(A, map, a.video, 'gif', 'WriteMode','append', 'DelayTime', 1/a.opt.fps);
end
otherwise
if a.opt.inner
im = frame2im( getframe(fh, ax.Position) ); % get the frame
else
im = frame2im(getframe(fh)); % get the frame
end
% crop so that height/width are multiples of two, by default MATLAB pads
% with black which gives lines at the edge
w = numcols(im); h = numrows(im);
w = floor(w/2)*2; h = floor(h/2)*2;
im = im(1:h,1:w,:);
% add the frame to the movie
writeVideo(a.video, im)
end
a.frame = a.frame + 1;
end
function out = close(a)
%ANIMATE.CLOSE Closes the animation
%
% A.CLOSE() ends the animation process and closes any output file.
%
% Notes::
% - if the filename was given as [] in the constructor then no
% action is taken.
%
if isempty(a.profile)
out = [];
else
switch a.profile
case {'GIF', 'FILES'}
otherwise
if nargout > 0
out = char(a.video);
end
close(a.video);
end
end
end
end
end