【手写数字识别】基于matlab GUI模板匹配手写数字识别【含Matlab源码 094期】

举报
海神之光 发表于 2022/05/29 05:28:50 2022/05/29
【摘要】 一、手写数字识别技术简介 1 案例背景 手写体数字识别是图像识别学科下的一个分支,是图像处理和模式识别研究领域的重要应用之一,并且具有很强的通用性。由于手写体数字的随意性很大,如笔画粗细、字体大小、倾斜...

一、手写数字识别技术简介

1 案例背景
手写体数字识别是图像识别学科下的一个分支,是图像处理和模式识别研究领域的重要应用之一,并且具有很强的通用性。由于手写体数字的随意性很大,如笔画粗细、字体大小、倾斜角度等因素都有可能直接影响到字符的识别准确率,所以手写体数字识别是一个很有挑战性的课题。在过去的数十年中,研究者们提出了许多识别方法,并取得了一定的成果。手写体数字识别的实用性很强,在大规模数据统计如例行年检、人口普查、财务、税务、邮件分拣等应用领域都有广阔的应用前景"。
本案例讲述了图像中手写阿拉伯数字的识别过程,对手写数字识别的基于统计的方法进行了简要介绍和分析,并通过开发一个小型的手写体数字识别系统来进行实验。手写数字识别系统需要实现手写数字图像的读取功能、特征提取功能、数字的模板特征库的建立功能及识别功能。

2 理论基础
2.1算法流程
首先,读入手写数字图片进行图像归一化处理,统一尺寸,默认为24x24图像块,并通过ostu算法进行二值化; 其次, 对二值化图像进行图像细化等形态学操作,并按照算法要求进行特征提取;最后,载入模板矩阵进行比对,选用欧式距离测度,得到识别结果。其算法流程图如图所示。
在这里插入图片描述
2.2 特征提取
根据手写数字图像本身的结构特征,通过计算端点、指定方向直线的交叉点个数来作为特征向量。其主要步骤如下。
(1)垂直交点
对细化后的手写数字图像分别在其列宽的:5/12 1/2 7/12处生成垂直的三条直线,提取这三条垂直直线与数字笔划的交点数并存储。
(2)水平交点
对细化后的手写数字图像分别在其列宽的号号处生成水平的三条直线,提取这1/3 1/2 2/3三条水平直线与数字笔划的交点数并存储。
(3)对角交点
对细化后的手写数字图像分别取两条对角直线,提取这两条对角直线与数字笔划的交点数并存储。
由于以上步骤均作用于经细化后的数字图像,其笔划简单且特征稳定,因此对其提取的基本交点及结构端点能反映数字的本质特征,可快速、有效地识别数字字符,并达到较好的识别正确率。
其中,提取笔划结构端点特征的算法如下。
①目标定位
对细化后的手写数字图像按行从上到下、按列从左到右进行顺序扫描,定位选择黑像素点P作为手写笔划目标。
②邻域统计
计算黑色像素P的8邻域之和N,若N=1,则像素P为端点,端点计数器加1;否则舍弃该点。
③遍历图像
遍历整个图像,重复进行目标定位、邻域统计的操作流程,提取端点特征。依据上述对手写数字图像的交点、端点特征提取方法, 本案例中的特征向量VEC由9个分量组成,其排列如下:
VEC=[垂直5/12处交点数, 垂直中线交点数, 垂直7/12处交点数,水平1/3处交点数,水平中线交点数,水平2/3处交点数,左对角线交点数,右对角线交点数,端点数]

2.3 模式识别
本案例采用的是基于模式知识库的识别方法,所以系统调研的关键步骤就是对数字字符的结构特征的分析及其模型的构造。因此,本案例首先对0~9这10个数字字符进行结构分析并建模,然后提取相关特征,最后构造模板库。在实验过程中,我们选择规范手写和自由手写两组样本作为训练样本对知识库进行参数调整,这些训练样本由200个规范手写体样本和200个自由手写体样本组成,通过计算样本对应分量的算术平均值获得知识库中特征向量的每个分量。
通过上述步骤得到的知识库由两套模板组成,在本次实验过程中,我们选择基于模板匹配的识别方法,通过计算欧式距离来衡量匹配程度。识别系统中的特征向量包含9个分量,且计算距离公式是欧式距离:
在这里插入图片描述
因此,在识别过程中分别计算待识别图像与知识库中各个模板特征向量之间的欧式距离,即与0~9这10个数字逐个比较,选择最小距离对应的数字作为最后的识别结果。

3 模板匹配算法
3.1 相似性测度求匹配
模板匹配的实际操作思路很简单:拿已知的模板,和原图像中同样大小的一块区域去对。最开始时,模板的左上角点和图像的左上角点是重合的,拿模板和原图像中同样大小的一块区域去对比,然后平移到下一个像素,仍然进行同样的操作, ……所有的位置都对完后,差别最小的那块就是我们要找的物体。

