请帮助使此代码正常工作 [英] Help get this code working please

查看:96
本文介绍了请帮助使此代码正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我已经在MATLAB上获得了这个项目,我试图在其中读取YUV文件.

以下是我的工作目录中的m个文件

函数[width,height,fps,startpos,NumberFrames] = readheader(fid)

%***************************************************** *******
%此函数将读取bath yuv文件的标题
%
%此文件 由readyseq使用,不应单独使用.
%
%N.年轻
% 5  6月 2001 
%***************************************************** *******

FileFormat = [];
%第一行将包含以下内容之一

%1. YUV  4 : 2 : 0 
%2. RVS RAW YUV  4 : 2 : 0 

%读取格式描述符
 for  for i =  1 : 17 
  FileFormat = [FileFormat fscanf(fid,' %c' 1 )];

  %检查以查看是否 yuv  420 (即上面的 1 的数字)
  如果 i ==  9 
    如果 FileFormat == '  YUV 4:2:0' 
      disp(' 文件格式为:YUV 4:2:0');
      FileFormat =  0 ;
       break ;
    结尾
  结尾

  %检查以查看是否是否 rvs raw yuv  420 (即上面的 2 )
  如果 i ==  17 
    如果 FileFormat == '  RVS RAW YUV 4:2: 0'
      disp(' 文件格式为:RVS RAW YUV 4:2:0');
      FileFormat =  1 ;
       break ;
    结尾
  结尾

  如果我们在这里得到,而我  17 ,我们有不受支持的FileFormat
  如果 i ==  17 
    disp(' 未知文件格式')
    FileFormat =  2 ;
  结尾
结尾

如果 FileFormat == < 0 
  [width,height,fps,startpos,NumberFrames] = readyuvheader(fid);
结束



函数[width,height,y] = readyseq(文件名,开始,停止)
%***************************************************** *******
%加载浴液yuv文件的功能
%
%语法:[width,height,ysequence] = readyseq(文件名,开始帧,结束帧)
%
%注意:这些帧从 0 编号.
%
埃文斯& A.N.杨
% 5  6月 2001 
%***************************************************** *******

%注意,我必须在这里使用一些C函数.我知道有些人不喜欢
将Matlab与c混合在一起,I '  m认为这是不可避免的.

%首先我们需要打开文件
[fid,msg] = fopen(文件名,' r ' );

%如果msg大于0,即其中包含某些内容,则我们有一个错误
如果length(msg)> 0

  disp('打开文件时出错.' );

别的
  %读取标题信息
  [width,height,fps,startpos,NumberFrames] = readheader(fid);

  如果开始<数字框和停止<数字帧

     现在我们可以阅读给定的帧了.
      i = 1;
      对于帧=开始:停止
          现在读取所需的帧
          y(i,:,:)= readyframe(fid,width,height,startpos,frame);
          i = i + 1;
      结尾
  结尾
结尾
fclose(fid);  



函数[width,height,fps,startpos,NumberFrames] = readyuvheader(fid)

%***************************************************** *******
%此功能将读取浴缸的标头
%
%此文件 由readheader使用,不应
%单独使用.
%
%N.年轻
% 5  6月 2001 
%***************************************************** *******

%我们应该使用 1  字符 for 现在
fscanf(fid,' %c' 1 );

%现在读取序列的宽度
width = ' ';
 while  ne(width, 0 )
  width = [width fscanf(fid,' %c' 1 )];
结尾

宽度= str2num(width);

%现在读取序列的高度
height = ' ';
 while  ne(height, 0 )
  height = [height fscanf(fid,' %c' 1 )];
结尾

height = str2num(height);

现在读取fps
fps = ' ';
 while  ne(fps, 0 )
  fps = [fps fscanf(fid,' %c' 1 )];
结尾

fps = str2num(fps);

最后,在序列中 中计算帧数
startpos = ftell(fid); %获取当前位置(应该是数据的开始)

fseek(fid, 0 '  eof'); %移至文件末尾
endpos = ftell(fid);

现在我们可以计算出帧数
FileLength = endpos-startpos;
FrameSize =宽度*高度*  1 . 5 ; %HACK
NumberFrames = FileLength/FrameSize;

%将文件重置为数据的开头
fseek(fid,startpos,'  bof'); 




函数[y,u,v] = readframe(fid,width,height)

从流中 读取所需的帧

元素=宽度*高度;

%下面的重塑会将我们在 中读取的数据转换为 2d数据并对其进行旋转.

%读y帧
[y,count] = fread(fid,elements,'  uchar');

%if count   0 ,则我们已到达文件末尾
如果 count == < 0 
    y = [];
    u = [];
    v = [];
其他
    y = reshape(y,[width,height])' ;

    %读u框架,记住这是y的大小的1/4
    u = fread(fid,elements/4,' uchar ' );
    u = reshape(u,[width/2,height/2])';

    %读了v框,记住 是y大小的1/4
    v = fread(fid,elements/4,'  uchar');
    v = reshape(v,[width/2,height/2])' ;
结束 




函数s = yuv2pgm(文件名,开始帧,停止帧)

