声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1704|回复: 2

[共享资源] 在Matlab环境下显示运动图像,可直接进行视频文件的读取

[复制链接]
发表于 2006-10-24 16:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
Avi2Movie 1.00


                                 18.12.2000

                           (c) by R.Rawer '99-'01

===============================================================================
(1) Contents
===============================================================================

(1) Contents
(2) About Avi2Movie
(3) Syntax of Avi2Movie
(4) Possible Code Modifications
(5) History of Changes
(6) Contact the Autor

===============================================================================
(2) About Avi2Movie
===============================================================================

Avi2Movie is a simple m-file to read the images of uncompressed AVIs into
MatLab and store them as a MatLab Movie-Dataset.

Avi2Movie is based on AviRead.

(*1*)         the section where the bytestream read from the AVI-file is transformed
        into a matrix
(*2*)         the section where you can add you own code to do caculations based on
        the picture data

Both sections are marked in the source code.

===============================================================================
(3) Syntax of Avi2Movie
===============================================================================

Syntax:

Result = Avi2Movie(filename,
                     number of frames to process,
                     first frame to display,
                     last frame to display)

Result : selected frames stored as one MatLab Movie-Dataset

Example: Avi2Movie('sample.avi',30,10,12)
            reads and processes the first 30 frames of the file
            'sample.avi', and saves frame 10, 11 and 12 as one
            MatLab Movie-Dataset.      

===============================================================================
(4) Possible Code Modifications
===============================================================================

AviRead justs reads the image data of AVI-files. In order to process this
image data you probably need to modify the the code in section (*2*).
If anothe arrangement of the pixelinformation is needed also modify
section (*1*) ind AviRead.m.

Use the folowing variables within your image processing routines:
  
   no_of_frames:   number of frames to be read
   time_per_frame: time to display each frame [ms] (reverse of frames
                     per 0.001 second)
   frames:           number of this frame
   columns:           number of pixels per line
   lines:           number of lines per image
   bytes:           number of bytes per pixel
   im:                   image data:
                        8bit/pixel:  uint8-Matrix[columns,lines]
                       16bit/pixel: uint16-Matrix[columns,lines]
                       24bit/pixel: uint8-Matrix[columns,lines,3]
                                    (3 Color Planes R,G,B)
                       32bit/pixel: uint32-Matrix[columns,lines]
   xx:                   raw image data:
                       8bit/pixel:  uint8-Matrix[columns*lines]
                                    (1-dimentional)
                       16bit/pixel: uint16-Matrix[columns*lines]
                                    (1-dimentional)
                       24bit/pixel: uint8-Matrix[3,columns*lines]
                                    (1-dimentional, 3 Color Planes R,G,B)
                       32bit/pixel: uint32-Matrix[columns*lines]                                          
                                    (1-dimentional)  
  
===============================================================================
(5) History of Changes
===============================================================================

V1.00 (22/12/2000):
-------------------

Generated Avi2Movie from AviRead V1.13

===============================================================================
(6) Contact the Author
===============================================================================

rrawer@gmx.de

http://www.rawer.de/rainer/software/
回复
分享到:

