How To Create Video from Sequence of Images Using MATLAB?


In MATLAB, we can create a video from a sequence of images. In this tutorial, we will explore the steps involved in creating a video from an image sequence and will take an example to understand the concept practically.

Process of Creating Video From a Sequence of Images

The step-by-step process of creating a video from a sequence of images is explained below −

  • Step (1) − Firstly, collect all the images in a folder that we want to use in the video.

  • Step (2) − Read all the images using the 'imread' function and store them in a cell array.

  • Step (3) − Create a video object by using the 'VideoWriter' function and define the name, format, frame rate, etc. of the video.

  • Step (4) − Specify the seconds per image, i.e. duration of each image in the video.

  • Step (5) − Open the video file and write the images into it with the help of 'for' loop and 'writeVideo' function.

  • Step (6) − End the 'for' loop and close the video file.

  • Step (7) − Display the created video using the 'VideoReader' function.

Hence, by following these 7 steps, we can easily create a video from a sequence of images in MATLAB.

Matlab Example

Now, let us consider an example program in MATLAB to demonstrate the process of creating a video from images.

% 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);

Output

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.

Conclusion

This is all about creating a video from a sequence of images in MATLAB. In this tutorial, we explained the step-by-step procedure of creating a video from a sequence of images and demonstrated the implementation of these steps through an example program.

Updated on: 06-Sep-2023

64 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements