A brief introduction to handwriting number recognition technology

Handwritten number recognition is a branch of the discipline of image recognition. It is one of the important applications in the field of image processing and pattern recognition, and has strong versatility. Because of the large randomness of handwritten numbers, such as stroke thickness, font size, tilt Angle and other factors may directly affect the accuracy of character recognition, so handwritten number recognition is a very challenging topic. In the past decades, many identification methods have been proposed and some achievements have been made. Handwritten number recognition is very practical and has a broad application prospect in large-scale data statistics such as routine annual inspection, population census, finance, taxation, mail sorting and other application fields.” This case describes the recognition process of handwritten Arabic digits in the image. It briefly introduces and analyzes the method based on statistics of handwritten numbers recognition, and carries out experiments by developing a small handwritten numbers recognition system. Handwritten digital recognition system needs to achieve handwritten digital image reading function, feature extraction function, the establishment of digital template feature database function and recognition function.

2 theoretical Basis 2.1 Algorithm ProcessFirstly, the handwritten digital images were read for image normalization, unified size, default 24×24 image block, and binarization was carried out by OSTU algorithm. Secondly, morphological operations such as image thinning were performed on the binarization image, and features were extracted according to the requirements of the algorithm. Finally, the template matrix is loaded and compared, and the Euclidean distance measure is used to get the recognition result. The algorithm flow chart is shown in the figure.2.2 Feature ExtractionAccording to the structure feature of handwritten digital image, the number of intersection points of the end points and the line with the specified direction is calculated as the feature vector. The main steps are as follows.(1) Vertical intersection pointsThree vertical lines are generated at the column width of the refined handwritten digital image: 5/12, 1/2, 7/12 respectively, and the number of intersection points between the three vertical lines and the digital strokes is extracted and stored.(2) Horizontal intersectionThree horizontal lines were generated at the column width of the refined handwritten digital image, and the number of intersection points between the 1/3, 1/2 and 2/3 horizontal lines and the digital strokes were extracted and stored.(3) Diagonal intersectionTwo diagonal lines were taken from the refined handwritten digital image, and the number of intersection points between the two diagonal lines and the digital strokes was extracted and stored. Because the above steps are applied to the refined digital image, the strokes are simple and the features are stable, so the basic intersection points and the structure endpoints extracted can reflect the essential features of the numbers, and the numeric characters can be quickly and effectively recognized, and a good recognition rate can be achieved. Among them, the algorithm for extracting the end features of stroke structure is as follows. (1) Target positioning The refined handwritten digital image was scanned sequentially from top to bottom in the row and from left to right in the column, and the black pixel P was selected as the stylus target. (2) Neighborhood statistics calculate the sum N of 8 neighborhoods of black pixel P. If N=1, then pixel P is the endpoint, and the endpoint counter is increased by 1. Otherwise discard the point. (3) Traversing the image traversing the entire image, repeat the operation process of target positioning and neighborhood statistics, and extract the endpoint features. According to the above method of feature extraction of intersection points and endpoints of handwritten digital images, the feature vector VEC in this case consists of 9 components, which are arranged as follows: VEC=[Number of intersection points at 5/12 vertical, number of intersection points on the vertical midline, number of intersection points at 7/12 vertical, number of intersection points at 1/3 horizontal, number of intersection points at 2/3 horizontal, number of intersection points on the left diagonal, number of intersection points on the right diagonal, number of endpoints]

2.3 Pattern RecognitionThis case uses the recognition method based on the pattern knowledge base, so the key step of the system research is to analyze the structure characteristics of the numeric characters and construct the model. So, this case starts with zeroThe structure analysis and modeling of the 10 numeric characters are carried out, and then the relevant features are extracted. Finally, the template library is constructed. During the experiment, we chose the standard written and free written two sets of samples as the training sample parameter adjustment on the knowledge base, the training samples from 200 specification of handwriting samples and 200 free handwriting samples, sample corresponding weight arithmetic average obtained by calculation in the knowledge base feature vector of each component. The knowledge base obtained through the above steps is composed of two sets of templates. In this experiment, we choose the recognition method based on template matching and measure the matching degree by calculating Euclidian distance. The feature vector in the recognition system contains 9 components, and the calculation distance formula is Euclidian distance:Therefore, the Euclidean distance between the image to be recognized and the feature vectors of each template in the knowledge base is calculated during the recognition process, namely, and 0The 10 digits are compared one by one, and the number corresponding to the minimum distance is selected as the final identification result.

