Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Optical Character Recognition Using MATLAB

J.Sashank Varma#1, G.V.N.S.Varma*2, K.Vikranth Reddy#3


#

Electronics and Communication Engineering Department, Vignans Institute of Information Technology

AbstractOptical character recognition (OCR) is used to convert the raw images into ASCII or other digital text, which reduces the file size and allows the text to be reformatted, searched, or processed by other applications. Nowadays OCR is used in many advanced fields like automatic number plate recognition, robotic vision, digitalization of books, computerize a record-keeping system in an office. Automatic number plate recognition is used by various police forces and as a method of electronic toll collection and cataloging the movements of traffic or individuals . It helps robotics by giving a way for the robot to get information from real world. The books that are digitalized can be searched for any term which helps people find books they required. In this paper we explain about how the OCR works using our algorithm in matlab. We will also explain our experimental results in this paper. Keywords OCR, digitalization, robotic vision, traffic surveillance.

I. INTRODUCTION Optical character recognition (OCR) is used to convert the raw images into ASCII or other digital text, which reduces the file size and allows the text to be reformatted, searched, or processed by other applications. The process of optical character recognition can be broadly divided into three stages. First dividing the image into lines, next dividing letters in each line, finally converting each letter image into text. II.
FLOW CHART

A. Algorithm for OCR To perform optical character recognition there are many steps .each one is explained below. 1) Input image: The input image can be either RGB or gray scale .if RGB is given as input convert the image into grey scale image. We convert image into grey scale for better processing. 2) Converting to binary image: The grey scale image has to be converted to binary image for the processing to take place. In binary image each cell value is either 0 or 1. 3) Separating first line from remaining lines: The first line separated from the remaining lines for processing each line individually. 4) Separate letters in each line: All the letters in first line are separate for comparison with database

5) Converting each letter image to text: All the letters images are compared with the database images and the most likely alphanumerical is entered as text. 6) Removing first line from processing image: As the first line is processed it is removed from the processing image. 7) Looping level: Here the program checks whether there are any remaining lines. If there are any remaining lines then the process goes to stage 3.Else to stage 8. 8) Saving the text file: Finally after all the letters are entered and there are no remaining lines, then the text file is saved. III. SOURCE CODE % OCR (Optical Character Recognition). % Read image imagen=imread('TEST_8.jpg'); % Convert to BW threshold = graythresh(imagen); imagen =~im2bw(imagen,threshold); %Storage matrix word from image word=[ ]; re=imagen; %Opens text.txt as file for write fid = fopen('text.txt', 'wt'); % Load templates load templates global templates % Compute the number of letters in template file num_letras=size(templates,2); while 1 %Fcn 'lines' separate 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)); % Resize letter (same size of template) img_r=imresize(n1,[42 24]); %-----------------------------------------------------------------% Call fcn to convert image to text letter=read_letter(img_r,num_letras); % Letter concatenation word=[word letter]; end fprintf(fid,'%s\n',word);%Write 'word' in text file (upper) % Clear 'word' variable word=[ ]; %*When the sentences finish, breaks the loop if isempty(re) %See variable 're' in Fcn 'lines' break end

end fclose(fid); clear all

%CREATE TEMPLATES %Letter A=imread('letters_numbers\A.bmp');B=imread('letters_num bers\B.bmp'); C=imread('letters_numbers\C.bmp');D=imread('letters_num bers\D.bmp'); E=imread('letters_numbers\E.bmp');F=imread('letters_num bers\F.bmp'); G=imread('letters_numbers\G.bmp');H=imread('letters_nu mbers\H.bmp'); I=imread('letters_numbers\I.bmp');J=imread('letters_numbe rs\J.bmp'); K=imread('letters_numbers\K.bmp');L=imread('letters_num bers\L.bmp'); M=imread('letters_numbers\M.bmp');N=imread('letters_nu mbers\N.bmp'); O=imread('letters_numbers\O.bmp');P=imread('letters_num bers\P.bmp'); Q=imread('letters_numbers\Q.bmp');R=imread('letters_num bers\R.bmp'); S=imread('letters_numbers\S.bmp');T=imread('letters_num bers\T.bmp'); U=imread('letters_numbers\U.bmp');V=imread('letters_nu mbers\V.bmp'); W=imread('letters_numbers\W.bmp');X=imread('letters_nu mbers\X.bmp'); Y=imread('letters_numbers\Y.bmp');Z=imread('letters_num bers\Z.bmp'); %Number one=imread('letters_numbers\1.bmp'); two=imread('letters_numbers\2.bmp'); three=imread('letters_numbers\3.bmp');four=imread('letters _numbers\4.bmp'); five=imread('letters_numbers\5.bmp'); six=imread('letters_numbers\6.bmp'); seven=imread('letters_numbers\7.bmp');eight=imread('letter s_numbers\8.bmp'); nine=imread('letters_numbers\9.bmp'); zero=imread('letters_numbers\0.bmp'); %*-*-*-*-*-*-*-*-*-*-*letter=[A B C D E F G H I J K L M... N O P Q R S T U V W X Y Z]; number=[one two three four five... six seven eight nine zero]; character=[letter number]; templates=mat2cell(character,42,[24 24 24 24 24 24 24 ... 24 24 24 24 24 24 24 ... 24 24 24 24 24 24 24 ... 24 24 24 24 24 24 24 ... 24 24 24 24 24 24 24 24]); save ('templates','templates')

