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.


Contrast Stretching or Contrast Normalization an Image in MATLAB

Contrast stretching is a simple image enhancement technique that improves the contrast in an image by expanding the dynamic range of intensity values it contains. It can only apply a linear scaling function to the image pixel values.

Some Mathematical Modelling: (source: ed.ac.uk)

It is necessary to specify the upper and lower pixel value limits over which the image is to be normalized. Often these limits will just be the minimum and maximum pixel values that the image type concerned allows. For example for 8-bit graylevel images the lower & upper limits might be 0 and 255. Call the lower and the upper limits a and b respectively.
The simplest of stretching scans the image to find the lowest & highest pixel values in the image. Call these c and d. Then each pixel P is scaled using the following function:

Values below 0 are set to 0 and values about 255 are set to 255. 

 MATLAB implementation of the above concept of Contrast Stretching or Contrast Normalization on an image is:
X = imread('image.jpg'); %reading a grayscale image
title('Original Image')
a = min(X(:));
  %minimum pixel of image X
b = max(X(:));
%maximum pixel of image X
X= (X-a).*(255/(b-a));
%just using the formula above
title('Contrast Streached Image')

 Results of Contrast Stretching in MATLAB:

Contrast Stretching or Contrast Normalization an Image in MATLAB
The original Image: Not Contrast Stretched
Contrast Stretching or Contrast Normalization an Image in MATLAB
The Resultant Image: Contrast Stretched


Removing the leading & trailing zeros from a vector Using MATLAB

This is what we need while we are developing in communication systems. These zeros could be used just to gather synchronization information or they may act as a start or end delimiter of the transmitted frame.
Thus to get the useful data back we need to remove all these padded zeros from the received frame, which could be visualized by a vector in MATLAB.

Removing both leading & trailing zeros from a vector Using MATLAB
Suppose we are having a vector like:
x=[0 0 0 -4 -2 0 -7 -8 0 0 0]
Clearly it has been padded by three zeros at the end & the starting.
But How to remove that? And that too specifically by using our Favorite software MATLAB!

So to get the vector as 4 -2 0 -7 -8 what we are going to do is type the below statement,

y = x(find(x,1,'first'):find(x,1,'last'));

The above will remove all the leading & trailing zeros from the vector 'x' & store it in vector 'y'.
y =

    -4    -2     0    -7    -8

Removing only leading zeros from a vector Using MATLAB
What if we just have padded only the leading zeros & trailing zeros are actually important bits of information, so we just need to remove the leading zeros from the vector 'x'.
How we are going to do it is simply like typing the following command,

y=x(find(x, 1, 'first'):end)

& will result in,
y =

    -4    -2     0    -7    -8     0     0     0

Removing only trailing zeros from a vector Using MATLAB
At last what if we want only the trailing zeros to be removed from the vector. Another simple single line MATLAB code for that will be,

y=x(1:find(x, 1, 'last'))

& will result in
y =

     0     0     0    -4    -2     0    -7    -8

So, it was a simple stuff. More specifically it was a game of array indexing of/in MATLAB.
You just going some experience in MATLAB to get an idea of these small but beautiful tricks!


MATLAB Program for Converting a Text File to Stream of Bits

What is a text file specifically? A sequence of character.
And how these characters can be visualized in terms of a number? By their ASCII equivalent.
And the ASCII equivalent code can be converted to binary format to get bit stream.
Refer WikiPedia page of ASCII to get a detailed information about ASCII codes. Or search over the internet with the term "ASCII".

The following code will be a blend of MATLAB's data file handling (accessing & reading the file saved in disk), some processing & interconversion among data-types (typecasting to be more specific).

The resultant 1D array will be a continuous stream of  1s & 0s depending on the text in the text file.

MATLAB code for Converting a Text File to Stream of Bits

clear all 
close all
[f_name,path] = uigetfile({'*.txt';'*.*'},'Select the Text (.txt) file'); 

%above line will let you to browse for the file using a GUI window
path=[path f_name]; %building the path of the text file
A = importdata(path);
%import a text data as a structure
for i=2:size(A,1)
%preserving a line brake like this, it will be helpful while reconstruction
x=strcat(x, A(i));
%this loop is to concatenate all the characters in a continuous sequence
x=x{1}; %converting structure to a character array
c=dec2bin(x,8); %create a cell of x character & 8bit
%for a continoues bit stream
for i=2:size(c,1)
for i=1:size(y,2)
%z is a bitstream now
disp('The Resultant Bit Stream is:');


Sample Text & the output bit stream using above MATLAB code to convert a text file to bit stream.

Suppose the text file is with the name : test.txt contains the following text,
Andhra pradesh
Arunachal pradesh
Tamil Nadu
Andaman and nicobar
Madhya Pradesh

The resultant bit stream after the execution is: