利用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. GIF录制

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha ============= 快手电脑版_快手_gif快手电脑版 GIF动画录制工具|GI ...

  2. Python3NumPy——常用函数

    Python3NumPy的常用函数 1. txt文件 (1) 单位矩阵,即主对角线上的元素均为1,其余元素均为0的正方形矩阵. 在NumPy中可以用eye函数创建一个这样的二维数组,我们只需要给定一个 ...

  3. Redis集群官方推荐方案 Redis-Cluster

    Redis-Cluster redis使用中遇到的瓶颈 我们日常在对于redis的使用中,经常会遇到一些问题 1.高可用问题,如何保证redis的持续高可用性. 2.容量问题,单实例redis内存无法 ...

  4. win2008R2管理员密码修改文档

    场景:忘记了win2008R2服务器的管理员密码.解决办法:1. 制作一个U盘启动盘:2. 系统通过U盘启动进入WINpe系统3. 在知道Win2008安装位置的情况下:查找C:\windows\sy ...

  5. win7 64位系统彻底卸载mysql

    一.在控制面板中卸载mysql软件,卸载后删除mysql安装目录下的所有文件 二.windows+R运行“regedit”文件,打开注册表 三.删除注册表:HKEY_LOCAL_MACHINE\SYS ...

  6. RouterOS配置静态IP上网/RouterOS做为二级路由上网

    说明:这里只展示关键步骤. -1.lan口的设置以及dhcp服务器的这些设置这里省略,参考:https://www.cnblogs.com/EasonJim/p/9589714.html 0.设置网卡 ...

  7. EBS R12.2.4 Changing IP

    [root@ebs ~]# vi /etc/hosts 127.0.0.1       localhost.localdomain   localhost ::1     localhost6.loc ...

  8. Android NDK 使用自己的共享库(Import Module)

    LOCAL_PATH := $(call my-dir)//标准mk语句,指编译路径,所有mk文件第一句都是这个 /**这个模块表示引用了一个本地的静态库include $(CLEAR_VARS) / ...

  9. JSON数据之使用Fastjson进行解析(一)

    据说FastJson是目前最快的解析Json数据的库,而且是国人开发出来的开源库.顶一下,付上官方网址:http://code.alibabatech.com/wiki/pages/viewpage. ...

  10. iOS:针对固定数据源,更好的封装cell

    一.介绍 在iOS开发中,tableView非常常用,能将其展示出来,它的数据源必不可少.当然数据源有动态下发的,有固定写死的,这里我只探讨固定写死的情况.对于死数据,我们在项目中经常遇到的场景就是我 ...