Part of the source code

function varargout = NumDemo(varargin)
% NUMDEMO MATLAB code for NumDemo.fig
%      NUMDEMO, by itself, creates a new NUMDEMO or raises the existing
%      singleton*.
%
%      H = NUMDEMO returns the handle to a new NUMDEMO or the handle to
%      the existing singleton*.
%
%      NUMDEMO('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in NUMDEMO.M with the given input arguments.
%
%      NUMDEMO('Property'.'Value',...). creates anew NUMDEMO or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before NumDemo_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to NumDemo_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one % instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help NumDemo

% Last Modified by GUIDE v2. 5 28-Jun- 2021. 11:22:33

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @NumDemo_OpeningFcn, ...
                   'gui_OutputFcn',  @NumDemo_OutputFcn, ...
                   'gui_LayoutFcn', [],...'gui_Callback'[]);if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before NumDemo is made visible.
function NumDemo_OpeningFcn(hObject, ~, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to NumDemo (see VARARGIN)

% Choose default command line output for NumDemo
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes NumDemo wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = NumDemo_OutputFcn(~, ~, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
axis([0 250 0 250]);
clc;

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(~, ~, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ButtonDown pos1
if strcmp(get(gcf, 'SelectionType'), 'normal')
    ButtonDown = 1;
    pos1 = get(handles.axes1, 'CurrentPoint');
end

% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(~, ~, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ButtonDown pos1
if(ButtonDown == 1)
    pos = get(handles.axes1,'CurrentPoint');
    line([pos1(1.1) pos(1.1)], [pos1(1.2) pos(1.2)].'Color'.'Black'.'LineStyle'.The '-'.'LineWidth'.8.'Marker'.'. '.'MarkerSize'.20);
    pos1 = pos;
end

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(~, ~, ~)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global ButtonDown
ButtonDown = 0;


% --- Executes on button press in pushbutton_clear.
function pushbutton_clear_Callback(~, ~, handles)
% hObject    handle to pushbutton_clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%cla;
cla(handles.axes1);

% --- Executes on button press in pushbutton_save.
function pushbutton_save_Callback(~, ~, handles)
% hObject    handle to pushbutton_save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[f, p] = uiputfile({'*.bmp'},'Save Image');
str = strcat(p, f);
px  = getframe(handles.axes3);
curImg = frame2im(px);
imwrite(curImg, str,'bmp');

% --- Executes on button press in pushbutton_classify.
function pushbutton_classify_Callback(~, ~, handles)
% hObject    handle to pushbutton_classify (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%strSample = 'Data.mat';
cla(handles.axes4,handles.axes5,handles.axes6);
load 'Data.mat';
I1=handles.I1;
curFea = GetFeature(I1);
I2 = Normalize_Img(I1);
bw1 = Bw_Img(I1);
bw2 = Thin_Img(bw1);
bw = bw2;
sz = size(bw);
[r, c] = find(bw==1);
rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)];
vs = rect(1)+rect(3) * /5/12 1/2 7/12];
hs = rect(2)+rect(4) * /1/3 1/2 2/3];
pt1 = [rect(1:2); rect(1:2)+rect(3:4)];
pt2 = [rect(1)+rect(3) rect(2); rect(1) rect(2)+rect(4)];
k1 = (pt1(1.2)-pt1(2.2)) / (pt1(1.1)-pt1(2.1));
x1 = 1:sz(2);
y1 = k1*(x1-pt1(1.1)) + pt1(1.2);
k2 = (pt2(1.2)-pt2(2.2)) / (pt2(1.1)-pt2(2.1));
x2 = 1:sz(2);
y2 = k2*(x2-pt2(1.1)) + pt2(1.2);
    axes(handles.axes4);
    imshow(I2,[]); 
    handles.I2 = I2;
    hold on;
    h = rectangle('Position', [rect(1:2)- 1 rect(3:4) +2].'EdgeColor'.'r'.'LineWidth'.2);
    legend(h, 'Digital area mark'.'Location'.'South');
    axes(handles.axes5);
    imshow(bw1,[]);
    handles.bw1 = bw1;
    axes(handles.axes6);
    imshow(bw,[],'Border'.'Loose');
    handles.bw = bw;
    hold on;
    h = [];
    for i = 1 : length(hs)
        h = [h plot([1 sz(2)], [hs(i) hs(i)], 'r-')];
    end
    for i = 1 : length(vs)
        h = [h plot([vs(i) vs(i)], [1 sz(1)].'g-')];
    end
    h = [h plot(x1, y1, 'y-')];
    h = [h plot(x2, y2, 'm-')];
    legend([h(1) h(4) h(7) h(8)] and {'Horizontal line'.'Vertical line'.'Left diagonal'.'Right diagonal'}, 'Location'.'BestOutside');
    hold off;

%yLabel = euclid(curFea);
yLabel = MaskRecon(Data, curFea);
h = warndlg(num2str(yLabel),'Identification result');



% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- If Enable = ='on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over pushbutton_clear.
function pushbutton_clear_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to pushbutton_clear (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
function testsample = GetFeature(I)
clc;
testsample = zeros(1.9);  %1line9Column matrix % extract feature I1 = Normalize_Img(I); bw1 = Bw_Img(I1); bw2 = Thin_Img(bw1); bw = bw2; sz = size(bw); [r, c] = find(bw==1);
rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)];
vs = rect(1)+rect(3) * /5/12 1/2 7/12];
hs = rect(2)+rect(4) * /1/3 1/2 2/3];
pt1 = [rect(1:2); rect(1:2)+rect(3:4)];
pt2 = [rect(1)+rect(3) rect(2); rect(1) rect(2)+rect(4)];
k1 = (pt1(1.2)-pt1(2.2)) / (pt1(1.1)-pt1(2.1));
x1 = 1:sz(2);
y1 = k1*(x1-pt1(1.1)) + pt1(1.2);
k2 = (pt2(1.2)-pt2(2.2)) / (pt2(1.1)-pt2(2.1));
x2 = 1:sz(2);
y2 = k2*(x2-pt2(1.1)) + pt2(1.2);
if flag
   % figure('Name'.'Number identification'.'NumberTitle'.'Off'.'Units'.'Normalized'.'Position'[0.2 0.45 0.5 0.3]);
    %subplot(2.2.1); imshow(I1, []); title('Original image'.'FontWeight'.'Bold');
    %subplot(2.2.2); imshow(I2, []); title('Normalized image'.'FontWeight'.'Bold');
    axes(handles.axes4);
    imshow(I2,[]); title('Normalized image'.'FontWeight'.'Bold');
    handles.I2 = I2;
    hold on;
    h = rectangle('Position', [rect(1:2)- 1 rect(3:4) +2].'EdgeColor'.'r'.'LineWidth'.2);
  

  %  subplot(2.2.3); imshow(bw1, []); title('Binary image'.'FontWeight'.'Bold');
 

 %   subplot(2.2.4); imshow(bw, [], 'Border'.'Loose'); title('Refine image'.'FontWeight'.'Bold');
    hold on;
    h = [];
    for i = 1 : length(hs)
        h = [h plot([1 sz(2)], [hs(i) hs(i)], 'r-')];
    end
    for i = 1 : length(vs)
        h = [h plot([vs(i) vs(i)], [1 sz(1)].'g-')];
    end
  
    hold off;
