matlab中如何将视频保存成图像
2017-02-22 09:29
1101 查看
利用MATLAB将视频的每一帧保存成一幅图像,并自动命名。本文方法简单,容易学习。
首先,读入视频。代码如下:
接下来,我们要写一个循环来将视频的每一帧保存成.png格式的图片。代码如下:
程序运行结束后,会在xxx文件夹中生成fnum/step张图片,每一张图片对应视频中相应的一帧。
如果需要做一些预处理,如选取感兴趣区域ROI、下采样等,可以通过修改倒数第二句话实现:
-->
-->
相关文档:
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:
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帮助文档
首先,读入视频。代码如下:
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 如何截取视频中的每一帧图像
- matlab figute窗口如何 保存的高分辨率图像
- 如何使用matlab从视频中提取出一帧帧图像
- Matlab如何保存被plot的图像
- Matlab中如何将视频转换成一帧一帧的图片并输出保存
- MATLAB读取图像的每一帧处理之后保存成视频文件
- matlab 视频转换到图像并保存
- matlab如何截取视频中的每一帧图像
- 如何保存Matlab/Simulink中示波器显示的图像
- matlab生成GIF动态图像并保存或matlab生成avi视频
- Matlab 如何截取视频中的每一帧图像
- 使用mplayer查看摄像头和luvcview保存YUV图像视频的播放
- OpenCV学习笔记(12)OpenCV调用Matlab函数进行保存视频的尝试
- Matlab 保存多个图像 getframe、imwrite、sprintf、num2str、doc
- 如何将RGB图像数据保存成tiff,可以保存曝光,增益,白平衡参数
- 利用OpenCV进行【视频的读取】与【保存视频文件或者图像】源码!~
- 如何使用R保存图像
- 在RFT中如何截取屏幕图像并保存到文件中?
- 如何用matlab绘微分方程组解的图像
- 在Qt中如何加载图像以及保存图像到数据库中