-
Notifications
You must be signed in to change notification settings - Fork 97
/
Movie.m
291 lines (235 loc) · 8.5 KB
/
Movie.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
%MOVIE Class to read movie file
%
% A concrete subclass of ImageSource that acquires images from a web camera
% built by Axis Communications (www.axis.com).
%
% Methods::
% grab Aquire and return the next image
% size Size of image
% close Close the image source
% char Convert the object parameters to human readable string
% skipttotime X
% skiptoframe X
%
% Properties::
% curFrame The index of the frame just read
% totalDuration The running time of the movie (seconds)
%
% See also ImageSource, Video.
%
%
% SEE ALSO: Video
% Copyright (C) 1993-2011, by Peter I. Corke
%
% This file is part of The Machine Vision Toolbox for Matlab (MVTB).
%
% MVTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% MVTB 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 Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with MVTB. If not, see <http://www.gnu.org/licenses/>.
classdef Movie < ImageSource
properties
rate % frame rate at which movie was captured
nframes;
totalDuration % in seconds
skippedFrames
curFrame
skip
movie
fullfilename
end
methods
function m = Movie(filename, varargin)
%Movie.Movie Image source constructor
%
% M = Movie(FILE, OPTIONS) is an Movie object that returns frames
% from the movie file FILE.
%
% Options::
% 'uint8' Return image with uint8 pixels (default)
% 'float' Return image with float pixels
% 'double' Return image with double precision pixels
% 'grey' Return greyscale image
% 'gamma',G Apply gamma correction with gamma=G
% 'scale',S Subsample the image by S in both directions
% 'skip',S Read every S'th frame from the movie
% invoke the superclass constructor and process common arguments
m = m@ImageSource(varargin{:});
m.curFrame = 1;
% open the movie file and copy some of its parameters to object
% properties
% see if it exists on the MATLAB search path
% p = fileparts( which('iread') );
% pth = [ fullfile(p, 'images') path2cell(path)];
% for p=pth
% fname = fullfile(p{1}, filename);
% if exist( fname ) > 0
% m.fullfilename = fullfile(p{1}, filename);
% m.movie = VideoReader(m.fullfilename);
% break;
% end
% end
if exist( filename, 'file' ) ~= 2
% file is not here, download it
if ~fileonserver(filename)
error('MVTB:badarg:nosuchfile', 'Can''t find file: %s', filename);
end
getfromserver(filename);
end
m.fullfilename = filename;
m.movie = VideoReader(m.fullfilename);
if isempty(m.movie)
error('MVTB:badarg:nosuchfile', 'Can''t find file: %s', filename);
end
m.width = m.movie.Width;
m.height = m.movie.Height;
m.rate = m.movie.FrameRate;
m.totalDuration = m.movie.Duration;
m.nframes = floor(m.totalDuration * m.rate);
end
function paramSet(m, varargin)
opt.skip = 1;
disp(varargin)
opt = tb_optparse(opt, varargin);
m.skip = opt.skip;
end
% destructor
function delete(m)
fprintf('Movie destructor, delete movie object\n');
delete(m.movie);
end
function close(m)
%Movie.close Close the image source
%
% M.close() closes the connection to the movie.
delete(m.movie);
end
function sz = size(m)
sz = [m.width m.height];
end
function [out, time] = grab(m, varargin)
%Movie.grab Acquire next frame from movie
%
% IM = M.grab() acquires the next image from the movie
%
% IM = M.grab(OPTIONS) as above but allows the next frame to be
% specified.
%
% Options::
% 'skip',S Skip frames, and return current+S frame
% 'frame',F Return frame F within the movie
% 'time',T Return frame at time T within the movie
%
% Notes::
% - If no output argument given the image is displayed using IDISP.
opt.skip = m.skip;
opt.frame = [];
opt.time = [];
opt = tb_optparse(opt, varargin);
if ~isempty(opt.frame)
m.movie.CurrentTime = m.curFrame + opt.skip;
end
if ~isempty(opt.time)
m.movie.CurrentTime = opt.time;
end
if isempty(opt.frame) & isempty(opt.time)
m.movie.CurrentTime = m.movie.CurrentTime + opt.skip / m.rate;
end
% read next frame from the file
if m.movie.hasFrame
data = readFrame(m.movie);
else
out = [];
return;
end
if (numel(data) > 3*m.width*m.height)
warning('Movie: dimensions do not match data size. Got %d bytes for %d x %d', numel(data), m.width, m.height);
end
if any(size(data) == 0)
warning('Movie: could not decode frame %d', m.curFrame);
else
% the data ordering is wrong for matlab images, so permute it
%data = permute(reshape(data, 3, m.width, m.height),[3 2 1]);
im = data;
end
% apply options specified at construction time
im = m.convert(im);
if nargout == 0
idisp(im);
else
out = im;
end
end
function skiptotime(m, t)
m.movie.CurrentTime = t;
end
function skiptoframe(m, n)
m.movie.CurrentTime = n / m.rate;
end
function s = char(m)
%Movie.char Convert to string
%
% M.char() is a string representing the state of the movie object in
% human readable form.
s = m.fullfilename;
s = strvcat(s, sprintf('%d x %d @ %d fps; %d frames, %g sec', m.width, m.height, m.rate, m.nframes, m.totalDuration));
s = strvcat(s, sprintf('cur frame %d/%d (skip=%d)', m.curFrame, m.nframes, m.skip));
end
end
end
% test if the file is on the server in root folder
function v = fileonserver(filename)
% list of all the files associated with the toolbox, test here rather than pester my server
filelist = [
"LeftBag.mpg"
"traffic_sequence.mpg"
];
v = ~isempty( intersect(filelist, filename) );
end
% works like loadimage
function getfromserver(filename)
% get data from server
fprintf('downloading from server...');
movie = webread( fullfile('http://petercorke.com/files/images', filename) );
fprintf('\n');
mvtb = fileparts( which('iread') );
mkdir_p(mvtb, fullfile('images', filename) );
filename = fullfile(mvtb, 'images', filename);
% save it to local file
fprintf('saving locally to %s\n', filename);
fp = fopen(filename, 'w');
fwrite(fp, movie);
fclose(fp);
end
% works like mkdir -p
% starting at base, it creates all the folders needed for filename
function mkdir_p(base, filename)
[pth,fname] = pathlist(filename);
dir = base;
for i=1:length(pth)
dir = fullfile(dir, pth{i});
if exist( dir ) ~= 7
% folder doesn't exist, create it
mkdir(dir);
end
end
end
% convert a file path into a cell array of path components and the filename (with
% extension)
function [pth,fname] = pathlist(filename)
[p,f,e] = fileparts(filename);
fname = [f e];
if isempty(p)
pth = {};
else
pth = strsplit(p, filesep);
end
end