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

一世执手 提交于 2019-12-19 04:14:09

问题


How do I extract the frames from a yuv 420 video? Let's say i want to store them as still images. How?


回答1:


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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/3614441/how-to-extract-frames-from-yuv-420-video-clip-and-store-them-as-different-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!