File Exchange

image thumbnail

Optical Character Recognition (OCR)

version (95.6 KB) by Diego Barragán
The aim of OCR is to classify optical patterns corresponding to alphanumeric or other characters.


Updated 08 Aug 2014

View Version History

View License

Editor's Note: Popular File 2011 2012 2013

The aim of Optical Character Recognition (OCR) is to classify optical patterns (often contained in a digital image) corresponding to alphanumeric or other characters. The process of OCR involves several steps including segmentation, feature extraction, and classification. This program use Image Processing Toolbox to get it.
For more information, visit: (spanish).

Cite As

Diego Barragán (2021). Optical Character Recognition (OCR) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (138)

Khushboo Agarwal

hussain albanna

Angel Luis

Ayonija Mathur

Mather Qurban

I got an error in read_letter : "Not enough input arguments. Error in read_letter (line 10) for n=1:num_letras"
Can anyone help me to solve it ?

Ankit Singhal

Can someone explain me how to use this in MATLAB??


Sir ! Only images given by you are working. I have tried other images but I am getting incorrect results !please help me with this !

Praveen Vadagave

Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in OCR (line 58)
fprintf(fid,'%s\n',word);%Write 'word' in text file (upper)

pls can anyone take me through how to rectify this error

Yisah Abdulrahaman

great work, sir i tried running the OCR.m file and it giving me this error

Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in OCR (line 58)
fprintf(fid,'%s\n',word);%Write 'word' in text file (upper)

pls can anyone take me through how to rectify this error

Tehsinudin Wazir

Excellent work sir

ayesha abbassi

does 'load templates' command works with 2014a? or not..?as in my case its giving error.


Engineer Zark

bhumika patel

how to find accuracy of character?

Ataliba Miguel

Dear Sir, honestly thus far I am struggling with the template that has the .bmp files. I mean I am looking ways to have the templates downloaded or some means to create it! Any help or hint I'd be very grateful.

Samia Rashid

hello sir, i trued to make use this code in my project where i need to recognize characters after taking images through a car. But this code doesn't show exact output when i use other images immediately taken from camera. it seems it can not recognize characters when the image contains other stuffs along with the characters. can u please help me in this case ?

Mr Smart

Monirul Islam

Is there any algorithm that has been used in this code?

anggi nego




moahaimen talib

OCR in matlab use what or algorithms does it use neural network or dNN CNN please provide with an answer

Huda Dr

balram naik

for what font and letter size do this code run?


i am new to this....
can somebody please tell me how to download this file?

David J. Mack




Firstly,I convey my gratitude to you!secondly,i want to say that the ocr can not accomplish the recognition of plate number.SO,can you help me?

Sumita Das

When i try to run this exact code, it shows error.

Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in OCR (line 58)
fprintf(fid,'%s\n',word);%Write 'word' in text file (upper)

What should I do?


Thank you very much, and Can I find any documents or papers about this method.

Pornsuda Jareansuk


Duc Hoang

Olek San

When can i get or generate letters for Cyrillic?


Really useful, but I think that the function "ocr.m" in Matlab is much simple and powerful.

Haijian Zhang

Jacob Berube

Ok, so how do I run this. I need to copy and paste something, but It wont let me do it on a certain program I use, so I went looking for something and I found this. It would be decent if you could add a .exe file!

Arya Arya

Good job. How can I modify the code for other languages?

Nivetha m

??? Undefined function or method
'detectMSERFeatures' for input arguments of type

