Optical Character Recognition (OCR)

Optical Character recognition often abbreviated as OCR generally involves reading text from digital image of printed or handwritten paper by a computer.It is an efficient way of turning hard-copy text into data files that can be edited.

This post involves the explanation of OCR by using MATLAB.

STEPS :

  • Image of Printed or Handwritten Text :

First we have to take the image of a  printed or handwritten text. After getting the image we are now able to perform OCR process on it so that we can recognize the text from the image and edit it as our requirements. As an example lets consider we have the following Image.


Untitled


  • Reading an Image :

The image taken can now be read by MATLAB by a command i.e. ‘imread’ which reads image from graphics file.The read image can be shown by using the command ‘imshow’ which Displays image in Handle Graphics figure as shown below


Untitled


We can also enter the title of the read image for our own understanding by using the command ‘title’ as given below

title(‘INPUT IMAGE’s TITLE’)

  • Converting To Gray Scale :

The next step is to convert the read image into Gray Scale which can be done by using the command ‘rgb2gray’ in MATLAB. The command converts RGB image or colormap to grayscale.

if size(I,3)==3 %Loop determines the conversion of whole image

I=rgb2gray(I);

End

  • Convert to Binary Image :

Next we have to convert the gray scale image to binary image. It is usually carried out in MATLAB by the command ‘im2bw’ which converts the image into binary image.To use this command first we have to compute the global image threshold level which is generally computed in MATLAB by a command known as ‘graythresh’ .

threshold = graythresh(I);

I =~im2bw(I,threshold);

  • Removing Small Objects and Storing Text :

After converting to binary image we will remove all the small connected objects that have fewer pixels producing another image so that the process of OCR can recognize the text of the image easily.In MATLAB this step is carried out by a command ‘bwareaopen’.

I = bwareaopen(I,20);

Where I is the binary image and 20 is the pixels of the objects to be removed.

After this we have to store the data which is text in this case in  a matrix.It can be done as below

word=[ ];

where word is the matrix storing the text from the image.

  • Opening the saved text in Text file So that it can be Edited :

Now we need to open the saved matrix in a text editor file so that the recognized text can be shown and edited if required.To identify the characters we have to save the images of individual alphabets and digits in the same directory in which the main code is saved.After saving them we will make a code of calling them and matching them with the alphabets and digits in the read image and will save this code say ‘templates’ in the same directory.The following main code performs the code.

CODE:

clc, close all, clear all

I=imread(‘PIC.jpg’);
imshow(I);
title(‘INPUT IMAGE WITH NOISE’)
% Conversion to gray scale
if size(I,3)==3 %RGB image
I=rgb2gray(I);
end
% Conversion to Binary image
threshold = graythresh(I);
I =~im2bw(I,threshold);
% Removing  all object containing fewer than 20 pixels
I = bwareaopen(I,20);
%Storing matrix word from image
word=[ ];
re=I;
%to write the file in text editor
fid = fopen(‘text.txt’, ‘wt’);
% Loading templates
load templates
global templates

num_letters=size(templates,2);
while 1
%Function ‘lines’ separates lines in text
[fl re]=lines(re);
imgn=fl;
% Label and count connected components
[L Ne] = bwlabel(imgn);
for n=1:Ne
[r,c] = find(L==n);
% Extract letter
n1=imgn(min(r):max(r),min(c):max(c));
% Resizing letter as size of templates
img_r=imresize(n1,[42 24]);
% Calling function to convert image to text
letter=read_letter(img_r,num_letters);
% Letter concatenation
word=[word letter];
end
%fprintf(fid,’%s\n’,lower(word)); %Write ‘word’ in text file (lower)
fprintf(fid,’%s\n’,word);
word=[ ]; % Clearing the matrix

if isempty(re) %Loop will break when sentence will end
break
end
end
fclose(fid);
%Opening ‘text.txt’ file
winopen(‘text.txt’)


Untitled

The following files contain the code of  identifying the letters and digits from the saved images.

Letter templates code reads the saved images of letters and digits and create the templates accordingly.

Read_letters code will read the input letters and digits and will identify them from the saved images.

Lines code will help if there are more than one line of text.It will read every line and identify the text.

These files can be downloaded by clicking on the respective functions.

Letter templates code

Read_letters

Lines