利用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. 更新 是 可用的 针对 安卓 软件开发包和工具 Updates are available for android software development packages and tools

    作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com 更新 是 可用的 针对 安卓 软件开发包和工 ...

  2. loj#2665. 「NOI2013」树的计数

    目录 题目链接 题解 代码 题目链接 loj#2665. 「NOI2013」树的计数 题解 求树高的期望 对bfs序分层 考虑同时符合dfs和bfs序的树满足什么条件 第一个点要强制分层 对于bfs序 ...

  3. php parse_url 解析URL并返回其组成部分

    [导读] php parse_url 函数教程parse_url ( PHP 4中, PHP 5中) parse_url -解析URL并返回其组成部分 描述 混合parse_url (字符串$网址[摘 ...

  4. 虚拟机(VMware Workstation)的使用方法(转)

    虚拟机(VMware Workstation)的使用方法:http://www.aboutyun.com/thread-6273-1-1.html

  5. FTP和TCP的文件传输效率对比测试分析

    前言 最近因项目需要,需要把一定数量的中等文件从开发板上传到电脑上,分别选择了FTP和TCP自定义协议两种方式进行传输,进行了简单的对比测试,故做如下记录. 测试环境 开发板:Linux,ARMv7 ...

  6. Java项目收藏

    数据库 MyCat:数据库中间件 IM.消息推送 t-io:不仅仅是百万级即时通讯框架 tio-im:基于t-io写的IM,主要目标降低即时通讯门槛,实现多端不同协议间的消息发送如http.webso ...

  7. android: 实现强制下线功能

    强制下线功能应该算是比较常见的了,很多的应用程序都具备这个功能,比如你的 QQ 号在别处登录了,就会将你强制挤下线.其实实现强制下线功能的思路也比较简单,只需要 在界面上弹出一个对话框,让用户无法进行 ...

  8. mysql yum安装

    # 下载yum源的rpm包wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm# 安装rpm包rpm - ...

  9. JS Range使用整理

    1.获取用户网页选中内容 <p>4月13日消息,据台湾媒体报道,32岁的孙燕姿(Sng Ee Tze)和后天将满34岁的荷兰籍印度尼西亚男友纳迪姆(Nadim Van Der Ros)交往 ...

  10. gitolite migration to bitbucket

    https://gist.github.com/kostajh/9249937 https://designhammer.com/blog/easily-migrate-git-repositorie ...