Have you ever tried to create Movie trailer of your favorite
movie or playback of your favorite pictures? Here let’s create one. Let’s try to create a Movie trailer,
a simple one.
Store all the images that are needed to create your video in
a folder.
There are two methods to create a video file. First method is creating video with array of
Images and the second method is write one frame at a time to the video file.
Method 1:
Read all the images into an array and write to the video
file.
MATLAB CODE:
%Create Video with Image
Sequence
clear all
clc
%Make the Below path as the
Current Folder
cd('C:\Documents and
Settings\AARON\My Documents\MATLAB\Images');
%Obtain all the JPEG format
files in the current folder
Files = dir('*.jpg');
%Find the total number of
JPEG files in the Current Folder
NumFiles= size(Files,1);
%Preallocate a 4-D matrix
to store the Image Sequence
%Matrix Format : [Height
Width 3 Number_Of_Images]
Megamind_Images =
uint8(zeros([600 1000 3 NumFiles*5]));
%To write Video File
VideoObj = VideoWriter('Create_Video.avi');
%Number of Frames per
Second
VideoObj.FrameRate = 5;
%Define the Video Quality [
0 to 100 ]
VideoObj.Quality = 80;
count=1;
for i = 1 : NumFiles
%Read the Images in the Current Folder one by one using For
Loop
I = imread(Files(i).name);
%The Size of the Images are made same
ResizeImg = imresize(I,[600 1000]);
%Each Image is copied 5 times so that in a second 1 image
can be viewed
for j = 1 : 5
Megamind_Images(:,:,:,count)=ResizeImg;
count = count + 1;
end
end
%Open the File
'Create_Video.avi'
open(VideoObj);
%Write the Images into the
File 'Create_Video.avi'
writeVideo(VideoObj,
Megamind_Images);
%Close the file
'Create_Video.avi'
close(VideoObj);
EXPLANATION:
1. %Make the
Below path as the Current Folder
cd('C:\Documents and Settings\AARON\My
Documents\MATLAB\Images');
The
‘cd’ command is used to make the given path as current folder.
When
you execute the above command, you can find the current folder changed to the
path specified in the above command.
2. %Obtain
all the JPEG format files in the current folder
Files = dir('*.jpg');
The
‘Files’ variable contains metadata.
>> Files(:).name
ans =
Megamind_01.jpg
ans =
Megamind_02.jpg
ans =
Megamind_03.jpg
ans =
Megamind_04.jpg
ans =
Megamind_05.jpg
ans =
Megamind_06.jpg
ans =
Megamind_07.jpg
3. %To write
Video File
VideoObj = VideoWriter('Create_Video.avi');
Mention the Video file name
4. %Number of
Frames per Second
VideoObj.FrameRate = 5;
Five frames will be played in 1
second.
5.
‘NumFiles’ contain number of JPEG images in the current folder.
In our example, the number of JPEG images is 7.
>> NumFiles
In our example, the number of JPEG images is 7.
>> NumFiles
NumFiles =
7
6.
Read the first Image using ‘Imread’ function.
7.
Resize the image to a size of your choice. See that you resize all
the images to a constant size.
8.
If we use only these 7 images then the video will be played within
1.4 seconds. And you may not be able to view the images in the video. So in
order to make all the images viewable for certain time, each frame is
replicated 5 times.
9.
The Number of Frames per second is 5. Since each image is
replicated for 5 times, one image will be seen per second. Therefore, our video
will run for 7 seconds with 7 Images * 5 repetition = 35 Images.
10.Store these 35 images in a matrix.
Images(:,:,:,count)=ResizeImg;
Images(:,:,:,count)=ResizeImg;
11.Finally, open the video file and write
the image matrix and close the file.
METHOD 2:
In the previous method, we wrote the
entire 35 images stored matrix to the video file at once. Here we are going to
read the image, convert to movie frame and write it one by one to the video
file.
MATLAB CODE:
%Create Video with Image
Sequence
clear all
clc
%Make the Below path as the
Current Folder
cd('C:\Documents and
Settings\AARON\My Documents\MATLAB\Images');
%Obtain all the JPEG format
files in the current folder
Files = dir('*.jpg');
%Number of JPEG Files in
the current folder
NumFiles= size(Files,1);
%To write Video File
VideoObj = VideoWriter('Create_Video01.avi');
%Number of Frames per
Second
VideoObj.FrameRate = 5;
%Define the Video Quality [
0 to 100 ]
VideoObj.Quality = 80;
%Open the File
'Create_video01.avi'
open(VideoObj);
for i = 1 : NumFiles
%Read the Image from the current Folder
I = imread(Files(i).name);
%Resize Image
ResizeImg = imresize(I,[600 1000]);
%Convert Image to movie Frame
frame = im2frame(ResizeImg);
%Each Frame is written five times.
for j = 1 : 5
%Write a frame
writeVideo(VideoObj, frame);
end
end
%Close the File
'Create_Video01.avi
close(VideoObj);
EXPLANATION:
1.
After reading the image from the current folder, convert the image
to movie frame using ‘im2frame’ function.
2.
Write the frame to the file and read next image and repeat the process
of converting and writing to the file till the last image is processed and
written.
3 comments:
Thank you. Your post is very helpful
There is color map error.
thanks bro my project completed
Enjoyed Reading? Share Your Views