Training and implementation of OCR algorithm to recognize letters and numbers

OCR, short for Optical Character Reader, is a technology that uses image processing to identify letters and numbers. The process of letter and number identification through image processing is based on the OCR algorithm and involves two main stages: training and recognition. In the training stage, the program learns the target letters, meaning we need to identify our desired letters and numbers for the program. In the recognition stage, we can read them. This article explains the step-by-step image processing for Latin letter identification with the implementation of an example using HALCON software.

To register for the “Machine Vision Training Course with HALCON Software,” click here.

Implementation of Letter and Number Identification (OCR) Using HALCON Image Processing Software

HALCON software is the best image processing software for implementing many image processing projects. This software is developed by the German company MVtec, and with just a few lines of code, you can implement letter and number (OCR) identification. Additionally, in the ready-made examples of image processing with HALCON software, several examples of OCR algorithm tutorials and implementations are provided. For more examples of letter and number identification, you can download and install HALCON software from here and here, respectively, and search for your desired examples (read the tutorial on searching for specific examples in the article “1000 Image Processing Projects with Ready-to-Use Source Codes“).

As mentioned, letter identification is based on two main stages, each with several sub-sections. The image below shows these stages along with the program written in HALCON software. The training stage is performed only once, and the learned results are saved for use in word identification. In the following article, each stage and its subsections will be explained step by step.

OCR Tutorial with HALCON Software

First Stage: Training Letters and Numbers with OCR

The training stage involves taking several images to teach the program. In this stage, we teach the program our letters by providing some images. Besides, HALCON software has a set of libraries that have already been trained, allowing us to perform image processing for letter and number identification without going through the training stage. However, since training usually improves the output results, in our example implementation, we will focus on training the letters ourselves.

The training stage consists of four parts:

1- Image Capture for OCR Implementation

The first step in processing images for letter identification is to obtain images. In other words, for training, we need some images of our letters and numbers, where all our desired letters and numbers are present at least once. In the example implementation, a regular USB webcam is used with an image size of 480*640. It is mounted on a stand.

2- Letter Identification and Numbering

To process images for letter identification, in this stage, we need to find the letters. Here, we use image processing to find the letters. In this example, we use the get_regions function to find the letters. This function takes the input image and outputs the regions of words in the image (SortedRegions).

get_regions(Image, SortedRegions)

Image: Input image

SortedRegions: Obtained letter regions

In the image below, you can see a view of the input image (Image) and the obtained regions from the letters (SortedRegions). Note that, at this stage, we have only extracted the regions, and the program does not yet know which region belongs to which letter.

OCR ProgrammingReading Letters and Numbers

3- Classification of Letters and Numbers with OCR Algorithm

The third stage of the training process in image processing for letter identification is to determine the class for each letter. In this stage, we assign a class to each found letter. In other words, in the previous stage, we found the region of each image, and now we assign region R to letter R, region S to letter S, etc. Therefore, each letter is assigned a class, and regions like I that have been repeated several times belong to a single class.

Classes:=[‘V’,’I’,’S’,’O’,’N’,’Y’,’T’,’E’,’M’,’.’,’R’]

Classes: Defined classes for regions

To specify the classes for letters, the Define_Classes function is used. This function takes the image, the regions (obtained from the get_regions function), and the classes (Classes) as input and assigns variables to the classes, saving the result for the training stage.

Define_Classes(Image, SortedRegions, Classes)

Image: Input image

SortedRegions: Regions obtained from the get_regions function

Classes: Defined classes for regions

کلاس بندی حروف با ocr

4- Training Letters and Numbers with OCR

After defining the classes, it’s time to move on to training. In this stage, features of all classes are extracted so that we can process images for letter identification. This is easily done in HALCON software.

The Train function is prepared for this purpose, and its input is the regions obtained in the previous stages. Additionally, the file saved in the previous stage (stage three) is called in this function. The output of the Train function is a variable called OCRClass, where the training information is stored. OCRClass is the main output file obtained from training, and it will be used to read letters in the next stage. In this file, features related to each region are extracted and stored.

Train(SortedRegions, OCRClass)

SortedRegions: Regions obtained from the get_regions function

3- Classification of Letters and Numbers with OCR Algorithm (Continued)

After obtaining the regions using the get_regions function, the next step is to classify the letters and numbers. This is done using the Classify function. The Classify function takes the sorted regions (obtained from get_regions), the OCRClass information obtained during the training stage, and a variable to store the recognized letters (Words) as input.

Classify(SortedRegions, OCRClass, Words)

SortedRegions: Regions obtained from the get_regions function

OCRClass: Information obtained from the training stage

Words: Recognized letters. The variable Words in the image below will be equal to Words=['V','I','S','I','O','N','S','Y','S','T','E','M','S','.','I','R'], as printed on the image.

Implementation and Training of OCR

In this article, an example written in HALCON software for image processing to identify Latin letters has been explained. The functions described in this program in various stages are the result of combining several lines of HALCON code. HALCON software has a comprehensive library of codes that can identify various types of letters and numbers (printed, engraved, embossed, etc.).

For your projects, you can consult with our experts. Get consultation.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

ارتباط سریع با ما