2010-10-12 73 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