This repository provides an Optical Character Recognition (OCR) system implemented in MATLAB. The system is designed to extract text from images and perform character recognition using contour-based features and the k-Nearest Neighbors (kNN) algorithm. It offers a comprehensive set of features and functions for preprocessing images, extracting contour descriptors, training character classifiers, and evaluating the performance of the OCR system.
To use the OCR system, follow these steps:
-
Clone the repository:
git clone https://github.com/ggkogkou/optical-character-recognition-matlab.git
-
Launch MATLAB and navigate to the project directory:
cd optical-character-recognition-matlab
-
Run the
evaluateCharacterRecognition
function to evaluate the performance of the OCR system using the provided test images.
To use the OCR system, follow these steps:
-
Prepare the training dataset: Collect a dataset of images containing printed characters. Annotate the characters in the images and split the dataset into training and test sets.
-
Train the classifiers: Use the training dataset to train the kNN classifiers. Extract features from the characters and use them to train the classifiers. Save the trained classifiers in a
.mat
file. -
Run the OCR system: Use the
readText
function to perform OCR on an input image. Provide the path to the image as an argument to the function. The function will load the trained classifiers, process the image, and return the extracted text.
lines = readText('input_image.png');
- Evaluate the results: Compare the extracted text with the ground truth to evaluate the accuracy of the OCR system. Use appropriate evaluation metrics such as character-level accuracy or word-level accuracy.
% Load the input image
img = imread('input_image.png');
% Perform OCR to extract the text
lines = readText(img);
% Display the extracted text
for i = 1:numel(lines)
disp(lines{i});
end
The OCR system provides several key functionalities:
The system can extract individual lines of text from an input image.
It segments the characters within each text line to isolate them for further processing.
The system extracts relevant features from the segmented characters to represent them numerically.
It trains k-Nearest Neighbors (kNN) classifiers using the extracted features to recognize characters.
Based on the trained classifiers, the system reconstructs the text by predicting the characters in each line.
The reconstructed text is stored as a cell array and can be written to a text file.
To run the OCR system, ensure that you have the following prerequisites:
- MATLAB
- Image Processing Toolbox: This toolbox provides a collection of functions and algorithms for image preprocessing, segmentation, and manipulation.
- Statistics and Machine Learning Toolbox: This toolbox contains the necessary functions and algorithms for machine learning, including kNN classification.
See CONTRIBUTING
This project is licensed under the GPL-3.0 License.
Please feel free to explore the repository further and make any necessary modifications to meet your specific needs.