views:

65

answers:

1

Hey guys, I got this error message when I tried to trigger the function below. Can anybody help me out? Thanks!

>> 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

The source code:

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);
+1  A: 

On every iteration of the outer loop, you appear to be growing imgYuv by one in the 4th dimension, starting from empty. But your inner loop always loops from 1 to nFrames-1. Therefore, it would seem to me like you're trying to access beyond the extent of imgYuv.

On an unrelated note, growing an array like this is typically very slow. You're much better off initialising imgYuv before you start, i.e. imgYuv = zeros([height,width,3,nFrames]).

Oli Charlesworth
Yup, you are right. After I initialise the matrix, the problem never comes back again. Thank you!
appi
@Yoursclark: You seem to be recalculating the histogram on every single iteration of the outer loop, and overwriting the previous results, which is a bit of a waste. Why not just move the histogram loop to the end of the function.
Oli Charlesworth