使用道具 举报

 楼主| 发表于 2006-10-24 16:06 | 显示全部楼层
  1. function [output] = Avi2Movie(filename,option,option2,option3)
  2. %--------------------------------------------------------------------
  3. % result = Avi2Movie (filename,
  4. %                     number of frames to process,
  5. %                     first frame to use,
  6. %                     last frame to use)
  7. %
  8. % Result = read Frames as MatLab MOVIE-format:
  9. %
  10. % Example: Avi2Mivie('sample.avi',30,10,12)
  11. %            reads and processes the first 30 frames of the file
  12. %            'sample.avi', and store frame 10, 11 and 12 as
  13. %            one MatLab Movie-Dataset.      
  14. %
  15. % NOTE: supports only uncompressed plain avi(RIFF) files
  16. %       (8, 16, 24, 32 Bit per Pixel)
  17. %
  18. % (c) by Rainer Rawer (using Matlab 5.3)
  19. % http://www.rawer.de/rainer/software/
  20. % rrawer@gmx.de
  21. % 22/12/2000
  22. %--------------------------------------------------------------------


  23. % default declarations:
  24. JUNK         = [74 85 78 75];                                % <JUNK>
  25. RIFF                         = [82 73 70 70];                         % <RIFF>
  26. AVI          = [65 86 73 32];                                % <AVI >
  27. MOVI         = [109 111 118 105];                % <movi>
  28. ValidFrameID = [48 48 100 98];                        % <00db>
  29. AVIH         = [97 118 105 104];                        % <avih>
  30. STRF         = [115 116 114 102];                % <strf>
  31. version      = '1.0';
  32. lines        = 240;  % default lines per frame
  33. columns      = 320;  % default colons per frame
  34. bytes        = 1;    % default bytes per pixel
  35. no_of_frames = 1;    % no of frames to read
  36. contador=0;
  37. %-------------------------------------------------------------------------


  38. %-------------------------------------------------------------------------
  39. % checking if files is existing and a valid RIFF/AVI file
  40. %-------------------------------------------------------------------------

  41.   if nargin == 0;
  42.      disp(['-------------------------------']);  
  43.      disp([' Avi2Movie V',version, '  by R.Rawer `99-`01'])
  44.      disp(['-------------------------------']);
  45.      disp([' usage: Avi2Movie(filename,'])
  46.      disp(['                  number of frames to process,'])
  47.      disp(['                  first frame to use,'])
  48.      disp(['                  last frame to use)'])
  49.      error(['### no parameters']);
  50.   end
  51.   if nargin < 4; option3=option2 ; end
  52.   if nargin < 3; option2 = 0, option3= 0 ; end
  53.   if nargin < 2; option = 0; option2 = 0, option3= 0 ; end
  54.   
  55.   
  56.   
  57.   fid = fopen(filename, 'r');
  58.   if fid < 3; error(['### Avi2Movie: ', filename, ' NOT found.']); end
  59.   
  60.   xx = uint8(fread(fid, 5000, 'uint8'));  
  61.   if max(xx(1:4)'==RIFF==0);                                                                                %check for: 'RIFF'
  62.      error(['### Avi2Movie: ', filename, ...
  63.            ' is not a valid RIFF file.']);
  64.   elseif max(xx(9:12)'==AVI==0);                                                                        %check for: 'AVI '
  65.           error(['### Avi2Movie: ', filename, ...
  66.                 ' is not a valid AVI file.']);
  67.   end        
  68.   fclose(fid);
  69.   
  70.   
  71. %-------------------------------------------------------------------------
  72. % Extracting AVI header information
  73. %-------------------------------------------------------------------------

  74. h=1;i=0;h2=0;e=1;
  75. while e==1;
  76.    h=h+1;
  77.    if min(xx(h:(h+3))'== AVIH);                                                                         %check for ID: 'avih'
  78.       h2=h;
  79.       e=0;
  80.    end                  
  81. end
  82. time_per_frame = double(xx(h2+8))+double(xx(h2+9))*256+double(xx(h2+10))*256*256+double(xx(h2+11))*256*256*256;
  83. no_of_frames   = double(xx(h2+24))+double(xx(h2+25))*256+double(xx(h2+26))*256*256+double(xx(h2+27))*256*256*256;
  84. columns        = double(xx(h2+40))+double(xx(h2+41))*256+double(xx(h2+42))*256*256+double(xx(h2+43))*256*256*256;
  85. lines          = double(xx(h2+44))+double(xx(h2+45))*256+double(xx(h2+46))*256*256+double(xx(h2+47))*256*256*256;

  86. i=0;h3=0;e=1;
  87. while e==1;
  88.    h=h+1;
  89.    if min(xx(h:(h+3))'==STRF);                                                                         %check for ID: 'strf'
  90.       h3=h;
  91.       e=0;
  92.    end                  
  93. end
  94. color_depth=double(xx(h3+22))+double(xx(h3+23))*256;
  95. switch color_depth
  96.    case 8
  97.       bytes=1;
  98.    case 16
  99.       bytes=2;
  100.    case 24
  101.       bytes=3;
  102.    otherwise bytes=4;
  103. end

  104. e=1;
  105. while e==1;
  106.    h=h+1;
  107.    if min(xx(h:(h+3))'==MOVI);                                                                         %check for ID: 'movi'
  108.       e=0;
  109.       h1=h;
  110.    end                
  111. end
  112. frame_length=double(xx(h1+8))+double(xx(h1+9))*256+double(xx(h1+10))*256*256+double(xx(h1+11))*256*256*256;
  113. frame_ID=xx(h1+4:h1+7);

  114. % re-open file to check foer actual framelength:
  115. fid = fopen(filename, 'r');
  116. if fid < 3; error(['### Avi2Movie: ', filename, ' NOT found.']); end
  117. % seek to place where 2nd frame should be:
  118. fseek(fid,h1+4+frame_length,-1);
  119. xx = uint8(fread(fid, 5000, 'uint8'));  
  120. h=0;i=0;h5=0;e=1;
  121. while e==1;
  122.    h=h+1;
  123.    if min(xx(h:(h+3))'==frame_ID');                                                         %check for ID: frame_ID
  124.       h5=h;
  125.       e=0;
  126.    end                  
  127. end
  128. frame_offset=h5;

  129. % check for compressed AVIs:
  130. if ((lines*columns)~=(frame_length/bytes));
  131.    error('### no compressed AVI supported !');
  132. end;   


  133. % check if fra,mes to read exeeds number of frames in file:
  134. if (option > no_of_frames)
  135.    option = no_of_frames;
  136.    disp(sprintf('Warning: No of frames to read adjusted to %d!',option));   
  137. end;

  138.    
  139. %display header information:
  140. frame_ID=double(frame_ID);
  141. disp('---------------------------------------------------------');
  142. disp(['Avi2Movie V',version,' by R.Rawer `99']);
  143. disp(sprintf('   filename                 : "%s"',filename));
  144. disp(sprintf('   number of frames to read : %d',option));
  145. disp(sprintf('   display frame            : #%d to #%d',option2,option3));
  146. disp('---------------------------------------------------------');
  147. disp(sprintf('   number of data blocks : %d',no_of_frames));
  148. disp(sprintf('   frames per second     : %5.2f',1000000*1/time_per_frame));
  149. disp(sprintf('   frame size            : %d x %d',columns,lines));
  150. disp(sprintf('   colour depth          : %d (%dbyte)',color_depth,bytes));
  151. disp(sprintf('   frame length          : %d (0x%x)',frame_length,frame_length));
  152. disp(sprintf('   frame ID              : %c%c%c%c',frame_ID(1),frame_ID(2),frame_ID(3),frame_ID(4)));
  153. disp(sprintf('   frame offset          : %d',frame_offset));
  154. disp('---------------------------------------------------------');



  155. %-------------------------------------------------------------------------
  156. % start reading single frames
  157. %-------------------------------------------------------------------------

  158. % re-open file for actual reading of data:
  159. fid = fopen(filename, 'r');
  160. if fid < 3; error(['### Avi2Movie: ', filename, ' NOT found.']); end
  161. fseek(fid,h1+3,-1);

  162. % read frames
  163. frames=0;
  164. if option>0; no_of_frames=option;end
  165. while (i<no_of_frames);
  166.    frames=frames+1;
  167.    i=i+1;
  168.    %disp(sprintf('processing frame %d of %d ',i,no_of_frames));
  169.    
  170.    frame_header = uint8(fread(fid, 8, 'uint8')');
  171.    f_length=double(frame_header(5))+double(frame_header(6))*256+double(frame_header(7))*256*256+double(frame_header(8))*256*256*256;
  172.    
  173.    % seek for next valid pixture dataset:
  174.    e=0;
  175.    while e==0;
  176.       if (frame_header(1:4)==JUNK );
  177.          %found a JUNK frame and skipping it...
  178.          %disp('reading JUNK frame...');
  179.          xx=uint8(fread(fid, f_length, 'uint8')');
  180.          frame_header = uint8(fread(fid, 8, 'uint8')');
  181.          f_length=double(frame_header(5))+double(frame_header(6))*256+double(frame_header(7))*256*256+double(frame_header(8))*256*256*256;
  182.       elseif f_length==0;
  183.          %found empty frame and skipping it...
  184.          %disp('reading empty frame...');
  185.          frame_header = uint8(fread(fid, 8, 'uint8')');
  186.          i=i+1;
  187.          f_length=double(frame_header(5))+double(frame_header(6))*256+double(frame_header(7))*256*256+double(frame_header(8))*256*256*256;
  188.       elseif (frame_header(1:4)== frame_ID')
  189.          %found valid frame....
  190.          %disp('found valid movi-frame...');
  191.          e=1;
  192.       else   
  193.          %found non-movi media frame
  194.          %disp('skipping non-movi frame...');
  195.          %disp('flength');f_length
  196.          xx=uint8(fread(fid, f_length, 'uint8')');
  197.          frame_header = uint8(fread(fid, 8, 'uint8')');
  198.          f_length=double(frame_header(5))+double(frame_header(6))*256+double(frame_header(7))*256*256+double(frame_header(8))*256*256*256;      
  199.       end   
  200.    end
  201.    
  202.    %==================================================================
  203.    % (*1*)                                                                                                                                                                =
  204.    %                                                                                                                                                                                 =   
  205.    % Reading Image Data (depening on number of bytes per pixel)      =
  206.    % and rearrange it to image-matrix                                                                    =
  207.    %==================================================================
  208.    switch bytes
  209.       case 1
  210.          % read 8bit per Pixel (greyscale) Data:
  211.          xx = uint8(fread(fid, frame_length/bytes, 'uint8'));   
  212.                    % reshape data as 2-dimentional image array:
  213.                 im = reshape(xx(1,:),columns,lines);
  214.       case 2
  215.          % read 16bit per Pixel Data:
  216.          xx = uint16(fread(fid, frame_length/bytes, 'uint16'));
  217.                    % reshape data as 2-dimentional image array:
  218.          im = reshape(xx(1,:),columns,lines);
  219.       case 3   
  220.          % read 24bit per Pixel (truecolor) Data:
  221.          xx = uint8(fread(fid, frame_length, 'uint8'));
  222.          xx = reshape(xx,3,frame_length/3)';
  223.          contador=contador+1;
  224.          % reshape data as 3-dimentional truecolor image array
  225.          % ([rows,lines,3], three color planes RGB):
  226.          im(:,:,3) = rot90(reshape(xx(:,1),columns,lines));
  227.          im(:,:,2) = rot90(reshape(xx(:,2),columns,lines));
  228.          im(:,:,1) = rot90(reshape(xx(:,3),columns,lines));
  229.          im2(:,:,contador)=im(:,:,1);
  230.          
  231.       otherwise  
  232.          % read 32bit per Pixel Data:
  233.          xx = uint32(fread(fid, frame_length/bytes, 'uint32'));
  234.                         % reshape data as 2-dimentional image array:
  235.          im = double(reshape(xx,columns,lines));
  236.       end           
  237.       

  238.   %=================================================================
  239.   % (*2*)                                                                                                                                                         =
  240.   %                                                                                                                                                                          =   
  241.   % processing data of each frame starts here                      =
  242.   % if you don't want to display any of the frames simply          =
  243.   % delete the folowing lines...  (cut to cut)                     =
  244.   %=================================================================
  245.   
  246.   %---cut---
  247.   % display image data if needed:
  248.   if option2>0;
  249.      if ((i>=option2)&(i<=option3));
  250.         figure('name',sprintf('Frame #%d',i));
  251.         switch bytes
  252.            case 1
  253.               imshow(im');colormap(gray);  
  254.            case 2
  255.               imshow(im');colormap(gray);
  256.            case 3
  257.               image(im);
  258.            otherwise
  259.               imshow(im);
  260.         end
  261.      end   
  262.   end
  263.   
  264.    
  265.   %---cut---
  266.   
  267.   
  268.    
  269.   
  270.   % My code comes here..
  271.   
  272.   
  273.   

  274.   %-----------------------------------------------------------------
  275.   % Use the following varibles:
  276.   %
  277.   % no_of_frames:   number of frames to be read
  278.   % time_per_frame: time to display each frame [ms] (reverse of Frames
  279.   %                                          per 0.001 second)
  280.   % frames:                          number of this frame
  281.   % columns:                  number of pixels per line
  282.   % lines:                          number of lines per image
  283.   % bytes:                          number of bytes per pixel
  284.   % im:                                  image data:
  285.   %                                            8bit/pixel:  uint8-Matrix[columns,lines]
  286.   %                                            16bit/pixel: uint16-Matrix[columns,lines]
  287.   %                                            24bit/pixel: uint8-Matrix[columns,lines,3]
  288.   %                                                                                (3 Color Planes R,G,B)
  289.   %                                            32bit/pixel: uint32-Matrix[columns,lines]
  290.   % xx:                                  raw image data:
  291.   %                                            8bit/pixel:  uint8-Matrix[columns*lines]
  292.   %                                                                                  (1-dimentional)
  293.   %                                            16bit/pixel: uint16-Matrix[columns*lines]
  294.   %                                                                                   (1-dimentional)
  295.   %                                            24bit/pixel: uint8-Matrix[3,columns*lines]
  296.   %                                                                                  (1-dimentional, 3 Color Planes R,G,B)
  297.   %                                            32bit/pixel: uint32-Matrix[columns*lines]                                          
  298.   %                                                                                  (1-dimentional)
  299.   %
  300.   % Note: if you prefer another byte-arangement in order to speed up
  301.   %                 your image processing refere to section (*1*) and modify
  302.   %            the reshape commands in order to speed up the rearanging
  303.   %                 process
  304.   %
  305.   %-----------------------------------------------------------------
  306.   
  307.   
  308.   %---------
  309.   % store image in MatLab Movie-Format
  310.   
  311.   mov(i)=im2frame(im);
  312.   
  313. end  % end reading single frames -----------------------------------





  314. % output read statistics:
  315. disp('---------------------------------------------------------');
  316. disp(sprintf('Read %d Blocks, %d valid Frames',i,frames));
  317. disp('---------------------------------------------------------');
  318. % plot results of maximum positions

  319. output=mov;
  320. movie(mov);

  321. disp (['script done !']);
  322. disp(' ');
复制代码
发表于 2006-11-12 13:44 | 显示全部楼层
程序太长了,得先慢慢调试
谢谢了
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-25 03:22 , Processed in 0.059243 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表