writeVideo
将视频数据写入到文件
说明
示例
将图像写入 Motion JPEG 2000 文件
将一个 RGB 图像写入采用无损压缩的 Motion JPEG 2000 文件。
创建一个包含来自示例静态图像 peppers.png
的数据的数组。
A = imread('peppers.png');
为新视频文件创建一个 VideoWriter
对象。使用 'Archival'
描述文件指定一个采用无损压缩的 Motion JPEG 2000 文件。
v = VideoWriter('myFile','Archival');
验证新文件的视频压缩类型。
v.VideoCompressionMethod
ans = 'Motion JPEG 2000'
打开要写入的视频文件。然后,将 A
中的图像数据写入该文件。
open(v) writeVideo(v,A)
关闭视频文件。
close(v)
将索引图像写入 AVI 文件
从示例索引图像文件 corn.tif
中读取图像和颜色图数据。
[X,map] = imread('corn.tif');
为新的索引 AVI 文件创建一个 VideoWriter
对象。
v = VideoWriter('myIndexed.avi','Indexed AVI');
将颜色图数据分配到 v
的 Colormap
属性。
v.Colormap = map;
打开要写入的文件。打开该文件后,您将无法更改 v
的属性。
open(v)
将 X
中的图像数据写入视频文件。然后关闭文件。
writeVideo(v,X) close(v)
将 MPEG-4 转换为 AVI 文件
将示例文件 xylophone.mp4
转换为未压缩的 AVI 文件。
创建要读取的对象并写入视频,然后打开 AVI 文件进行写入。
reader = VideoReader('xylophone.mp4'); writer = VideoWriter('transcoded_xylophone.avi', ... 'Uncompressed AVI'); writer.FrameRate = reader.FrameRate; open(writer);
读取和写入每一帧。
while hasFrame(reader) img = readFrame(reader); writeVideo(writer,img); end close(writer);
从动画创建 AVI 文件
按以下步骤将一组帧写入压缩的 AVI 文件:生成一组帧,为要写入的文件创建视频对象,然后将帧写入视频文件。
设置坐标区和图窗属性,以生成视频帧。
Z = peaks; surf(Z); axis tight manual set(gca,'nextplot','replacechildren');
为输出视频文件创建 VideoWriter 对象并打开该对象以进行写入。
v = VideoWriter('peaks.avi');
open(v);
生成一组帧,从图窗中获取帧,然后将每一帧写入文件。
for k = 1:20 surf(sin(2*pi*k/20)*Z,Z) frame = getframe(gcf); writeVideo(v,frame); end close(v);
输入参数
v
— 输入 VideoWriter
对象
VideoWriter
对象
输入 VideoWriter
对象。使用 VideoWriter
创建该对象。
img
— 表示灰度或 RGB 彩色图像的值
二维数组 | 三维数组 | 四维数组
表示灰度或 RGB 彩色图像的值,指定为二维、三维或四维数组:
对于单一灰度图像、单色图像或索引图像,
img
必须是二维的:高×宽。对于单一真彩色 (RGB) 图像,
img
是三维的:高×宽×3。对于一系列灰度图像,
img
是四维的:高×宽×1×帧数。文件中所有帧的高和宽必须一致。对于一系列 RGB 图像,
img
是四维的:高×宽×3×帧数。文件中所有帧的高和宽必须一致。
创建 AVI 或 MPEG-4 文件时:
img
是single
、double
或uint8
值的数组,代表一个或多个灰度或 RGB 彩色图像,writeVideo
将这些图像作为一个或多个 RGB 视频帧写入。single
或double
类型的数据必须位于范围[0,1]
中,写入索引 AVI 文件时除外。
创建 Motion JPEG 2000 文件时:
img
是uint8
、int8
、uint16
或int16
值的数组,表示一个或多个单色或 RGB 彩色图像。
数据类型: single
| double
| int8
| int16
| uint8
| uint16
frame
— 帧数据
1×1 结构体数组 | 结构体的 1×F
数组
帧数据,指定为表示单个帧的 1×1 结构体数组,或表示多个帧的 1×F
结构体数组。每个帧都包含两个字段:cdata
和 colormap
。frame
数组通常由 getframe
函数返回。
如果 colormap
不为空,则 cdata
的每个元素应为二维(高×宽)数组。文件中所有帧的高和宽必须一致。
colormap
最多可包含 256 个条目。colormap
的每个元素必须在 [0,1]
范围内。
当您创建一个 VideoWriter
对象时,profile
输入和 cdata
的大小确定 writeVideo
如何使用 frame
。
VideoWriter 对象的 profile | cdata 的每个元素的大小 | writeVideo 的行为 |
---|---|---|
| 二维(高×宽) | 使用所提供的帧。 |
'Grayscale AVI' | 二维(高×宽) | 使用所提供的帧。colormap 应为空。 |
所有其他描述文件 | 二维(高×宽) | 使用 colormap 字段构造 RGB 图像帧 |
三维(高×宽×3) | 忽略 colormap 字段。使用 cdata 字段构造 RGB 图像帧 |
数据类型: struct
版本历史记录
在 R2010b 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)