YUV video to Images. Find the end of the video?

Coderaemon

I am using this Function to convert YUV video to images. It reads the video, frame by frame and works fine.
I am looping through all the frames and have to stop at the end when all the frames have been read. Problem is that it doesn't tell me that end is reached and keeps on displaying frames in a circular manner i.e after last frame comes the first and so on. if I can somehow know the total number of frames I can break the loop.

function[intensity_array] = roi()
intensity_array = [];
for i=1:1000000000
    try
        image = loadFileYuv('D:\test_data\test_ardu_2sec_short.yuv',320,240,i);
        roi = image.cdata([37:52],[278:290],1);  % y,x row, column
        max_red_intensity = max(max(roi));
        intensity_array(end+1) = max_red_intensity;
    catch
        intensity_array(end+1) = 0;
        break
    end    
    disp(['iter:', num2str(i)]);
end
end

I see that once the last frame is reached there is some error(below) and then the cycle begins again. So Can I break there ?

Error using reshape
To RESHAPE the number of elements must not change.

Error in loadFileYuv (line 18)
    imgYuv(:, :, 1) = reshape(buf, width, height).'; % reshape

P.S: It's not a matlab in-built function but an open source one so maybe not many people know how it operates. But you can surely get an idea from its code.

Daniel

For any valid inputs loadFileYuv returns the correct output. So fix your code, not passing any invalid frame indices. Your loop should end at:

s=dir('D:\test_data\test_ardu_2sec_short.yuv')
num_of_frames=s.bytes/1.5/320/240

With a resolution of 320*240 you have 320*240 Y-Pixes. For U and V per definition the resolution reduced by a factor of 4 to 160*120. So you have 1.5*320*240 pixels with 1 byte each.

For to large frame indices, the function always returns the first frame. This is cause because the return value of fseek isn't checked.

To make the function more robust, replace line 15 with:

assert(0==fseek(fileId, (idxFrame(f) - 1) * sizeFrame, 'bof'),'fseek failed, probably end of file is reached');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related