Text Overlay on an Image, And Save using MATLAB : Add text over an Image & Save Using MATLAB

To the people who love MATLAB, especially for accomplishing the task of image processing it may happen that editing or enhancing the image is felt more comfortable in MATLAB as compared to  photo editing software like Picasa, Paint, or even Photoshop!

This time we just want to overlay some text using MATLAB over an image. In MATLAB its a 4 line command work (or you may even further reduce it).

So the MATLAB code for Text Overlay on an Image, And Save is:

imshow(imread('xyz.jpg')); %reading an image

text(20, 300, 'Divilabs.com', 'FontName', 'Comic Sans MS', 'FontSize', 20, . . .
 'BackgroundColor', [.6, .6, .6], 'VerticalAlignment', 'Bottom'); %all the text option to try out with

a = getframe(gcf); %getting a screen shot for the currently opened plot window

imwrite(a.cdata, 'resultant.jpg'); %saving the text overlayed image file

Explanation of the above MATLAB code:
Here in the above code we are reading an image & loading it on an image plot window. Then the 'text' function will write the text over the plotted image in that window itself.
The first 2 parameters 20 & 300 are just to specify the location of overlayed text on the image. the next 4 parameters 'FontName', 'Comic Sans MS', 'FontSize', 20 are obviously suggesting the font name & size to the text function.
What the parameter set 'BackgroundColor', [.6, .6, .6] will do is, they will specify the background color on which the text will be written in the image. 'VerticalAlignment', 'Bottom' will write the text on the bottom of that text box.

a = getframe(gcf) : this will click a screen shot of the current image plot window with the text overlayed.

imwrite(a.cdata, 'resultant.jpg'): this will save the image file with the text overlayed with the name resultant.jpg

Resultant Images with text overlayed on tweaking with different Argument of text function

With the command set: 
text(20, 300, 'Divilabs.com', 'FontName', 'Comic Sans MS', 'FontSize', 20, ...
 'BackgroundColor', [.6, .6, .6], 'VerticalAlignment', 'Bottom'); 

We got:
Text Overlay on an Image, And Save using MATLAB
Text Overlay on an Image, And Save using MATLAB : Add text over an Image & Save Using MATLAB
With the command set: 
text(50, 400, 'Divilabs.com', 'FontName', 'Comic Sans MS', 'FontSize', 25, ...
 'BackgroundColor', [.8, .6, .8], 'VerticalAlignment', 'Center'); 

We got: 
Text Overlay on an Image, And Save using MATLAB : Add text over an Image & Save Using MATLAB
Text Overlay on an Image, And Save using MATLAB : Add text over an Image & Save Using MATLAB
 So, at last I would suggest that just play around by tweaking the above parameters & get the desired result. If you got some interesting result please do add that in the follow up comments below or just mail be so that I could add up in the article.


0 comments: