开发者

How to extract frames from yuv 420 video clip and store them as different images, using matlab?

开发者 https://www.devze.com 2023-01-13 18:39 出处:网络
How do I extract the frames from a yuv 420 video? Let\'s say i want to store them as sti开发者_JS百科ll images. How?Here\'s a submission from the MathWorks File Exchange that should do what you want:

How do I extract the frames from a yuv 420 video? Let's say i want to store them as sti开发者_JS百科ll images. How?


Here's a submission from the MathWorks File Exchange that should do what you want:

  • Convert YUV CIF 4:2:0 video file to image files by Da Yu

The function loadFileYuv from the above submission will load a YUV file and return an array of movie frames. Each movie frame is a structure with the following fields:

  • cdata: A matrix of uint8 values. The dimensions are height-by-width-by-3.
  • colormap: An N-by-3 matrix of doubles. It is empty on true color systems.

You can therefore extract the cdata field from each movie frame in the array and save/use it as an RGB image.

Your code might look something like this:

nFrames = 115;     %# The number of frames
vidHeight = 352;   %# The image height
vidWidth = 240;    %# The image width
mov = loadFileYuv('myVideo.yuv',vidHeight,vidWidth,1:nFrames);  %# Read the file
for k = 1:nFrames  %# Loop over the movie frames
  imwrite(mov(k).cdata,['myImage' int2str(k) '.bmp']);  %# Save each frame to
                                                        %#   a bitmap image file
end


sorry can't help with matlab but on the command line you can do it with ffmpeg

ffmpeg -i input.yuv -r 1 -f image2 images%05d.png

-r 1 means rate = every frame


You can use this code below:

vidObj1 = mmreader('testballroom_0.avi');  %# Create a video file object
nFrames = vidObj1.NumberOfFrames;   %# Get the number of frames
vidHeight1 = vidObj1.Height;         %# Get the image height
vidWidth1 = vidObj1.Width;           %# Get the image width

%# Preallocate the structure array of movie frames:

mov1(1:nFrames) = struct('cdata',zeros(vidHeight1,vidWidth1,3,'uint8'),...
                    'colormap',[]);  %# Note that colormap is empty!

You can access each frame from the matrix mov1 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消