Follow by Email

Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Recent Posts

Create Video with Sequence of Images


                   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

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; 
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.










like button Like "IMAGE PROCESSING" page

How to add caption/subtitles to Video in MATLAB


                                    In my previous post, I discussed about how to create videofrom still images and now we can add subtitles to the video. I used the ‘text’ function to add the subtitles.
The procedure to add subtitles to the Video:
·         Read an video file
·         Read each video frame data
·         Add text to each frame using the syntax ‘text(x,y,string)’.
·         Use imshow to display the image.
·         Get the frame from the figure displayed.

MATLAB CODE:
%Input a Video file
obj=mmreader('Poem.avi');
A=read(obj);
j=1;
%Preallocate the frame structure
Frame=struct('cdata',1,'colormap',cell([1 100]));

                                 INPUT VIDEO
 

%Get the first frame as image
%Add text
%Use 'getframe' to grab the figure
Caption={'How to add Caption/Subtitles';'to video in MATLAB';'GET CODE AT http://angeljohnsy.blogspot.com/';};
I=A(:,:,:,1);
imshow(I),hold on
text(80,100,Caption{1},'Color','w','FontWeight','Bold','FontSize',30);
text(150,200,Caption{2},'Color','w','FontWeight','Bold','FontSize',30);
text(300,480,Caption{3},'Color','r','FontWeight','Bold','FontSize',15);
Frame(j:j+9)=getframe;
j=j+10;


%Get the Second frame as an image i.eA(:,:,:,2);
%Add the Poem title and the author name
Title_={'THE ROAD NOT TAKEN';'-ROBERT FROST'};
I=A(:,:,:,2);
imshow(I), hold on
text(40,100,Title_{1},'Color','k','FontWeight','Bold','FontSize',40);
text(350,550,Title_{2},'Color','k','FontWeight','Bold','FontSize',30);
Frame(j:j+9)=getframe;
j=j+10;


%Create a cell array with the Poem versus
Txt={'TWO roads diverged in a yellow wood,';
'And sorry I could not travel both';
'And be one traveler, long I stood';
'And looked down one as far as I could';   
'To where it bent in the undergrowth;';
'Then took the other, as just as fair,';   
'And having perhaps the better claim,';
'Because it was grassy and wanted wear;';  
'Though as for that the passing there';
'Had worn them really about the same,';
'And both that morning equally lay';   
'In leaves no step had trodden black.';
'Oh, I kept the first for another day!';   
'Yet knowing how way leads on to way,';
'I doubted if I should ever come back.';
'I shall be telling this with a sigh'
'Somewhere ages and ages hence:';  
'Two roads diverged in a wood, and I—';
'I took the one less traveled by,';
'And that has made all the difference.';};

%display the image
%Add the poem lines
%Grab the frame
%There are 20 lines totally
%Add 3 and 2 lines alternatively in each frame
    inc=0;
for i=3:size(A,4)
   
    n=mod(i,2);  
    I=A(:,:,:,i);
   
    imshow(I), hold on
for k=1:2+n
    text(80,440+(k*40),Txt{k+inc},'Color','w','FontWeight','Bold','FontSize',25);
   
end
    inc=inc+2+n;
    Frame(j:j+19)=getframe;
    j=j+20;
 end


Create a Video File:
obj=avifile('Road Not Taken.avi','FPS',3);
 obj=addframe(obj,Frame);
obj=close(obj);


                            VIDEO WITH  SUBTITLE






Explanation:
       text(40,100,Title_{1},'Color','k','FontWeight','Bold','FontSize',40);

                This code will add the text ‘THE ROAD NOT TAKEN’ with color font size 40, font weight bold and color black (k).

          

like button Like "IMAGE PROCESSING" page

How to get frames from a video

MATLAB CODE to get images from video:
To extract frames from a video.
              Frames can be obtained from a video and converted into images. To convert a video frame into an image, the MATLAB function ‘frame2im’ is used.  
                                      To read a video in avi format, the function ‘aviread’ is used.  The original format of the video that I am using as an example is .gif file format. I converted the .gif file format image into an avi format video. Learn how to convert a gif image into a video...
 For instance, in the ‘fun.avi’ video I am interested in the frame number 58. So I am converting the 58th frame into an image.