clear all

function [fl re]=lines(im_texto) im_texto=clip(im_texto); z=im_texto imwrite(z,'binary clip','jpg'); num_filas=size(im_texto,1); for s=1:num_filas if sum(im_texto(s,:))==0 nm=im_texto(1:s-1, :); % First line matrix rm=im_texto(s:end, :);% Remain line matrix fl = clip(nm); re=clip(rm); break else fl=im_texto;%Only one line. re=[ ]; end end function img_out=clip(img_in) [f c]=find(img_in); img_out=img_in(min(f):max(f),min(c):max(c));%Crops image function letter=read_letter(imagn,num_letras) % Computes the correlation between template and input image % and its output is a string containing the letter. % Size of 'imagn' must be 42 x 24 pixels global templates comp=[ ]; for n=1:num_letras sem=corr2(templates{1,n},imagn); comp=[comp sem]; end vd=find(comp==max(comp)); %*-*-*-*-*-*-*-*-*-*-*-*-*if vd==1 letter='A'; elseif vd==2 letter='B'; elseif vd==3 letter='C'; elseif vd==4 letter='D'; elseif vd==5 letter='E'; elseif vd==6 letter='F'; elseif vd==7 letter='G'; elseif vd==8 letter='H'; elseif vd==9

letter='I'; elseif vd==10 letter='J'; elseif vd==11 letter='K'; elseif vd==12 letter='L'; elseif vd==13 letter='M'; elseif vd==14 letter='N'; elseif vd==15 letter='O'; elseif vd==16 letter='P'; elseif vd==17 letter='Q'; elseif vd==18 letter='R'; elseif vd==19 letter='S'; elseif vd==20 letter='T'; elseif vd==21 letter='U'; elseif vd==22 letter='V'; elseif vd==23 letter='W'; elseif vd==24 letter='X'; elseif vd==25 letter='Y'; elseif vd==26 letter='Z'; %*-*-*-*-* elseif vd==27 letter='1'; elseif vd==28 letter='2'; elseif vd==29 letter='3'; elseif vd==30 letter='4'; elseif vd==31 letter='5'; elseif vd==32 letter='6'; elseif vd==33 letter='7'; elseif vd==34 letter='8'; elseif vd==35 letter='9'; else letter='0'; end

IV. EXPERIMENTAL RESULTS We have taken a test image and applied our algorithm to it in matlab. The various stages in the the processing are illustrated below. We took an image as shown in Fig. 2

Fig. 5 Now by comparing each letter with our database we can determine which letter it is. Next the remaining lines are as shown in Fig. 6

Fig. 1 input image

Next we converted it to binary image as shown in Fig. 3 Fig. 6 Now again divide the lines as shown in Fig. 7

\
Fig. 3 input as binary image

Fig. 7 Now divide the first line into separate letters as shown in Fig. 8

Now we have separated the first line from remaining lines and the resulting images are shown in Fig. 4

Fig. 8 The remaining line is shown in Fig. 9

The remaining line is divided into separate letters as shown in Fig. 10


Fig. 4

Now the first line is divided into separate letters as shown in Fig. 5

Fig. 10

Now by comparing the separate letters with database we can find out the letters. The result is shown in a text file as shown in Fig. 11

Fig. 11

IV. CONCLUSIONS In this paper we have discussed the algorithm and implementation of optical character recognition in matlab. By showing the results results we have shown the working of a OCR. Till now the algorithm is developed to use with only capital letters. But by increasing the database we can also identify small letters and special characters. By further developing the algorithm we can also extract tables, lines and many other things from the image. We also create font specific optical character recognition and by further developing the algorithm we can create hand writing recognition system and hand writer recognition systems. We finally conclude that OCR has a lot of advantages and the working is illustrated above. REFERENCES
[1] [2] [3] http://en.wikipedia.org/wiki/Optical_character_recognition http://www.mathworks.com/ http://en.wikipedia.org/wiki/Book_scanning

You might also like