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

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

0 comments:

Enjoyed Reading? Share Your Views

Previous Post Next Post Home
Google ping Hypersmash.com