MATLAB CODE:
%How to get frames from a video

mov=aviread('fun.avi');

Im=frame2im(mov(1,58));
figure,imshow(Im);


Frame 58
The above process can be done using 'mmreader' function also.

MATLAB CODE:

Vobj=mmreader('fun.avi');

Im=read(Vobj,58);
imshow(Im);

After playing the video, I thought of modifying some frames. So I converted the frames from 58 to 64 into images and added a smiley to the images by performing image arithmetic operations.

Then I converted those images into frames and saved as a video file in ‘smiley.avi’.

Lmov=aviread('smiley.avi');

MIm=frame2im(Lmov(1,1));
figure,imshow(MIm);

Modified Frame 58


%Preallocating the structure array
frame=struct('cdata',1,'colormap',cell([1 91]));


I added the modified frames to the original video.


%Modify the video in the middle
frame(1,1:57)=mov(1,1:57);
frame(1,58:87)=Lmov(1,1:30);
frame(1,88:91)=mov(1,64:67);
implay(frame,5);












  If we are interested in playing only some of the frames in the original video then, we can view those frames alone. Here I am interested in viewing the frames from 27 to 33.


%Part of the video
scz=figure;
set(scz, 'position', [300 100  size(Im,1)+130 size(Im,2)-100]);
FRange=[1 27:33];
%Here 1 is for the number of times the video has to be repeated
%27:33 indicates the frames from 27 to 33.
movie(scz,mov,FRange,5);

We can also save the desired frames into a file. An avifile object is created with the file name ‘VidPart.avi’. Then the desired frames from 27 to 33 are added by using the syntax ‘addframe’.


%To save the desired part alone

obj=avifile('VidPart.avi','FPS',5);
obj=addframe(obj,mov(27:33));
obj=close(obj);

like button Like "IMAGE PROCESSING" page

How to create Video from still image


                  Using a single JPEG image, we can create image sequences.  To create image sequences, we need to convert the image into frame. The matlab function   ‘im2frame’ is used to create frame from an image. In this example, I took an image and created some frames by performing rotation operation on that image.
                             Initially, I pre allocated a structure array.  When the function ‘im2frame’ is used on an image, a frame is created.  The frame is a structure that contains the cdata and the colormap.


Example:
I=imread(‘ball.jpg’);
frame(1)=im2frame(I);
frame(1)
ans =
       cdata: [323x400x3 uint8]
      colormap: []
After allocating the space for the frames, I rotated the image from 1 to 360 degrees with the interval of 10 degrees.  36 frames will be obtained.
During each rotation operation, the size of the image will be modified. So, I resized the image to the original size after every rotation.
MATLAB CODE:
%Create a video from still image
I=imread('ball.jpg');
m=size(I,1);
n=size(I,2);
j=1;
%Preallocate the structure array
%The fields are 'cdata' and 'colormap'
%The size should be 1xm
frame=struct('cdata',1,'colormap',cell([1 36]));

for i=1:10:360
    %Rotate the image
    Im=imrotate(I,i);
    %To convert an image to frame use the function 'im2frame'.
    frame(j)=im2frame(imresize(Im,[m n]));
j=j+1;
end

%scz=figure;
%set(scz, 'position', [300 100  size(I,1)+200 size(I,2)]);
%movie(scz ,frame,1,30);

%Play the image sequence with fps=10.
implay(frame,10);






We can either use ‘movie’ function or ‘implay’ function to play the video.
Function: movie
If the ‘movie’ function is used. Then we can specify the window size and the position.
%movie(scz ,frame,N,FPS);

N-number of times
FPS-Frames per second
Function :  implay
It can take two parameters, frames and the FPS.
like button Like "IMAGE PROCESSING" page
Next Post Home
Related Posts Plugin for WordPress, Blogger...
Google ping Hypersmash.com