📄 videoreader_load.m
字号:
function [out,out2] = videoReader_load(cmd, handle, varargin)
%[...] = videoReader_load(cmd, handle, varargin)
% videoReader plugin for reading videos stored as individual images in
% MAT files (one image per MAT file). This plugin uses Matlab's LOAD
% 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
% 'load' 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]);
% save(sprintf('%04d.mat', i), 'frame');
% end
% % read in the video and show it as a movie
% vr = videoReader('%04d.mat', 'plugin','load', 'varname','frame');
% 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).
persistent nextHandle handles varnames dirnames filenames currFrames infos;
if isempty(nextHandle)
nextHandle = int32(now);
handles = int32(zeros(0,0));
varnames = {};
dirnames = {};
filenames = {};
currFrames = [];
infos = {};
end
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 = loadFrame(varnames{i}, 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
i = length(handles)+1;
handles(i) = nextHandle;
out = nextHandle;
nextHandle = int32(double(nextHandle)+1);
if (mod(length(varargin)-1,2) ~= 0)
error('Optional arguments must come in name-value pairs');
end
varnames{i} = '';
for j=2:2:length(varargin)
if (strcmp(varargin{j}, 'varname'))
varnames{i} = varargin{j+1};
else
error(['Unrecognized optional argument name: ', varargin{j}]);
end
end
filepattern = varargin{1};
[dirnames{i}, filenames{i}] = getFileList(filepattern);
img = loadFrame(varnames{i}, 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,...
'varname', varnames{i});
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
%--------------------------------------------
function out = loadFrame(varname, dirname, fname)
if isempty(varname)
tmp = load(fullfile(dirname, fname));
% choose the biggest 2D or 3D matrix
fnames = fieldnames(tmp);
scores = zeros(numel(fnames),1);
vals = struct2cell(tmp);
for j=1:length(scores)
scores(j) = getVariableScore(vals{j});
end
[dummy,fieldIdx] = max(scores);
out = getfield(tmp, fnames{fieldIdx}); %#ok<GFLD> -- better backward compatibility
else
out = getfield(load(fullfile(dirname, fname), varname), ...
varname); %#ok<GFLD> -- better backward compatibility
end
%--------------------------------------------
function score = getVariableScore(x)
%score = getVariableScore(x)
% Assigns a heuristic score to the variable x. Variables that look
% like images (are 2D or 3D numeric matrices) receive very high
% scores. Bigger variables get higher scores that smaller ones. All
% variables receive a non-zero score.
% soft-decision bias (makes desired features soft constraints)
soft = 1e-10;
sz = size(x);
if length(sz) < 2
nPix = numel(x);
else
nPix = sz(1)*sz(2);
end
score = ...
((((ndims(x)==3)+soft) * ((size(x,3)==3)+soft)) +... % rgb or
((ndims(x)==2)+soft)) * ... % grayscale
(nPix+soft) * ... % and big
(isnumeric(x)+soft); % and numeric
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -