Matlab中的aviread函数

来源:互联网 发布:c语言四则运算程序a,b 编辑:程序博客网 时间:2024/04/29 04:10

       在Matlab视频处理中,常用的一个函数就是aviread了。这个函数能读取avi格式的视频(这里称之为图像序列),可以认为是视频处理的第一步。本文从matlab 2011b的aviread函数介绍着手,写一篇aviread函数的介绍,并附上matlab关于aviread函数使用的两段源代码,以供参考。

     介绍aviread函数之前,先介绍几个图像处理里面的概念:

    1. AVI格式:AVI(Audio Video Interleave):比较早的AVI是Microsoft开发的。其含义是Audio Video Interactive,就是把视频和音频编码混合在一起储存(本文使用的avi格式的视频不存在音频)。AVI也是最长寿的格式,已存在10余年了,虽然发布过改版(V2.0于1996年发布),但已显老态。AVI格式上限制比较多,只能有一个视频轨道和一个音频轨道(现在有非标准插件可加入最多两个音频轨道),还可以有一些附加轨道,如文字等。AVI格式不提供任何控制功能。

    2.Truecolor Image:真彩色格式的图像。假设图像由M行N列的像素构成,每个像素点上的值由R,G,B三个颜色通道构成,每个颜色通道的取值为0~255的整数。因此,真彩色格式的图像的存储空间为M * N *3 的三维矩阵,这个三维矩阵中每个元素的取值为0~255的整数。

    3.Indexed Image:索引图像。图像实际上只有限种类的颜色,种类数量为m。假设图像仍然由M行N列的像素构成。实际上这个图像就可以看做由m种色彩,总数M*N的小瓷砖拼成的。为了描述这幅图像,我们在M*N个点上,定每个点的值为索引号,索引号的范围是0~m,表示每个像素点只能取m种颜色的一种。另外,为了使索引下标有颜色上的意义,我们同时构造出一张颜色的索引表,这个表的存储空间为m*3,在每个索引号(每行)对应不同的R,G,B颜色空间。这样我们就能跟据每个点的值查找出该点的颜色,从而描述出一幅图像了。 因此一幅索引图的存储空间为M*N+m*3。

     说到这里了,我们看看Matlab中是如何定义aviread函数的使用吧。

aviread

1.功能:Read Audio/Video Interleaved (AVI) file

Note:aviread will be removed in a future  release. UseVideoReaderinstead.

注: aviread的存储空间在将来会被释放,每次运行的时候,command window总是会出现:

Warning: AVIREAD will be removed in a future release. Use MMREADER instead.

2.语法 (Syntax)

mov= aviread(filename)

 例如:avi = aviread('samplevideo.avi');
mov = aviread(filename,index)

 这里的index代表图像的帧数的索引号,就是第i帧。或者第i帧到第j帧。

 例如:I=aviread('samplevideo.avi',1); % I 表示samplevideo图像序列的第一帧图像。

3.描述  (Description)

   mov= aviread(filename) reads the AVI moviefilename into the MATLAB movie structuremov. Iffilename does not include an extension, then .avi is used. Use themoviefunctionto view the moviemov. On UNIX platforms,filename must be anuncompressed AVI file.

   mov has two fields, cdata and colormap. The content of these fields varies depending on the type of image.

  aviread supports 8-bit frames, for indexed and grayscale images, 16-bit grayscale images, or 24-bit truecolor images. Note, however, that movie only accepts 8-bit image frames; it does not accept 16-bit grayscale image frames.

   mov= aviread(filename,index) reads only the frames specified by index. index can be a single index or an array of indices into the video stream. In AVI files, the first frame has the index value 1, the second frame has the index value 2,and so on.


整段程序示例一:

avi格式视频的读取和显示(Matlab代码)

clear data                                                                              

disp('input video');                             % 显示 "input video"

avi = aviread('samplevideo.avi');        % avi: MATLAB movie structure

video = {avi.cdata};                            % cdata: M x N x 3 的矩阵用来表示真彩色的图像

for a = 1:length(video)                      % length(video)表示这段视频的帧数

imshow(video{a})                              %按帧数顺序显示图像

drawnow;                                            

end             

-----------------------------------------------------------------------------------


整段程序示例二:

avi格式视频的读取和倒放(Matlab代码)

clear data                                                                              

disp('input video');                            % 显示 "input video"

avi = aviread('samplevideo.avi');       % avi: MATLAB movie structure

video = {avi.cdata};                            % cdata: M x N x 3 的矩阵用来表示真彩色的图像

for a = length(video) :-1:1                % length(video)表示这段视频的帧数

imshow(video{a})                             %按帧数倒序显示图像

drawnow;                                            

end                         

原创粉丝点击