end
v{1} = [1:sz(2); repmat(hs(1), 1, sz(2)))';
v{2} = [1:sz(2); repmat(hs(2), 1, sz(2))]';
v{3} = [1:sz(2); repmat(hs(3), 1, sz(2)))';
v{4} = [repmat(vs(1), 1, sz(1)); 1:sz(1)]';
v{5} = [repmat(vs(2), 1, sz(1)); 1:sz(1)]';
v{6} = [repmat(vs(3), 1, sz(1)); 1:sz(1)]';
v{7} = [x1; y1]';
v{8} = [x2; y2]';

for i = 1 : 8
    num(i) = GetImgLinePts(bw, round(v{i})- 1);
end
num(9) = sum(sum(endpoints(bw)));
testsample=num;
Copy the code

Third, the operation result

Iv. Matlab version and references

1 Matlab version 2014A

2 References [1] CAI Limei. MATLAB Image Processing — Theory, Algorithm and Case Analysis [M]. Tsinghua University Press, 2020. [2] Yang Dan, ZHAO Haibin, LONG Zhe. Detailed Analysis of MATLAB Image Processing Examples [M]. Tsinghua University Press, 2013. [3] Zhou Pin. MATLAB Image Processing and Graphic User Interface Design [M]. Tsinghua University Press, 2013. [4] LIU Chenglong. Proficient in MATLAB image processing [M]. Tsinghua University Press, 2015.