MATLAB 如何从图像序列创建视频

如何使用MATLAB从图像序列创建视频

在MATLAB中,我们可以从图像序列创建视频。在本教程中,我们将探讨从图像序列创建视频的步骤,并以实例来理解概念。

从图像序列创建视频的过程

下面解释了从图像序列创建视频的逐步过程−

  • 步骤(1) − 首先,将要在视频中使用的所有图像收集到一个文件夹中。

  • 步骤(2) − 使用“imread”函数读取所有图像,并将它们存储在一个单元数组中。

  • 步骤(3) − 使用“VideoWriter”函数创建一个视频对象,并定义视频的名称、格式、帧速率等。

  • 步骤(4) − 指定每个图像的秒数,即视频中每个图像的持续时间。

  • 步骤(5) − 使用“for”循环和“writeVideo”函数将图像写入视频文件。

  • 步骤(6) − 结束“for”循环并关闭视频文件。

  • 步骤(7) − 使用“VideoReader”函数显示创建的视频。

因此,通过遵循这7个步骤,我们可以在MATLAB中轻松地从图像序列创建视频。

MATLAB示例

现在,让我们考虑一个在MATLAB中的示例程序,以演示从图像创建视频的过程。

% MATLAB Program to create a video from a sequence of images
% Read the images and store in a cell array
images = cell(4, 1);
images{1} = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425236.jpg');
images{2} = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425323.jpg');
images{3} = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425308.jpg');
images{4} = imread('https://www.tutorialspoint.com/assets/questions/media/14304-1687425340.jpg');

% Resize all the images to a common size
Img_Size = [480, 640];    % Adjust as per your images
for i = 1:length(images)
   images{i} = imresize(images{i}, Img_Size);
end

% Create a video file with 30 fps
vdo = VideoWriter('video_file.avi', 'Motion JPEG AVI');
vdo.FrameRate = 30;

% Specify seconds per image
sec_per_img = 3;

% Open the created video file
open(vdo);

% Load the images to the video file
for i = 1:length(images)
   % Convert the image to a frame
   f = im2frame(images{i});
   % Write each frame multiple times to match seconds per image
   for j = 1:sec_per_img
      writeVideo(vdo, f);
   end
end

% Close the video file
close(vdo);

输出

Replace the image URLs with your images. Run the code in your MATLAB environment, having the required toolboxes to write and play the video files.

结论

这就是关于在MATLAB中从一系列图像创建视频的全部内容。在这个教程中,我们详细解释了从一系列图像创建视频的逐步过程,并通过一个示例程序展示了这些步骤的实现。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程