以上所描述的是相似性测度法求匹配的求解思路,其在计算机中操作的如图2所示。设模板T叠放在搜索图上平移,被模板覆盖搜索图下的那个图像叫做子图Si , j,i , j 为这块子图的左上角像素点在S图的坐标,称为参考点,从图2可知,i , j 的取值范围是:1<i ,j <N- M+1. 现在可以比较T和Si , j的内容。若两者一致,则T和S之差为零. 因此,可用下列公式(1) 和公式(2) 来衡量T和Si , j的相似程度。
在这里插入图片描述
在这里插入图片描述
在(2) 式中第3项表示模板总能量,是一个与(i , j) 无关的常数;第1项是模板覆盖下子图的能量,它随着(i , j) 的位置缓慢地改变;第2项表示的子图与模板的互相关系,随着(i , j) 的改变而改变,当T和Si , j匹配时这项取值最大。因此可用下列相关函数(3) 作相似性测度。
在这里插入图片描述
当矢量t 和S1之间的夹角为0时,即当S1(i , j) =kt 时(k为常量) ,有R(i , j) =1,否则R(i , j) <1. 显然R(i , j) 越大,模板T和Si , j就越相似,点(i , j) 就是我们要寻找的匹配点。

3.2 序贯相似性检测的算法
用相关法求匹配的计算量很大,因为模板要在(N- M+1)2个参考位置上作相关计算,除了在匹配点外,其它点作的都是无用功。因此,人们提出一种叫序贯相似性检测的算法,简称SSDA(Sequential SimiliarityDetectionAlgorithm) 其要点是:
在数字图像中,SSDA法用公式(6) 计算图像f ( x, y) 在点(i , j) 的非相似度m(i , j) 作为匹配尺度。式中(i , j) 表示的不是模板中心坐标,而是它左上角坐标。模板的大小为n ×m。
在这里插入图片描述
如果在(i , j) 处图像中有和模板一致的图案时,则m(i , j) 值很小,反之则很大。特别是模板和搜索图下的子图部分,完全不一致的场合下,如果在模板内的各像素与图像重合部分对应的像素灰度差的绝对值依次增加,其和会急剧增大。 因此,在作加法时,如果灰度差的绝对值部分和超过某一阈值时,就认为这个位置不存在和模板一致的图案,从而转移到下一个位置上计算m(i , j)。并且在这模板下的各像素点计算中止,因此能大幅度地缩短计算时间,提高匹配速度。
根据上述思路,我们可以进一步改进SSDA算法。就是把在图像上的模板移动分为粗检索和细检索2个阶段进行。首先进行粗检索,它不是让模板每次移动1个像素,而是每隔若干的像素把模板和图像重叠,并且计算匹配的尺度,从而求出待寻找的图案大致存在的范围。然后在这个范围内,让模板每隔1个像素移动1次,根据匹配的尺度确定寻找图案的所在位置。这样,整体上计算模板匹配的次数减少了,计算时间缩短,匹配的速度提高了。但用这种方法具有漏掉图像中最恰当位置的危险性。

3.3 相关算法
2个函数的相关性定义,可用公式(7)表示:
在这里插入图片描述
f*表示f 的复共轭。我们知道相关理论类似卷积理论,F( u, v) 和 H( u, v) 分别表示f ( x, y) 和h( x, y)的傅立叶变换. 根据卷积理论有
在这里插入图片描述
可知卷积是空间域过滤和频率域过滤之间的纽带。相关的重要用途在于匹配。在匹配中,f ( x, y) 是一幅包含物体或区域的图像。如果想要确定f 是否包含有感兴趣的物体或区域,让h( x, y) 作为那个物体的区域(通常称该图像为模板)。如果匹配成功,2个函数的相关值会在h找到f 中相应点的位置上达到最大。从上面分析可知,相关算法可以有2种方法:可在空间域进行,也可在频率域进行。

3.4 幅度排序相关算法
这种算法有2个步骤组成:
第1步,把实时图中的各个灰度值按幅度的大小排成列的形式,然后在对它进行二进制(或三进制) 编码,根据二进制排序的序列,把实时图变换为二进制阵列的一个有序的集合{ Cn, n =1,2, …, N}。这一过程称之为幅度排序预处理。
第2步,序贯地将这些二进制序列与基准图进行由粗到细的相关,直到确定出匹配点为止。由于篇幅的限制,这里就不列出例子了。

3.5 分层搜索的序惯判决算法
这种分层搜索算法是直接基于人们先粗后细寻找事物的惯例而形成的,例如,在中国地图上找肇庆的位置时,可以先找广东省这个地域,这过程称为粗相关。然后在这个地域中,再仔细确定肇庆的位置,这叫做细相关。很明显,利用这种方法,可以很快找出肇庆的位置。因为在这过程中省略了寻找广东省以外区域所需的时间,这种方法称为分层搜索的序贯判决法,利用这种思想形成的分层算法具有相当高的搜索速度。限于篇幅,这里只给出这种操作的一般思路。

4 总结
模式匹配本质就是应用数学。模板匹配过程如下:①将图像数字化,按顺序取出每个点的像素值;②代入事先建立的数学模型进行预处理;③选择一种合适的算法进行模式匹配;④将匹配后图像的坐标列出或直接在原图显示.。在模板匹配操作中最为关键的是如何建立数学模型,这是正确匹配的核心。