%***************************************************** *******
%funtion可以按yuv顺序读取 到内存并
同时将其转换为一系列pgm图像
%
%语法:Y_Sequence = yuv2pgm(文件名,开始帧,停止帧);
%
%这将从给定序列 中读取前n帧,
%,还将pgm副本保存到frame0.pgm,frame1.pgm等,
%
%注意:帧从 0 编号.
%
埃文斯& A.N.杨
% 5  6月 2001 
%***************************************************** *******

%读取序列的y帧
[width,height,y] = readyseq(filename,startframe,stopframe);

a = '  frame';
c = ' .pgm';

%注意,您的matlab副本上可能没有'  saveimg'文件,
%,您必须获得 >哑光的副本'  pgmwrite'.

 for  for i =  1 :stopframe-startframe + 1
   b = [int2str(i + startframe-1)];
   saveimg(squeeze(y(i,:,:)),strcat(a,b,c));
%pgmwrite(squeeze(y(i,:,:)),strcat(a,b,c));
结尾
s = y; 



现在,一个m文件不是一个功能,只是为了测试代码是否有效.这被保存为demo.m

%Sample yuv文件读取

%读取y帧 0 到序列test.yuv的 2 .
y = yuv2pgm('  test.yuv' 0  2 );

%the上面还将保存 3  pgm文件:
%
%frame0.pgm
%frame1.pgm
%frame2.pgm


frame0 = squeeze(y( 1 ,:,:));
frame1 = squeeze(y( 2 ,:,:));
frame2 = squeeze(y( 3 ,:,:));

%显示框架
数字;
图片(帧0);
colormap(gray( 256 ))

数字;
image(frame1);
colormap(gray( 256 ))

数字;
image(frame2);
colormap(gray( 256 ))




毕竟,我现在在命令窗口演示中键入
输出如下

??? Error using ==> mrdivide
Matrix dimensions must agree.
Error in ==> readyuvheader at 49
NumberFrames = FileLength / FrameSize;
Error in ==> readheader at 48
  [width,height,fps,startpos,NumberFrames] = readyuvheader(fid);
Error in ==> readyseq at 26
  [width,height,fps,startpos,NumberFrames] = readheader(fid);
Error in ==> yuv2pgm at 19
[width,height,y]=readyseq(filename,startframe,stopframe);
Error in ==> demo2 at 4
y = yuv2pgm(''test.yuv'',0,2);



因此,现在我通过将宽度和高度替换为CIF或Qcif格式视频的尺寸,将其从width * height * 1.5更改为FrameSize来进行描述.

现在我得到了这个命令

>> demo
File format is : YUV 4:2:0
??? Error using ==> fseek
Invalid offset.

Error in ==> readyframe at 16
fseek(fid,startpos + (frame * yuvElements),''bof'');



请帮助

非常感谢Advance

解决方案

严重的问题:为什么将此内容发布在CodeProject而不是MATLAB论坛上?


我'已经更改了代码,并设法使其正常工作.稍后,我将在这本书上写一篇详尽的文章.

Hi everyone
I''ve got this project on MATLAB where i''m trying to read in a YUV file.

Below are the m files within my work directory

function [width,height,fps,startpos,NumberFrames]=readheader(fid)

%********************************************************
% This function will read the header of a bath yuv file
%
% This file is used by readyseq and should not be used on its own.
%
% N.Young
% 5 June 2001
%********************************************************

FileFormat = [];
%The first line will contain one of the following

%1.   YUV 4:2:0
%2.   RVS RAW YUV 4:2:0

%read the format descriptor
for i=1:17
  FileFormat = [FileFormat fscanf(fid,'%c',1)];

  %Check to see if it is yuv 420 (i.e. number 1 above)
  if i==9
    if FileFormat == 'YUV 4:2:0'
      disp('File format is : YUV 4:2:0');
      FileFormat=0;
      break;
    end
  end

  %Check to see if it is rvs raw yuv 420 (i.e. number 2 above)
  if i==17
    if FileFormat == 'RVS RAW YUV 4:2:0'
      disp('File format is : RVS RAW YUV 4:2:0');
      FileFormat=1;
      break;
    end
  end

  %if we get here and i is 17, we have an unsupported FileFormat
  if i==17
    disp('Unknown file format')
    FileFormat = 2;
  end
end

if FileFormat==0
  [width,height,fps,startpos,NumberFrames] = readyuvheader(fid);
end



function [width,height,y]=readyseq(filename,start,stop)
%********************************************************
% Function to load a bath yuv file
%
% Syntax : [width,height,ysequence] = readyseq(filename,start frame,end frame)
%
% NOTE: the frames are numbered from 0.
%
% A.N.Evans & N.Young
% 5 June 2001
%********************************************************

%Note that I have to use some C functions here.  I know some people dont like
%mixing matlab with c, I'm affraid its un avoidable.

%First we need to open the file
[fid,msg] = fopen(filename,'r');

%if msg is longer than 0, i.e. it has something in it, we have an error
if length(msg) > 0

  disp('Error opening the file.');

