⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 videoread.m

📁 Video IO toolbox for matlab. 用directshow做的
💻 M
字号:
function mov=videoread(fname,varargin)
%VIDEOREAD Read an video file in a mannter compatible with Matlab's
%          built-in AVIREAD function, but using a videoReader object to do
%          the work instead.  
%
%   Performance note: If you are reading a long video that will not fit
%   in memory, you could read in a chunk of frames at a time.
%   Unfortunately, this will result in O(N^2) performance cost or 
%   possible seek misalignments.  For most users, it is highly preferable
%   to use a videoReader object instead of VIDEOREAD.
%
%
%   MOV=VIDEOREAD(FNAME)
%     Read the video file FNAME into the Matlab movie structure MOV.  MOV
%     has two fields, "cdata" and "colormap".  Since all videoReader
%     plugins currently output 24-bit images in all cases, colormap will
%     always be blank.    
%
%   MOV=VIDEOREAD(FNAME,VRARGS)
%     The same as MOV=VIDEOREAD(FNAME), but VRARGS are passed to the 
%     videoReader constructor.  VRARGS should be a cell array of arguments.
%
%   MOV=VIDEOREAD(FNAME,INDEX)
%     Reads only the frame(s) specified by INDEX.  INDEX can be a single
%     index or an array of indices into the video stream, where the first
%     frame is  number one.  IMPORTANT NOTES: If you use videoReader
%     directly, it assumes  frames start at 0, not 1.  We use 1-indexing
%     here in VIDEOREAD to maximize  compatibility with MathWork's AVIREAD.  
%     Also, if you find yourself making several calls to VIDEOREAD to 
%     extract different portions of the file and/or if the number of 
%     frames is large, consider using videoReader directly instead of this 
%     wrapper function.
%
%   MOV=VIDEOREAD(FNAME,VRARGS,INDEX)
%     The same as MOV=VIDEOREAD(FNAME,INDEX), but VRARGS are passed to the 
%     videoReader constructor.  VRARGS should be a cell array of arguments.  
%
% Examples:
%   mov = videoread(fullfile(videoIODir, 'tests/numbers.uncompressed.avi'));
%   mov = videoread(fullfile(videoIODir, 'tests/numbers.uncompressed.avi'), 1:10);
%   mov = videoread(fullfile(videoIODir, 'tests/numbers.wmv3.avi'), {'DirectShow'});
%
% SEE ALSO:
%   videoReader
%   buildVideoIO 
%
%Copyright (c) 2006 Gerald Dalley
%See "MIT.txt" in the installation directory for licensing details (especially 
%when using this library on GNU/Linux). 

if nargin==1,
  vrargs = {};
elseif nargin==2,
  if (iscell(varargin{1})),
    vrargs = varargin{1};
  else
    vrargs = {};
    index  = varargin{1};
  end
elseif nargin==3,
  vrargs = varargin{1};
  index  = varargin{2};
end

vr = videoReader(fname, vrargs{:});
if ~(exist('index','var')),
  info = getinfo(vr);
  index = (0:info.numFrames-1); % 0-indexed
else
  index = index - 1; % to 0-indexed
end

mov = struct;
currFrame = -1;
if (size(index)>0)
  % index supplied or deduced from file
  for i=1:length(index)
    f = index(i);
    if (f == currFrame+1)
      if ~next(vr), error('Could not advance to frame %d\n', f+1); end
    else
      if ~seek(vr, f), error('Could not seek to frame %d\n', f+1); end
    end
    currFrame = f;
    mov(i).cdata = getframe(vr);
    mov(i).colormap = [];
  end
else
  % no index was supplied and we could not deduce the number of frames 
  % in the video.
  i=1;
  while next(vr)
    mov(i).cdata = getframe(vr);
    mov(i).colormap = [];
    i = i+1;
  end
end

vr = close(vr);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -