利用MATLAB将视频的每一帧保存成一幅图像,并自动命名。本文方法简单,容易学习。

首先,读入视频。代码如下:

mov = VideoReader('xxxxxx.avi');    % 将xxxxxx.avi读入MATLAB中,并用名为mov的结构体保存
fnum = mov.NumberOfFrames; % 获取视频帧数

接下来,我们要写一个循环来将视频的每一帧保存成.png格式的图片。代码如下:

% 将第i帧写入到xxx文件夹内"img_000x.png"图片中
imgOrder=0; % 图片按顺序编号
for i = 1:3:fnum % i从1到fnum, step=3
Img=read(mov,i); % 读取第i帧;每次读取一帧可防止内存不足
imwrite(Img,['xxx/img_',sprintf('%04d',imgOrder),'.png']) % 将第i帧写入到xxx文件夹内"img_000j.png"图片, j=imgOrder
imgOrder=imgOrder+1;
end

程序运行结束后,会在xxx文件夹中生成fnum/step张图片,每一张图片对应视频中相应的一帧。

如果需要做一些预处理,如选取感兴趣区域ROI、下采样等,可以通过修改倒数第二句话实现:

imwrite(Img,['xxx/img_',sprintf('%04d',imgOrder),'.png']); % 假设每一帧的原始大小为:1920*1080(宽*高)

--> imwrite(Img(51:950,151:1550,:),['xxx/img_',sprintf('%04d',imgOrder),'.png']); % 选取ROI,保存的图像大小为:1400*900

--> imwrite(Img(51:2:950,151:2:1550,:),['xxx/img_',sprintf('%04d',imgOrder),'.png']); % 选取ROI并下采样,保存的图像大小为:700*450

相关文档:

help VideoReader

VIDEOREADER Create a multimedia reader object.

OBJ = VIDEOREADER(FILENAME) constructs a multimedia reader object, OBJ, that can read in video data from a multimedia file. FILENAME is a string specifying the name of a multimedia file. There are no restrictions on file extensions. By default, MATLAB looks for the file FILENAME on the MATLAB path.

If the object cannot be constructed for any reason (for example, if the file cannot be opened or does not exist, or if the file format is not recognized or supported), then MATLAB throws an error.

OBJ = VIDEOREADER(FILENAME, 'P1', V1, 'P2', V2, ...) constructs a multimedia reader object, assigning values V1, V2, etc. to the specified properties P1, P2, etc.

If an invalid property name or property value is specified, MATLAB throws an error and the object is not created. Note that the property value pairs can be in any format supported by the SET function, e.g. parameter-value string pairs, structures, or parameter-value cell array pairs.

Example:

% Construct a multimedia reader object associated with file 'xylophone.mpg' with
% user tag set to 'myreader1'.
readerobj = VideoReader('xylophone.mpg', 'tag', 'myreader1'); % Read in all video frames.
vidFrames = read(readerobj); % 此句会消耗大量内存,易导致计算机内存不足 % Get the number of frames.
numFrames = get(readerobj, 'numberOfFrames'); % Create a MATLAB movie struct from the video frames.
for k = 1 : numFrames
mov(k).cdata = vidFrames(:,:,:,k);
mov(k).colormap = [];
end % Create a figure
hf = figure; % Resize figure based on the video's width and height
set(hf, 'position', [150 150 readerobj.Width readerobj.Height]) % Playback movie once at the video's frame rate
movie(hf, mov, 1, readerobj.FrameRate);

doc VideoReader

Use the VideoReader function with the read method to read video data from a file into the MATLAB workspace.

The file formats that VideoReader supports vary by platform, as follows (with no restrictions on file extensions):

All Platforms:

Motion JPEG 2000 (.mj2)

Windows:

AVI (.avi),

MPEG-1 (.mpg),

Windows Media Video (.wmv, .asf, .asx),

and any format supported by Microsoft DirectShow.

Macintosh:

AVI (.avi),

MPEG-1 (.mpg),

MPEG-4 (.mp4, .m4v),

Apple QuickTime Movie (.mov),

and any format supported by QuickTime as listed on http://www.apple.com/quicktime/player/specs.html.

Linux:

Any format supported by your installed plug-ins for GStreamer 0.10 or above, as listed on http://gstreamer.freedesktop.org/documentation/plugins.html, including AVI (.avi) and Ogg Theora (.ogg).

For more information, see Supported Video File Formats in the MATLAB Data Import and Export documentation

参考文献:

[1] http://jingyan.baidu.com/article/642c9d34e520d9644a46f7b7.html

[2] matlab帮助文档

