如何使用 matlab 从 yuv 420 视频剪辑中提取帧并将它们存储为不同的图像?

发布于 2024-09-16 23:41:18 字数 44 浏览 4 评论 0原文

如何从 yuv 420 视频中提取帧?假设我想将它们存储为静态图像。如何?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟若柳尘 2024-09-23 23:41:18

这是来自 MathWorks File Exchange 的提交,它应该可以满足您的要求:

函数loadFileYuv 将加载 YUV 文件并返回电影帧数组。每个电影帧都是一个具有以下字段的结构:

  • cdatauint8 值的矩阵。尺寸为高 x 宽 x 3。
  • colormap:N×3 双精度矩阵。在真彩色系统上它是空的。

因此,您可以从数组中的每个影片帧中提取 cdata 字段,并将其保存/用作 RGB 图像。

您的代码可能如下所示:

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

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

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
海风掠过北极光 2024-09-23 23:41:18

抱歉无法帮助您使用 matlab,但在命令行上您可以使用 ffmpeg

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

-r 1 表示速率 = 每帧

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

靑春怀旧 2024-09-23 23:41:18

您可以使用下面的代码:

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!

您可以访问矩阵 mov1 中的每一帧:)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文