I want to get a single frame from yuv formate video file

3 views (last 30 days)
clear;
vlc='balloons_c_1_1024x768-.yuv';
vld='balloons_d_1_1024x768.yuv';
vrc='balloons_c_5_1024x768.yuv';
vrd='balloons_d_5_1024x768.yuv';
function[y,u,v]=Test2(vid,width,height,nframe)
%r(:,nframe)=y(:,:,nframe)+1.140*v(:,:,nframe);
%g(:,nframe)=y(:,:,nframe)-0.395*u(:,:,nframe)-0.581*v(:,:,nframe);
%b(:,nframe)=y(:,:,nframe)+2.032*u(:,:,nframe);
fid=fopen(vid,'r');
stream=fread(fid,'*uchar');
frame_length=1.5*width*height;
y=uint8(zeros(height,width,nframe));
u=uint8(zeros(height/2,width/2,nframe));
v=uint8(zeros(height/2,width/2,nframe));
for iframe=1:nframe
frame=stream((iframe-1)*frame_length+1:iframe*frame_length);
yImage = reshape(frame(1:width*height), width, height)';
uImage = reshape(frame(width*height+1:1.25*width*height), width/2, height/2)';
vImage = reshape(frame(1.25*width*height+1:1.5*width*height), width/2, height/2)';
y(:,:,nframe)=yImage;
u(:,:,nframe)=uImage;
v(:,:,nframe)=vImage;
end
%....................................................
height=768;
width=1024;
nframe=500;
[Y,U,V]=Test2(vlc,width,height,nframe);
imshow(Y(:,:,125));

Answers (1)

Naga
Naga on 26 Nov 2024
Hi Sahar,
The code incorrectly assigns the Y, U, and V components to the same index nframe, causing all frames to overwrite each other. This results in only the last frame being stored, losing all previous frames.
Assign the components to iframe instead of nframe to ensure each frame is stored uniquely:
% Correct frame indexing
y(:,:,iframe) = currentYComponent;
u(:,:,iframe) = currentUComponent;
v(:,:,iframe) = currentVComponent;

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Products


Release

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!