利用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. [HDU5361]In Touch

    [HDU5361]In Touch 题目大意: 有\(n(n\le2\times10^5)\)个点,每个点有三个属性\(l_i,r_i,c_i\).表示若\(|i-j|\in[l_i,r_i]\),\ ...

  2. C# 格式化新招

    C# 格式化新招 ) from Attribute_Item where AttributeSysNo=$AttributeSysNo and Name='$Name' and SysNo !=$Sy ...

  3. mysql5.7报err 1055错误 sql_mode=only_full_group_by

    vim /etc/my.cnf 末尾增加 sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_B ...

  4. caffe solver 配置详解

    caffe solver通过协调网络前向推理和反向梯度传播来进行模型优化,并通过权重参数更新来改善网络损失求解最优算法,而solver学习的任务被划分为:监督优化和参数更新,生成损失并计算梯度.caf ...

  5. goto语句引起的crosses initialization of XXX

    1. 背景 goto语句虽然目前已经不提倡使用,但是用起来还是很方便,尤其是老代码中见的比较多. 在改动有goto语句的老代码时需要特别注意,是否跳过来资源的释放.有用变量的初始化等等. 很久之前写c ...

  6. LM && NTLM && ophcrack && RainBow table (转)

    Windows密码的加密方式:Windows 主要使用以下两种(包含但不限于)算法对用户名和密码进行加密:分 别是LanManager(LM)和NTLM,LM只能存储小于等于14个字符的密码hash, ...

  7. 版本控制-GitHub

    前面几篇文章,我们介绍了Git的基本用法及Git服务器的搭建,本篇文章来学习一下如何使用GitHub.GitHub是开源的代码库以及版本控制库,是目前使用网络上使用最为广泛的服务,GitHub可以托管 ...

  8. 【LeetCode】238. Product of Array Except Self

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  9. 【C#】详解C#事件

    目录结构: contents structure [+] 事件基本介绍 定义事件类型 定义事件成员 定义引发事件的方法 以线程安全的方式引发事件 登记事件关注 揭秘事件 显式实现事件 为什么需要显式实 ...

  10. Android--保持加速度传感器在屏幕关闭后运行(收集)

    由于写论文需要,需要用手机加速度采集数据,关于android加速度传感器的介绍网上一抓一大把,但大多都是大同小异,跟官网文档差不多.自己写了个取加速度传感器的APK,发现数据有点不对劲,原理屏幕一关后 ...