二、部分源代码

function varargout = daunxujian1(varargin)
% DAUNXUJIAN1 MATLAB code for daunxujian1.fig
%      DAUNXUJIAN1, by itself, creates a new DAUNXUJIAN1 or raises the existing
%      singleton*.
%
%      H = DAUNXUJIAN1 returns the handle to a new DAUNXUJIAN1 or the handle to
%      the existing singleton*.
%
%      DAUNXUJIAN1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DAUNXUJIAN1.M with the given input arguments.
%
%      DAUNXUJIAN1('Property','Value',...) creates a new DAUNXUJIAN1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before daunxujian1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to daunxujian1_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 daunxujian1
 
% Last Modified by GUIDE v2.5 23-Oct-2020 21:52:34
 
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @daunxujian1_OpeningFcn, ...
                   'gui_OutputFcn',  @daunxujian1_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 daunxujian1 is made visible.
function daunxujian1_OpeningFcn(hObject, eventdata, 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 daunxujian1 (see VARARGIN)
% Choose default command line output for daunxujian1
handles.output = hObject;
handles.Result = [];
handles.File = [];
% Update handles structure
guidata(hObject, handles);
clc; warning off all;
InitAxes(handles);
% UIWAIT makes daunxujian1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
 
 
% --- Outputs from this function are returned to the command line.
function varargout = daunxujian1_OutputFcn(hObject, eventdata, 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;
 
 
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
    '*.*','All Files' },'载入图像',...
    fullfile(pwd, 'images'));
I = imread(fullfile(pathname, filename));
Result = Process_Main(I);
handles.File = fullfile(pathname, filename);
handles.Result = Result;
guidata(hObject, handles);
InitAxes(handles)
axes(handles.axes1); imshow(handles.Result.Image); title('原图像');
 
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isempty(handles.Result)
    axes(handles.axes1); imshow(handles.Result.Image); title('原图像');
    axes(handles.axes2); imshow(handles.Result.Medfilt); title('中值滤波图像');
end
 
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isempty(handles.Result)
    axes(handles.axes1); imshow(handles.Result.Image); title('原图像');
    axes(handles.axes2); imshow(handles.Result.Medfilt); title('中值滤波图像');
    axes(handles.axes3); imshow(handles.Result.Normalize); title('归一化图像');
end
 
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isempty(handles.Result)
    axes(handles.axes1); imshow(handles.Result.Image); title('原图像');
    axes(handles.axes2); imshow(handles.Result.Medfilt); title('中值滤波图像');
    axes(handles.axes3); imshow(handles.Result.Normalize); title('归一化图像');
    axes(handles.axes4); imshow(handles.Result.Bww); title('二值化图像');
end
 
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isempty(handles.Result)
    axes(handles.axes1); imshow(handles.Result.Image); title('原图像');
    axes(handles.axes2); imshow(handles.Result.Medfilt); title('中值滤波图像');
    axes(handles.axes3); imshow(handles.Result.Normalize); title('归一化图像');
    axes(handles.axes4); imshow(handles.Result.Bww); title('二值化图像');
    axes(handles.axes6); imshow(handles.Result.Thin); title('细化处理');
    axes(handles.axes5); imshow(handles.Result.Bw); title('特征提取');
    hold on;
    h = [];
    for i = 1 : length(handles.Result.hs) %绘制水平线
        h = [h plot([1 handles.Result.sz(2)], [handles.Result.hs(i) handles.Result.hs(i)], 'r-')];
    end
    for i = 1 : length(handles.Result.vs)%绘制竖直线
        h = [h plot([handles.Result.vs(i) handles.Result.vs(i)], [1 handles.Result.sz(1)], 'g-')];
    end
    h = [h plot(handles.Result.x1, handles.Result.y1, 'y-')];%绘制左对角线
    h = [h plot(handles.Result.x2, handles.Result.y2, 'm-')];%绘制右对角线
end
 
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uiputfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...
    '*.*','All Files' },'Save Image',...
    fullfile(pwd, 'Result/result.png'));
if ~isequal(filename, 0)
    imwrite(handles.Result.label, fullfile(pathname, filename));
    msgbox('保存图像成功!', '信息提示框');
end
 
 
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
choice = questdlg('确定退出?', ...
    '退出', ...
    '是','否','否');
switch choice
    case '是'
        close;
    otherwise
        return;
end
 
 
function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 
% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
 
 
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
 
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
 
 
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit1,'string',handles.Result.label);
 
 
% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if ~isempty(handles.Result)
    axes(handles.axes1); imshow(handles.Result.Image); title('原图像');
    axes(handles.axes2); imshow(handles.Result.Medfilt); title('中值滤波图像');
    axes(handles.axes3); imshow(handles.Result.Normalize); title('归一化图像');
    axes(handles.axes4); imshow(handles.Result.Bww); title('二值化图像');
    axes(handles.axes6); imshow(handles.Result.Thin); title('细化处理');
end

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221

三、运行结果

在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.

文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。

原文链接:qq912100926.blog.csdn.net/article/details/112461486

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。