2010-10-12 65 views
-1

嘿,伙计们,当我试图触发下面的函数时,我得到了这个错误消息。有人可以帮我吗?谢谢!MATLAB编码问题

>> changeYuv('tilt.yuv',352,288,1:40,40); 
??? Index exceeds matrix dimensions. 
    Error in ==> changeYuv at 32 
      j=histogram(imgYuv(:,:,1,k+1)); 

>> [x,y,z,a]=size(imgYuv) 
x = 
    288 
y = 
    352 
z = 
    3 
a = 
    40 

的源代码:

function [imgYuv, S]= changeYuv(fileName, width, height, idxFrame, nFrames) 
% load RGB movie [0, 255] from YUV 4:2:0 file 

fileId = fopen(fileName, 'r'); 

subSampleMat = [1, 1; 1, 1]; 
nrFrame = length(idxFrame); 

for f = 1 : 1 : nrFrame 
    % search fileId position 
    sizeFrame = 1.5 * width * height; 
    fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof'); 

    % read Y component 
    buf = fread(fileId, width * height, 'uchar'); 
    imgYuv(:, :, 1,f) = reshape(buf, width, height).'; 

    % read U component 
    buf = fread(fileId, width/2 * height/2, 'uchar'); 
    imgYuv(:, :, 2,f) = kron(reshape(buf, width/2, height/2).', subSampleMat); % reshape and upsample 

    % read V component 
    buf = fread(fileId, width/2 * height/2, 'uchar'); 
    imgYuv(:, :, 3,f) = kron(reshape(buf, width/2, height/2).', subSampleMat); % reshape and upsample 

    %histogram difference of Y component 
    for k=1:(nFrames-1) 
     h=histogram(imgYuv(:,:,1,k)); 
     j=histogram(imgYuv(:,:,1,k+1)); 
     X=abs(h-j)/256; 
     S(k)=sum(X); 
    end 
end 
fclose(fileId); 
+0

请在错误行上方的某处运行'[x,y,z,a] = size(imgYuv)'并告诉我们'a'的值。 – 2010-10-12 02:42:31

+0

嗨马克。我已经添加了imgYuv的大小。它有帮助吗? – view 2010-10-12 02:46:31

回答

1

在外部循环的每次迭代,你似乎是个在第四维增长imgYuv,从空的开始。但是你的内部循环总是从1循环到nFrames-1。因此,在我看来,您试图访问超出imgYuv的范围。

在一个不相关的说明中,像这样增长一个数组通常非常缓慢。你在开始之前初始化imgYuv要好很多,例如imgYuv = zeros([height,width,3,nFrames])

+0

是的,你说得对。在我初始化矩阵后,问题再也没有回来。谢谢! – view 2010-10-13 02:43:02

+0

@Yoursclark:你似乎在外循环的每一次迭代中重新计算直方图,并覆盖以前的结果,这有点浪费。为什么不直接将直方图循环移动到函数的末尾? – 2010-10-13 09:52:22