matlab中如何将视频保存成图像的更多相关文章

  1. 在WPF程序中将控件所呈现的内容保存成图像(转载)

    在WPF程序中将控件所呈现的内容保存成图像 转自:http://www.cnblogs.com/TianFang/archive/2012/10/07/2714140.html 有的时候,我们需要将控 ...

  2. 在WPF程序中将控件所呈现的内容保存成图像

    原文:在WPF程序中将控件所呈现的内容保存成图像 有的时候,我们需要将控件所呈现的内容保存成图像保存下来,例如:InkCanvas的手写墨迹,WebBrowser中的网页等.可能有人会说,这个不就是截 ...

  3. WPF编程,将控件所呈现的内容保存成图像的一种方法。

    原文:WPF编程,将控件所呈现的内容保存成图像的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/detai ...

  4. Opencv 视频保存为图像

    // 视频存为图片.cpp : 定义控制台应用程序的入口点. // /*================================================================ ...

  5. MVC下 把数据库中的byte[]值保存成图片,并显示在view页面

    MVC下 把数据库中的byte[]值转成图片,并显示在view页面 controller中的action方法 //显示图片[AllowAnonymous]public ActionResult Sho ...

  6. matlab中动态绘图并保存为视频的小例子

    如题,多的就不说了,先上一个效果: 每隔0.1秒,绿色的直线转动一个角度. 再看看代码如何实现: fuction main clear; clc; %%%%%%%%%%%%%%%%%%%%%%%%%% ...

  7. Matlab中如何用命令方式保存图像?

    命令很简单,例如下面这个代码将当前图像保存到F1.emf文件中,保存格式为emf saveas(gcf,'F.emf','emf'); 当然了,也可以保存为jpg格式,修改为: saveas(gcf, ...

  8. matlab中图片数据类型转换uint8与double

    matlab中处理图像像素点数据: img1=double(imread('lenna.bmp')); matlab中imshow图片,要先转换成uint8: subplot(1,2,1),imsho ...

  9. 在OpenCV中实现matlab中的im2double功能

    最近在把matlab的代码转化到VS2010上. matlab中采用im2double将读入的图像转换为double型,在OpenCV中就需要对图像进行深度的转换. 读入一幅灰度图像,深度为1(8U) ...

随机推荐

  1. 安卓工作室 文件浏览器 android studio File browser

    安卓工作室 文件浏览器 android studio  File browser 作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: ...

  2. CF1039C.Network Safety

    目录 题目链接 题解 代码 题目链接 CF1039C.Network Safety 题解 对于一对相邻点,^异或后相同的值唯一a_i ^ t= b_i,a_i ^ b_i = t 对于不在t集合的直接 ...

  3. 潭州课堂25班:Ph201805201 爬虫基础 第十二课 点触验证码二 (课堂笔记)

    为上次代码添加 模拟人操作 的鼠标的移动轨迹 # -*- coding:utf-8 -*- # 斌彬电脑 # @Time : 2018/9/14 0014 上午 8:08 from selenium ...

  4. CF733F Drivers Dissatisfaction【链剖】【最小生成树应用】

    F. Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  5. Android MediaPlayer架构 -- 前言小知识点(二)

    本文系作者自己学习之所用,文章内容仅出自作者拙劣之思考,问题之处烦请不吝指教. 在frameworks\av\media\libmedia\mediaplayer.cpp中会有语句:const sp& ...

  6. 轻松学C#----第一篇笔记

    C#是微软公司推出的一种面向对象的开发语言,于2002年发布,经历了10年的发展,已经广泛应用于各种生产领域.以它为基础语言的ASP.NET已经称为主流网站开发技术.[入门] 共三章: 第一篇:C#概 ...

  7. oracle 列相减——(Oracle分析函数Lead(),Lag())

    lag和lead函数,用于取出数据的前n行的数据和后n行的数据,当然要和over(order by)一起组合 其实这2个函数的作用非常好理解,Lead()就是取当前顺序的下一条记录,相对Lag()就是 ...

  8. Jenkins配置基于角色的项目权限管理

    转自: http://www.cnblogs.com/gao241/archive/2013/03/20/2971416.html, 版权归原作者. 本文将介绍如何配置jenkins,使其可以支持基于 ...

  9. EBS 由数据库端找到对应的前台URL地址

    SELECT home_url FROM icx_parameters; SELECT profile_option_value     FROM fnd_profile_option_values  ...

  10. mac pro 如何让终端默认运行python3.X而不是2.7

    Mac版本的Python默认是2.7,安装高版本后需要修改为你安装的版本. 1,首先打开终端 open ~/.bash_profile 打开配置文件 2. 写入python的外部环境变量(本人的版本是 ...