The Code in MATLAB for OCRby David Dunning
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.
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'); imshow(I)
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); imshow(Igray)
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)); imshow(Ibw)
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)); imshow(Iedge)
All objects in an image -- and the properties of each object -- can be determined using the following MATLAB code. [Ilabel num] = bwlabel(Ifill); disp(num); Iprops = regionprops(Ilabel); Ibox = [Iprops.BoundingBox]; Ibox = reshape(Ibox,[4 50]); imshow(I)