Error in ==> OCRMETHOD at 8
[mserRegions, mserConnComp] = detectMSERFeatures(I,

sung youl kim

Vishal Gupta

Nandhini R

i am doing a project on extracting OCR from video,please guide me..


I miss a documentation.
Using a function would be more useful than a script, which disables all warnings (warnings are important to be considered!) and which kills all of my results in the Matlab workspace by a "clc, clear all, close all".

please tell me how to create other fonts for templates.when I have changed my templates to my own it is not working properly
please guide me.
Also what is the font and process used to create template in the code.
Pls answer my question

Mohamed Meska


Excellent contribution , thank you very much . Now how I can create other templates for use with sensitive or other text font.

JIBU mathew

i am doing project on english handwritten recognition .please guide me ti this
thanks a lot

anh hoang

how to download this file

amir abdi

very god link

amir abdi

amir abdi


Ghaffar gh

Ghaffar gh


Very simply perfect


alex yang

Very simple but powerful idea.

student s

from where can i download this code. I am under character recognition. Please help me for getting the code

Lutful Kabir


please from where i get source cod?


Please let me know, how can use this OCR with SVM. Thank a lot!

hussein Ali

Good working





Anyone can help, How to run this code ?

Chandra Shekhar

Nice work.


sir it is not able to detect the text from video frame. please help


excuse me for all people,i'm newbie about this..i am a student of computer science departmen and i have an interest of making OCR buat read the character or text from hand writing. for example, i write something on the paper and convert it to it possible ?
thanks before



Hola diego, tengo una pregunta estoy tratando de hacer un algoritmo que me reconozca las letras usando redes neuronales con una interfaz grafica, pero el manuscrito de prueba lo tengo que cargar, queria saber si habia alguna manera de crear un recuadro sobre el GUI que sea como "paint" y se pueda dibujar la letra directamente ahi con el mouse



Nice code. I used it to generate the templates for 187 fonts in english. I dont know where to upload it for everyone to use..


nice code...

Imran Kanjoo

Imran Kanjoo

Sir its a nice code. i am getting little problem with understanding of this code. can you let me know how did you create the template.mat file from the letters. my is the same i also want to extend this code for lower case letters and local languages. one other thing is that when i replace
sem=corr2(templates{1,n},imagn); in read_letter.m to directly read the letters images and compare or find the correlation. but it does not gives the accurate results. if you read please must reply me.


Can anybody tell me that how to understand this code for character recognition?
or what is the way
please help.
Its Urgent!!

Wyfi Mcfly

maximum rather

Wyfi Mcfly

nicely done. for more robust results i found preforming a skeletal reduction of the input image text (when properly prepared), and making a template of skeletal comparisons for the correlation coefficient calculation can get around most font issues.
The next thing is properly aligning the input letter to that of the template. This can be accomplished in many ways, or you can simply preform an image rotation of all 360 degrees at take the minimum confidence coefficient of that process.


Hello Diego...
Cheers for ur code..
I want to know that how to recognize special characters like +,/,| etc..??
Also your code is not applicable for lowercase letters so please give me some information that how to tackle this.


Pon Selvalingam

Hello sir
It is perfect code, really thanks
Please send this project pdf

sidharth jhawar

hello sir,the code works well for majority of the images..perhaps doesnt work for joined text.sir,if u cud plz send a text file describing the objective if all the four .m files??

pankaj Durve

i use matlab 6.5,wheh i run oCR project,then templates loading error is come.why error is come?how to error is removed?

kolayuyelik kolay

Quite good!


plz anyone can give me the logic behind this program....on wht basis feature extraction has been done...nd hw Image proceesing tool box is used for simulatin...plz help me out asap....

Mr Smart

Fabio Bento

Quite good!


Hello! Did any one succeeded to add some new character templates? I am trying to add the OCR-B font templates but I don't know how to do it. I tried in photoshop but it didn't work. Does anyone know if is there a special program to do it?


It helps me so much!

Lamis zaki

Really good job , you helped me out :) thnx


It is perfect code, really thanks

King Lee

Hi, im planning to improve your code to be able to recognize about the small cases alphabet also,but i cannot access the thumb.db files in the letter number's folder...
Can you tell me it need what to open?


sir, nice your OCR project...i also using your code for my character recognition for my project...but i code some error because of edge of image...can sir look my project and suggest a solution...i need ur help sir..tq

here is my project

vipin vc

good program
Dear Bogdan

Suppose that we have an image with two lines of text. With "clip", only obtain one line and leave the rest.

Khaing Thaw

Hi Hannah,
these images of letters are in the letters_numbers folder. there are 36 image file, if u also want to recognize lower case letters u can add lower letter image files into that folder. and need to edit and add some coding in .m file in OCR folder.
clip is not built-in function, it is a sub function use in line.m function to crop the lines, u can see it lower part of line.m function.
good luck,

Claus Becker

Hannah Whitby

Hi Diego and everyone,
Good program, but I have a few questions:
Where/how did you make these images of the letters that are in the template? I need to be able to identify lower case letters as well as upper case and have tried just using microsoft paint but although they are .bmp the appear the wrong format. Any ideas anyone?
Also - what is 'clip'? my matlab doesn't seem to support it but when i changed it to imcrop it stalls.

Khaing Thaw

Hi Diego, Nice ur coding, complete in a few lines, functions, templates ,... except clip function :P

Hi Ashley, to find the spaces calculate the spacing of components in image Labeled,. it is my suggestion, how Diego?

Hi Priyanka, add some coding in create_templates.m and image files (BMP, 42x24) for lower characters,.

Thanks Diego,.

Ashley Figer

Dear sir,
how do we detect blank spaces between the characters?
and print them in the output

pupu QQ

not very good for car plate in China.

mrudula sekhar

hi sir, ur work is really good.
i am too trying to implement the same as a part of my academics, so like to know more about the subject. can u please help me ?
i did like to know about the different steps involved in character recognition. can u provide me with some pdf documents which i can depend to complete my project.


Problem solved. The problem was the performance of my PC.

Thanks again for the program



i use Matlab 7.9.0 (R2009b) and i received an error "Segmentation violation detected" while i tried to run the OCR program. i'm beginner in matlab so can anyone help me please?

PS: thank you Diego for the OCR program

uet taxila


Hi Mr. Diego,
at the moment i'm developing a computer vision system to detect and recognize license plates.I experienced your project and works very well but i have a question. What method are you using to do the recognition? SVM's? Neural Networks?

kind regards

Priyanka M

Hi Diego..
dis is priyanka,from India
It was a very nice job.Can u plz help me with dis project..???
Plz tell me how to recognize lower case letters..?? and also plz help me with how to obtain the bitmap images of the letters u place in letters_numbers folder so dat I can try for the regional languages...
Plz plz plz do reply me....

Diego Barragán

Dear Bogdan

Suppose that we have an image with two lines of text. With "clip", only obtain one line and leave the rest.

Kind regards.


Thank you for answare, but I have a nother question : in lines.m file exist next line:
What exactly make cilp in this expresion?

Diego Barragán

Hello Bogdan

I use IMCROP instead CLIP.

Kind regards.


I hava a question for Mr Diego Barragán.
In the Zip file I cannt find the function clip, but On this page link, exist!!!! Where is integrate this function?

vaibhav sharma

Please can anyone tell me how to implement this code on fpga??
Its urgent....mail me at

thank you very much


dear sir,
it was a very nice job. however for certain pics it cant read the image correctly. do u have any idea on how to add the image filtering codes to make this program read the image more accurate?
i really need ur help on this matter.



Sid H, come on!

If you can do better, we are waiting!

Sid H

Amateur work, both code and concepts. Not robust. Cant support fonts, noise or files different from what the up-loader provides. But great as a concepts demo for undergrad-and-below classes.

chaogui xia

thank you


Very nice job, I have 2 questions the code ignores white space which should not and also it does not detect lowercase letters.
how should we detect white spaces between words?

Cong Zhou

good job!

Cong Zhou

karthik sarma

this program is not working for other image files. i created some image files using paint in ariel and times new roman font. the program failed to give correct result


Anybody can help me? My FYP is about converting an image to text. Image may be frim newpaper (anywhere) and should be converted into text and saved into Doc. or pdf but it must retain the oiginal format.

Thank u,


It works great.But for images with dots or small patches the result is ambiguous.If the code can eliminate this problem,(not recognizing non characters) it will be a good algorithm for character recognition.

athi rayan

hello sir please give me as a compltet report in ocr

ved vrath


firas aakroum


manish arora

these files really works..
result not being 100%..

minnal sagi



hi author
i am using matlab 6.5 version. Can i work your project my matlab?

eklil alwrd

Paul Paul

raghav khanna

can u tell me wat feature algorithm r u using??

Tran Dinh Vuong

Dear Sir, I'm doing handwritten english character recognition.Please guide me to progress this project. thanks lot.

Pradeep Mapitigama

Dear Sir,
I'm doing handwritten english character recognition.Please guide me to progress this project.I'm doing this for fullfill the computer science.
thanks lot.

Vihang Patil

Nice work. Tried creating my own images of numbers and names alike and found sometimes some letters being wrongly identified, especially I, 1, O,0,Q
Other than that nice work!

John Smith

Good application

Diego Barragán

Hi, this is Diego Barragán, from Ecuador.

I appreciate your comments about OCR.

The error perhaps is due the version of MATLAB. I use version (R14) Service Pack 3.

And other restriction of OCR is the size of letter. The size must be bigger or equal to 42 x 24 pixels.


Austin K

this is a very good work.
a very good logic and simple to understand .
yet there are lot of sectors which can improve.
and i dont know why it happens is that there is a error while loading the templates.
always shows that the file was corrupted.
any way best wishes and once again good work.

MATLAB Release Compatibility
Created with R14SP3
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!