This video shows the steps to implement OCR (Optical character recognition) for recognizing characters and words in an image in MATLAB App. This App is developed using MATLAB App Designer R2023a.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Complete source code and other details:
Main method:
methods (Access = private)
% Button pushed function: OCRReadtextfromImageButton
function OCRReadtextfromImageButtonPushed(app, event)
i = imread(app.Image.ImageSource);
ocr_result = ocr(i);
app.ImageTextTextArea.Value = ocr_result.Text;
end
end
Complete Class:
classdef OCR_App < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
OCRReadtextfromImageButton matlab.ui.control.Button
ImageTextTextArea matlab.ui.control.TextArea
ImageTextTextAreaLabel matlab.ui.control.Label
Image matlab.ui.control.Image
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: OCRReadtextfromImageButton
function OCRReadtextfromImageButtonPushed(app, event)
i = imread(app.Image.ImageSource);
ocr_result = ocr(i);
app.ImageTextTextArea.Value = ocr_result.Text;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Get the file path for locating images
pathToMLAPP = fileparts(mfilename('fullpath'));
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create Image
app.Image = uiimage(app.UIFigure);
app.Image.Position = [1 136 325 318];
app.Image.ImageSource = fullfile(pathToMLAPP, 'image1.png');
% Create ImageTextTextAreaLabel
app.ImageTextTextAreaLabel = uilabel(app.UIFigure);
app.ImageTextTextAreaLabel.HorizontalAlignment = 'right';
app.ImageTextTextAreaLabel.Position = [333 417 63 22];
app.ImageTextTextAreaLabel.Text = 'Image Text';
% Create ImageTextTextArea
app.ImageTextTextArea = uitextarea(app.UIFigure);
app.ImageTextTextArea.Position = [411 167 230 274];
% Create OCRReadtextfromImageButton
app.OCRReadtextfromImageButton = uibutton(app.UIFigure, 'push');
app.OCRReadtextfromImageButton.ButtonPushedFcn = createCallbackFcn(app, @OCRReadtextfromImageButtonPushed, true);
app.OCRReadtextfromImageButton.Position = [200 63 168 23];
app.OCRReadtextfromImageButton.Text = 'OCR - Read text from Image';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = OCR_App
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Screenshots:
Sample Image
Respective Text from OCR:
It was the best of
times, it was the worst
of times, it was the age
of wisdom, it was the
age of foolishness…
Excerpt:
The provided content pertains to a video tutorial explaining how to implement Optical Character Recognition (OCR) in an image using MATLAB App Designer R2023a. The video provides step-by-step instructions for creating an app capable of reading and decoding the text contained in an image. Key sections of the source code pertaining to the OCR function and the user interface are also presented. The tutorial encourages user interaction and assistance via a provided contact email address and website. Practical demonstration is given by running the OCR app to extract text from a sample image.