# IMAGE PROCESSING

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

### Read, process and save video in MATLAB

This tutorial will come in handy if you are interested in learning about video processing using MATLAB. Techniques such as Reading and writing a video file, displaying the frames and writing the frames as images in a folder are discussed below.

‘VideoReader’ command in MATLAB creates reader object for the video file. This object contains the metadata or the parameters related to the video such as Frame rate, Height and Width of the frames,duration of the video etc.
To read all the frames in the video, we can use two methods. The first method is to find the number of frames in the video and read it. The second method is to read the frames until no more video frames are available. Here in the tutorial, the frame rate is assumed to be constant throughout the duration of the video. At constant frame rate, the number of frames in the video is obtained by direct multiplication of frame rate and video duration. In our example, the video is 13.29 seconds long and the frame rate is 24.0. Multiplying 13.29 and 24 gives 318.96 that is 319 frames available in the video.

1. To Read the frames in the video , display , save as image file and store as mat file

EXAMPLE : 1
%MATLAB CODE:

%Num_Frames = Vptr.NumberOfFrames;
NFrames = round(Vptr.FrameRate*Vptr.Duration);

%Find the height and weight of the frame
Nrows = Vptr.height;
Ncols = Vptr.width;

%Preallocate the matrix
Img_s = zeros([Nrows,Ncols,NFrames]);

for i = 1:NFrames

%To display all the frames
figure,imshow(Img);

%To save the images
Img_name=['Image',num2str(i),'.jpg'];
imwrite(Img,Img_name);

%To store in MAT file
Img_s(:,:,i)=Img;
end

%Save the matrix as .mat file
Save Video_Images.mat Img_s;

EXPLANATION:
The above MATLAB code can
a. Display all the frames in the video
b. Save all the frames as images in the current working directory
c. Store all the frames as images in a multidimensional matrix and save it as ‘.mat’ file

After creating the video reader object, the number of frames is computed using the frame rate and duration of the video. The height and width of the frame can be obtained from the metadata.

‘readFrame’ extracts each frame sequentially in the image format. The image can be further displayed using ‘imshow’ or written to an image file using ‘imwrite’ command or stored in a multidimensional matrix as stack of images.

The name format of the images saved in the current working directory will be ‘Image1.jpg’,’Image2.jpg’…’Image319.jpg’

2. To read all the frames in the video and display few frames
EXAMPLE 2:

%MATLAB CODE
NFrames = round(Vptr.FrameRate*Vptr.Duration);

Jump_ptr = 27;
N = 1;

%To display the Images
for i=1:NFrames

if(mod(i-1,Jump_ptr)==0)
figure(2),subplot(3,4,N),imshow(Img);
N=N+1;
end

end

EXPLANATION:
The above MATLAB code reads all the frames in the video but displays only few frames. This example typically highlights the use of MATLAB command ‘subplot’.
Instead of displaying all the frames, frames with specific interval are displayed. In this instance,frame 1 will be displayed first, frame 28 the next then followed by 55 and so on and so forth. ‘mod’ command is used to find the remainder after division, so whenever ‘i’ assumes the value equal to multiples of the variable ‘Jump_ptr’ then the image will be displayed. To displayall the images in the same figure, ‘subplot’ can be used.

‘subplot(3,4,N)’ refers that the ‘figure(2)’ can be divided into 3 rows and 4 columns and each image can be placed in each position.  In the given example, number of frames =319 and the interval distance (Jump_ptr) is 27, then 319/27 gives 12. So the subplot is divided as 3 rows and 4 columns to allocate spacefor 12 images.

3. To read from a video file and write the frames to another video file

%To write frames to the video
Wptr = VideoWriter('VIDE02.mp4','MPEG-4');
Wptr.FrameRate=10;
open(Wptr);
for i=1:120
writeVideo(Wptr,Img);

end
close(Wptr);

EXPLANATION:

Create the video writer object using ‘VideoWriter’ for ‘VIDEO2.mp4’
Set the frame rate for the video to be written to a file.
Here, the frame rate 10 indicates,10 frames will be displayed per second in a video.
‘open’ command will open the video file to start the writing process. Instead of 319 frames from the original video(‘VIDEO1.MP4’), only 120 frames are written to the video file.So it is unnecessary to go through all the frames in the video. First read the frame from the input video file and write it to the output video file. After 120 frames are read from the input file and written to the output file, the output file is closed.

4. To read a video file and process the frames and write it to another video file

%To write frames to the video
%Find number of frames
NFrames = round(Vptr.FrameRate*Vptr.Duration);

%Create Video Writer Object
Wptr = VideoWriter('VIDEO_NOISY.mp4','MPEG-4');
%Open the output video file
open(Wptr);
for i=1:NFrames
Img = imnoise(Img,'salt & pepper');
%write to video file
writeVideo(Wptr,Img);

end
%Close the output video file
close(Wptr);

EXPLANATION:

All the frames in the input video is processed and then written to an output file. Here, noise is added to each frame in the intermediate step and then written to the output video file. However, instead of addition of noise, the image can be enhanced or processed in the intermediate step.

EXAMPLE:

%EXAMPLE - VIDEO PROCESSING
%Set the frame rate
%Crop the Image

%Find number of frames
NFrames = round(Vptr.FrameRate*Vptr.Duration);

%Create Video Writer Object
Wptr = VideoWriter('VIDEO_Enhance.mp4','MPEG-4');

Wptr.FrameRate = 10;
%Open the output video file
open(Wptr);
for i=1:230

Img = imadjust(Img,[0 0 0; 0.7 0.7 0.5],[]);

%Crop undesired portion
Img = Img(1:end,251:end,:);

%write to video file
writeVideo(Wptr,Img);

end
%Close the output video file
close(Wptr);

EXPLANATION:

In this example, the frame rate is set to 10 and Instead of reading all the frames(319), 230 frames are read starting from the first frame. Each frame is enhanced and a portion of it is cropped as well.

Like "IMAGE PROCESSING" page

### 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

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

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

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

%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 "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 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
j=1;
%Preallocate the frame structure
Frame=struct('cdata',1,'colormap',cell([1 100]));

INPUT VIDEO

%Get the first frame as image
%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
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';
'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
%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=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 "IMAGE PROCESSING" page