📄 videoreader_imread.m
字号:
function [out,out2] = videoReader_imread(cmd, handle, varargin)
%[...] = videoReader_imread(cmd, handle, varargin)
% videoReader plugin for reading videos stored as individual images on
% disk. This plugin uses Matlab's IMREAD function to read the frames and
% is implemented as M-code instead of as a MEX function. Users should not
% call this function directly. Instead they should use the videoReader
% constructor and specify the 'imread' plugin.
%
% Type 'help videoReader' for usage documentation.
%
%Example:
% % make a few video frames -- a rotating gradient
% for i=1:10
% frame = repmat(mod([0:99]/99 + (i-1)/10, 1), [75,1]);
% imwrite(frame, sprintf('%04d.jpg', i));
% end
% % read in the video and show it as a movie
% vr = videoReader('%04d.jpg', 'plugin','imread');
% for i=1:10
% seek(vr,i);
% imshow(getframe(vr));
% title(sprintf('frame %d', i));
% pause(1);
% end
% vr = close(vr);
%
%Copyright (c) 2008 Gerald Dalley
%See "MIT.txt" in the installation directory for licensing details (especially
%when using this library on GNU/Linux).
% Registry of reader data. The videoIO architecture calls for plugins to
% keep all the data for a videoReader/videoWriter object stored in the
% plugin and accessible through objects identified by handles. This allows
% videoIO to work well with MEX functions. Here we store the data for each
% open object as well as some bookkeeping information.
persistent nextHandle handles dirnames filenames currFrames infos;
if isempty(nextHandle)
nextHandle = int32(now); % handle to use for the next open command
handles = int32(zeros(0,0)); % list of currently-active handles
dirnames = {}; % image files directory for each handle
filenames = {}; % for each handle, a list of image files
currFrames = []; % for each handle, the current frame num
infos = {}; % for each handle, what getinfo returns
end
% Get the handle for this instance
i = validateHandle(cmd,handle,handles);
if strcmp(cmd, 'close'),
%........................................................................
% CLOSE COMMAND
if ~isempty(varargin)
error('The "close" command does not take any optional arguments');
end
handles = handles([1:i-1, i+1:end]);
dirnames = {dirnames{[1:i-1, i+1:end]}};
filenames = {filenames{[1:i-1, i+1:end]}};
currFrames = currFrames([1:i-1, i+1:end]);
infos = {infos{[1:i-1, i+1:end]}};
elseif strcmp(cmd, 'getframe')
%........................................................................
% GETFRAME COMMAND
if ~isempty(varargin)
error('The "getframe" command does not take any optional arguments');
end
dirname = dirnames{i};
fname = filenames{i}{currFrames(i)+1};
out = imread(fullfile(dirname, fname));
elseif strcmp(cmd, 'getinfo')
%........................................................................
% GETINFO COMMAND
if ~isempty(varargin)
error('The "getinfo" command does not take any optional arguments');
end
out = fieldnames(infos{i});
out2 = struct2cell(infos{i});
elseif strcmp(cmd, 'next')
%........................................................................
% NEXT COMMAND
if ~isempty(varargin)
error('The "next" command does not take any optional arguments');
end
currFrames(i) = currFrames(i) + 1;
if currFrames(i) < infos{i}.numFrames
out = int32(1);
else
out = int32(0);
end
elseif strcmp(cmd, 'open')
%........................................................................
% OPEN COMMAND
% videoReader_imread('open',-1, filepattern)
% filepattern may use wildcards, e.g. 'mydir/*.png', or it may use
% an sprintf-like '%d' string, e.g. 'mydir/%04d.png'. The wildcard
% or sprintf substitution must be in the filename, not in a directory
% name (e.g. neither 'mydir/%04d/pic.png' nor 'mydir/*/pic.png' is
% allowed).
%
% For sprintf strings, we assume that the substituted number is the
% frame number. Currently, only non-negative numbers are supported.
% Only frames that are found when the open command is executed can
% be read.
%
% For wildcard strings, all files matching the pattern are examined
% and they are sorted in alphabetic order. The alphabetically-first
% frame is used as frame 0. Only frames that are found when the
% open command is executed can be read.
%
% Both the percent character (%) and wildcards (?,*) cannot appear
% in the same file pattern at the present time (e.g.
% 'mydir/*/%04d.png' is not allowed). The percent character may
% appear at most once (e.g. 'mydir/%04d_%04d.png' is not allowed).
i = length(handles)+1;
handles(i) = nextHandle;
out = nextHandle;
nextHandle = int32(double(nextHandle)+1);
if length(varargin)>1
error('No optional arguments are supported for the "open" command are supported for the imread plugin');
end
filepattern = varargin{1};
[dirnames{i}, filenames{i}] = getFileList(filepattern);
img = imread(fullfile(dirnames{i}, filenames{i}{end}));
currFrames(i) = -1;
infos{i} = struct(...
'url', varargin{1}, ...
'fps', -1, ...
'height', size(img,1), ...
'width', size(img,2), ...
'numFrames', length(filenames{i}), ...
'fourcc', '',...
'nHiddenFinalFrames', 0);
elseif strcmp(cmd, 'seek')
%........................................................................
% SEEK COMMAND
if length(varargin) ~= 1
error('The "seek" command takes exactly one argument');
end
currFrames(i) = varargin{1};
if currFrames(i) < infos{i}.numFrames && currFrames(i) >= 0
out = int32(1);
else
out = int32(0);
end
elseif strcmp(cmd, 'step')
%........................................................................
% STEP COMMAND
if length(varargin) ~= 1
error('The "step" command takes exactly one argument');
end
currFrames(i) = currFrames(i) + varargin{1};
if currFrames(i) < infos{i}.numFrames && currFrames(i) >= 0
out = int32(1);
else
out = int32(0);
end
else
error(['Unrecognized command: "' cmd '"']);
end
%--------------------------------------------
function i = validateHandle(cmd,handle,handles)
if strcmp(cmd, 'open')
if (handle ~= int32(-1))
% This is a slightly pedantic check to detect if someone has been
% messing with the constructor or is trying to call us directly.
error('Unexpected handle for the "open" command');
end
i = int32(-1);
else
[tst,i] = ismember(handle,handles);
if ~tst
error('Invalid load plugin handle');
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -