The Code in MATLAB for OCR


MATLAB is an acronym for “matrix laboratory” and refers to a high-level programming language and programming environment developed by technical computer software company MathWorks. OCR is an abbreviation for “optical character recognition” and refers to the process of reading data in printed form and identifying optical patterns that correspond to letters, numbers and other characters.


  • All of the steps involved in OCR -- segmentation, feature extraction and classification -- can be implemented using MATLAB. Segmentation, which involves checking the connectivity of shapes, and labeling and isolating them, is the most important stage because it allows the OCR program to extract features from each individual character. Segmentation of handwritten text is particularly difficult because handwritten characters tend to be connected to each other. Feature extraction extracts the most useful features, in terms of recognition, from each character and classification assigns those features to character classes.

Image Capture

  • According to a paper presented at a seminar given by LASIN Laboratory of Synergetics at the University of Ljubljana, OCR can be implemented in MATLAB using the Neural Network Toolbox and Image Processing Toolbox add-ons. The first stage involves reading the image into the MATLAB workspace as a bitmap file. This is a type of graphic file in which each picture element, or pixel, corresponds to one or more binary digit, or bit, in memory. The following code is used for this task.

    I = imread('training.bmp');

Image Conversion

  • The next stage is to convert the colored image, which is stored as separate red, green and blue overlays, into a grayscale image, which consists of discrete shades of gray. The following MATLAB code converts the RGB image into a grayscale image.

    Igray = rgb2gray(I);


  • Subsequently, a technique known as “threshing” is used to convert the grayscale image into a binary image. The cell of MATLAB codes required to convert the grayscale image into a binary image looks something like this:

    Ibw = im2bw(Igray,graythresh(Igray));

Edge Detection

  • Once the binary image is created, individual characters are cropped into subimages. These provide the raw data for the feature-extraction routine. The subimages must be cropped sharply at the border of each character, if they are to be of standard size, so detecting the edge of each character is important. The edge of an image can be detected using the following MATLAB code.

    Iedge = edge(uint8(Ibw));


  • All objects in an image -- and the properties of each object -- can be determined using the following MATLAB code.

    [Ilabel num] = bwlabel(Ifill);
    Iprops = regionprops(Ilabel);
    Ibox = [Iprops.BoundingBox];
    Ibox = reshape(Ibox,[4 50]);

Related Searches


Promoted By Zergnet


You May Also Like

  • What is OCR?

    Optical Character Recognition (OCR) is the ability to convert a printed paper document into a document that can be edited on a...

  • How to Check If a Figure Exists in MATLAB

    MATLAB, produced by Mathworks, is a technical software package for data collection, analysis and display. MATLAB provides a wide variety of pre-made...

  • How to Use Tesseract OCR in Java

    Tesseract optical character recognition libraries offer developers a method to scan documents and text to an image. You use the Tesseract libraries...

  • How to Read UPC Barcodes

    Have you ever wondered what all those numbers and lines used to scan merchandise actually mean? Barcodes can be a bit frustrating...

  • How to Convert a JPEG to WordPad Text

    The process for converting an image file, such as a JPEG, into a WordPad rich-text format (RTF) or TXT text file is...

  • How to Convert 3D Data to 2D Using MATLAB

    MATLAB is a technical software environment that allows you to import, manipulate and analyze large amounts of data. In many cases, it...

  • How to Specify Colors in MATLAB

    Matrix Laboratory, known as MATLAB, is a powerful computation software tool commonly used in academic research environments. Plots of data you produce...

  • How to Turn Input Into a Vector in MATLAB

    MATLAB is capable of performing powerful computations but also offers many of the conveniences common to high-level programming languages, like receiving and...

  • What Is Optical Character Recognition Software?

    The ability to scan a document into machine-readable form is a matchless time saver. Rather than retyping the document, and possibly inserting...

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!