matlab中如何将视频保存成图像
利用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中如何将视频保存成图像的更多相关文章
- 在WPF程序中将控件所呈现的内容保存成图像(转载)
在WPF程序中将控件所呈现的内容保存成图像 转自:http://www.cnblogs.com/TianFang/archive/2012/10/07/2714140.html 有的时候,我们需要将控 ...
- 在WPF程序中将控件所呈现的内容保存成图像
原文:在WPF程序中将控件所呈现的内容保存成图像 有的时候,我们需要将控件所呈现的内容保存成图像保存下来,例如:InkCanvas的手写墨迹,WebBrowser中的网页等.可能有人会说,这个不就是截 ...
- WPF编程,将控件所呈现的内容保存成图像的一种方法。
原文:WPF编程,将控件所呈现的内容保存成图像的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/detai ...
- Opencv 视频保存为图像
// 视频存为图片.cpp : 定义控制台应用程序的入口点. // /*================================================================ ...
- MVC下 把数据库中的byte[]值保存成图片,并显示在view页面
MVC下 把数据库中的byte[]值转成图片,并显示在view页面 controller中的action方法 //显示图片[AllowAnonymous]public ActionResult Sho ...
- matlab中动态绘图并保存为视频的小例子
如题,多的就不说了,先上一个效果: 每隔0.1秒,绿色的直线转动一个角度. 再看看代码如何实现: fuction main clear; clc; %%%%%%%%%%%%%%%%%%%%%%%%%% ...
- Matlab中如何用命令方式保存图像?
命令很简单,例如下面这个代码将当前图像保存到F1.emf文件中,保存格式为emf saveas(gcf,'F.emf','emf'); 当然了,也可以保存为jpg格式,修改为: saveas(gcf, ...
- matlab中图片数据类型转换uint8与double
matlab中处理图像像素点数据: img1=double(imread('lenna.bmp')); matlab中imshow图片,要先转换成uint8: subplot(1,2,1),imsho ...
- 在OpenCV中实现matlab中的im2double功能
最近在把matlab的代码转化到VS2010上. matlab中采用im2double将读入的图像转换为double型,在OpenCV中就需要对图像进行深度的转换. 读入一幅灰度图像,深度为1(8U) ...
随机推荐
- HDU 4352 XHXJ's LIS 数位dp lis
目录 题目链接 题解 代码 题目链接 HDU 4352 XHXJ's LIS 题解 对于lis求的过程 对一个数列,都可以用nlogn的方法来的到它的一个可行lis 对这个logn的方法求解lis时用 ...
- POJ.2750.Potted Flower(线段树 最大环状子段和)
题目链接 /* 13904K 532ms 最大 环状 子段和有两种情况,比如对于a1,a2,a3,a4,a5 一是两个端点都取,如a4,a5,a1,a2,那就是所有数的和减去不选的,即可以计算总和减最 ...
- GDB高级调试
一.多线程调试 多线程调试可能是问得最多的.其实,重要就是下面几个命令: info thread 查看当前进程的线程. thread <ID> 切换调试的线程为指定ID的线程. break ...
- C++学习笔记44:继承与派生
类的组合,类的继承 类的组合(汽车类,轮子类,此时可以把轮子类组合到汽车类:) 类的继承(交通工具类,汽车类,此时汽车类可以派生自交通工具类:) 组合:常用描述has a.. 继承:常用描述is a ...
- unity3d的Animation 动画播放器的基本API
直接上代码: //动画名称 private const string ANIM_NAME = "Take 001"; //模型对象 private GameObject obj = ...
- ASP.NET MVC 4 中的JSON数据交互
前台Ajax请求很多时候需要从后台获取JSON格式数据,一般有以下方式: 拼接字符串 return Content("{\"id\":\"1\",\& ...
- OEMbutton乱码问题解决
一.出现故障: 在Linux环境中安装tid=12">Oracle 10g,启用EM时.出现button显示乱码现象,例如以下: 二.分析问题: 由于在安装Oracle10g时,JDK ...
- 修改Unity中Lua文件的默认打开程序
项目中引用了XLua,而Lua文件又是以txt文件结尾的,当修改系统的扩展脚本编辑器为vs后双击lua文件(xx.txt)默认也使用vs打开了,无提示的黑白文本编辑 昨办? -. 后来看到网上有写Un ...
- 结合MongoDB开发LBS应用(转)
原文链接:结合MongoDB开发LBS应用 简介 随着近几年各类移动终端的迅速普及,基于地理位置的服务(LBS)和相关应用也越来越多,而支撑这些应用的最基础技术之一,就是基于地理位置信息的处理.我所在 ...
- Redis更新的正确方法
原文(缓存更新的套路):看到好些人在写更新缓存数据代码时,先删除缓存,然后再更新数据库,而后续的操作会把数据再装载的缓存中.然而,这个是逻辑是错误的.试想,两个并发操作,一个是更新操作,另一个是查询操 ...