else
  %read the header info
  [width,height,fps,startpos,NumberFrames] = readheader(fid);

  if start < NumberFrames & stop < NumberFrames

     %Now we can read the given frames.
      i=1;
      for frame = start:stop
          %now read the required frame
          y(i,:,:)=readyframe(fid,width,height,startpos,frame);
          i=i+1;
      end
  end
end
fclose(fid);



function [width,height,fps,startpos,NumberFrames]=readyuvheader(fid)

%********************************************************
%This function will read the header of a bath yuv
%
% This file is used by readheader and should not be
% used on its own.
%
% N.Young
% 5 June 2001
%********************************************************

%we should have 1 null character for yuv now
fscanf(fid,'%c',1);

%Now read the width of the sequence
width='';
while ne(width,0)
  width = [width fscanf(fid,'%c',1)];
end

width = str2num(width);

%Now read the height of the sequence
height='';
while ne(height,0)
  height = [height fscanf(fid,'%c',1)];
end

height = str2num(height);

%Now read the fps
fps='';
while ne(fps,0)
  fps = [fps fscanf(fid,'%c',1)];
end

fps = str2num(fps);

%Finally, caluclate the number of frmes in the sequence
startpos = ftell(fid);  %get the current position (should be the start of the data)

fseek(fid,0,'eof'); %move to the end of the file
endpos = ftell(fid);

% now we can calculate the number of frames
FileLength = endpos-startpos;
FrameSize = width * height * 1.5; %quick HACK
NumberFrames = FileLength / FrameSize;

%Reset the file to the start of the data
fseek(fid,startpos,'bof');




function [y,u,v]=readframe(fid,width,height)

%read the required frame from the stream

elements = width*height;

%The reshape below turns the data we read in into 2d data and rotates it.

%read the y frame
[y,count] = fread(fid,elements,'uchar');

%if count is 0, then we have reached the end of the file
if count==0
    y=[];
    u=[];
    v=[];
else
    y=reshape(y,[width,height])';

    %read the u frame, remember this is 1/4 of the size of the y
    u = fread(fid,elements/4,'uchar');
    u=reshape(u,[width/2,height/2])';

    %read the v frame, remember this is 1/4 of the size of the y
    v = fread(fid,elements/4,'uchar');
    v=reshape(v,[width/2,height/2])';
end




function s = yuv2pgm(filename,startframe,stopframe)

%********************************************************
%Funtion to read in a yuv sequence, to memory and
%convert it to a series of pgm images at the same time
%
%Syntax : Y_Sequence = yuv2pgm(filename,startframe,stopframe);
%
%This will read the first n frames from the given sequence,
%and also save pgm copies to, frame0.pgm, frame1.pgm and so on,
%
%NOTE: The frames are numbered from 0.
%
% A.N.Evans & N.Young
% 5 June 2001
%********************************************************

%Read the y frames of the sequence
[width,height,y]=readyseq(filename,startframe,stopframe);

a = 'frame';
c = '.pgm';

%Note, You may not have the 'saveimg' file on your copy of matlab,
% in which case you will have to get a copy of matts 'pgmwrite'.

for i=1:stopframe-startframe+1
   b=[int2str(i+startframe-1)];
   saveimg(squeeze(y(i,:,:)),strcat(a,b,c));
%   pgmwrite(squeeze(y(i,:,:)),strcat(a,b,c));
end
s=y;



NOW A m file thats not a function, just to test the code works. This is saved as demo.m

%Sample yuv file reading

%Read the y frames 0 to 2 of the sequence test.yuv
y = yuv2pgm('test.yuv',0,2);

%the above will also have save 3 pgm files:
%
% frame0.pgm
% frame1.pgm
% frame2.pgm


frame0 = squeeze(y(1,:,:));
frame1 = squeeze(y(2,:,:));
frame2 = squeeze(y(3,:,:));

%show the frames
figure;
image(frame0);
colormap(gray(256))

figure;
image(frame1);
colormap(gray(256))

figure;
image(frame2);
colormap(gray(256))




So after all that i now type in the command window demo
The output is as follows

??? Error using ==> mrdivide
Matrix dimensions must agree.
Error in ==> readyuvheader at 49
NumberFrames = FileLength / FrameSize;
Error in ==> readheader at 48
  [width,height,fps,startpos,NumberFrames] = readyuvheader(fid);
Error in ==> readyseq at 26
  [width,height,fps,startpos,NumberFrames] = readheader(fid);
Error in ==> yuv2pgm at 19
[width,height,y]=readyseq(filename,startframe,stopframe);
Error in ==> demo2 at 4
y = yuv2pgm(''test.yuv'',0,2);



So now I change the description of FrameSize by changing it from width*height*1.5 by replacing width and height with the dimensions of a cif or Qcif format video.

Now i get this command

>> demo
File format is : YUV 4:2:0
??? Error using ==> fseek
Invalid offset.

Error in ==> readyframe at 16
fseek(fid,startpos + (frame * yuvElements),''bof'');



Please Help

Many Thanks in Advance

解决方案

Serious question: why do you post this on CodeProject rather than the MATLAB forum?


I''ve changed the code and managed to get this working. I will write a thorough article on this later


这篇关于请帮助使此代码正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