如何在matlab中将3D数组直接转换为视频 [英] How to convert a 3D array directly to a video in matlab

查看:36
本文介绍了如何在matlab中将3D数组直接转换为视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个尺寸为 600x600x28 的 3D 矩阵,最后一个索引是帧数,我想将其转换为视频文件.正如您在下面的代码中看到的,我将数组转换为大小为 600X600X3X28 的 4D 矩阵,以使其与 WriteVideo 格式兼容.但我不知道为什么输出视频是空的!

I have a 3D matrix with a dimension of 600x600x28 which the last index is the number of frames and want to convert it to a video file. As you can see in the code below I convert the array into a 4D matrix with the size of 600X600X3X28 to make it compatible with WriteVideo format. but I am not sure why the output video is empty!

Orig = randi([1 1000],600,600,28);

x = uint8(255 * mat2gray(Orig));
map=jet;

for i=1:size(x,3)
x_all(:,:,:,i) = ind2rgb(x(:,:,i),map);
end

x_all = uint8(x_all);
v = VideoWriter('myvideo');
open(v);
writeVideo(v,x_all);
close(v)

推荐答案

您不必将视频转换为 RGB,您可以创建灰度视频.

You don't have to convert the video to RGB, you may create a Grayscale video.

示例:

x = randi([0, 255], 600, 600, 28, 'uint8');
v = VideoWriter('myvideo_grayscale.avi', 'Grayscale AVI');
open(v);
writeVideo(v, x);
close(v)


您收到黑色视频的原因是 ind2rgb 返回范围 [0, 1] 内的值,并且在转换为 uint8 时:x_all = uint8(x_all);,所有的值都是0s和1s(而不是范围[0, 255]).


The reason you are getting a black video is that ind2rgb returns values in range [0, 1] and when converting to uint8: x_all = uint8(x_all);, all the values are 0s and 1s (instead of range [0, 255]).

创建彩色视频的示例:

Orig = randi([1 1000],600,600,28);

x = im2uint8(mat2gray(Orig));

map = jet(256); % Use colormap with 256 colors (the default is 64).

x_all = zeros(size(x, 1), size(x, 2), 3, size(x, 3), 'uint8'); % Allocate space

for i=1:size(x,3)
    x_all(:,:,:,i) = im2uint8(ind2rgb(x(:,:,i), map));
end

v = VideoWriter('myvideo.avi');
open(v);
writeVideo(v,x_all);
close(v)


如果您实际上得到的是一个空的视频文件(不仅仅是黑色视频),则说明您的设置有问题.


In case you are actually getting an empty video file (not just black video), something is wrong with your setup.

这篇关于如何在matlab中将3D数组直接转换为视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