【手写数字识别】基于matlab Fisher分类手写数字识别 【含Matlab源码 505期】
一、Fisher分类手写数字识别简介
1引言
手写体数字识别在过去的几十年里一直是模式识别领域的研究热点,在手写较多的领域如邮政编码、统计报表、财务报表、支票的数字识别等方面有广泛应用.专家、学者提出了很多识别算法,但是很多只是停留在实验室中,由于书写风格的不同造成了各种字符变形,研究高性能的手写数字识别算法是一个有相当挑战性的课题.提取字符特征大体分两类:一是统计特征,通常包括点矩、特征区域等;二是结构特征,通常包括圈、端点、拐角点、笔画、轮廓等现有的识别方法很多,例如基于模板匹配或结构特征的方法、使用模糊推理的方法、基于矩和变换的方法、基于神经网络的方法等.
2 预处理及特征提取
为防止手写时(粘带等操作)出现离散的孤立点,笔者采用面积滤波法去除离散点.滤波算法描述如下:
[step 1] 循环:扫描整个书写区;
[step 2] 计算:根据8邻域像素点计算各连通区域的像素点数目, 作为面积值s;
[step 3] 判断:若面积值ss Threshold(指定阈值) , 则使用背景色重绘当前离散点区
[step 4] 继续:跳转到step 2;
[step 5] 输出:滤波处理完成后, 得到相对连通的数字图像.
对输入的手写图像进行上述处理后,得到了相对较为规整的数字图像,减少了干扰点源,为后续的特征提取工作做了较好的预处理.如图1为书写时获得的输入图像含噪声点的情况,通过面积滤波后得至到噪声点较少的图像2.
图1 含噪声点的图像 图2 滤波后 图3 区域定位 图4 特征分割
滤波处理完成后,进入边界判断处理.为得到数字的边界进行归一化由于书写数字大部分情况都不能充满数字区,所以笔者采用从数字区的4个边界向中心紧缩的方法寻找数字边界.区域定定位结果如图3.此种定位方法好处就是能够动态地获得各小区域的比值,实现了一种弹性的归一化,不必用专门放大或缩小卜的方法作归一化,为后续识别工作带来方便.定位完成后,对数字进行特征提取.特征提取有很多方法,笔者采用5×5的模板[4]:
水平方向上的间距为dn=(h2-h1)/5(1)
垂直方向上的间距为dy=(V2-V1)/5(2)
由于整个数字区域的水平距离、垂直距离很多时候不是5的倍数,会造成最后一列和最后一行的数据不能完全包括在内,所以在实际操作中,笔者将前4x4小区域间距分别采用所求得的dh,dy,水平方向余下的全部作为最后一行的范围,其宽度为dhs w<2dh; 垂直方向余下的全部作为最后一列的范围, 其高度为dy≤h<2·dy.所以有时会看到分割后最后一列和最后一行的大小与前4x4小区域不一致.如上操作将得到5×5=25维特征E值,特征值的选取为每一个小区域中数字像素数与整个小区域面积之比,若大于给定的阈值Th=0.05,便将此维特征记为为1,否则为0.
3 Fisher算法
在解决模式识别问题时, Fisher判别是一种有效的模式分类算法[5, 6] 它求出一个最佳分类向量, 将原来高维的模式样本特征投影到最佳分类向量空间以达到减少特征维数的目的, 这是Fisher算法要解决的基本问题.
Fisher算法的主要思想:为得到最佳分类向量, 需要计算各类别样品均值、样品类内离散度矩阵、总类间离散度矩阵、样品类间离散度矩阵.根据Fisher准则找到最佳分类向量, 将训练样品集进行投影到待求的直线方向上, 然后求出边界点,最后将待测样品特征向已求出的直线方向投影,计算与训练样品投影点的边界距离关系,便可对应地识别结果.
最初Fisher算法主要是进行两类问题的分类, 而问题中要解决0~9中的10个数字的分类识别问题, 所以需要构建10×(10-1) /2=45次分类.Fisher算法描述如下:
[step 1] 均值:计算各类的样品均值:
其中Ni是对应ωi类中的样品个数, X为样品特征向量.
[step2]离散度矩阵:对样品计算类内离散度矩阵Si
各样品的总类内离散度矩阵S:
Sw=Z Sji=0, 1(5)
计算样品类间离散度矩阵S。
Sd=(mo-m1)(mo-m1)T(6)
[step 3] 投影向量:Fisher判别的目的是找一个最佳分类向量W, 使得映射后的类间离散度矩阵和类内离散度矩阵的比值最大.
[step5]分类:对于待测样品特征值X, 计算它在最佳分类向量上的投影点yx:
yx=WTX (12)
上述算法描述中, 采用了两类Fisher分类法, 对于10个数字的识别, 在实际编程时需要两两分类处理, 最后在计算结果对应最大值者位置为对应的类别序号, 即为识别结果.
二、部分源代码
function varargout = main_gui(varargin)
% MAIN_GUI MATLAB code for main_gui.fig
% MAIN_GUI, by itself, creates a new MAIN_GUI or raises the existing
% singleton*.
%
% H = MAIN_GUI returns the handle to a new MAIN_GUI or the handle to
% the existing singleton*.
%
% MAIN_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MAIN_GUI.M with the given input arguments.
%
% MAIN_GUI('Property','Value',...) creates a new MAIN_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before main_gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to main_gui_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 main_gui
% Last Modified by GUIDE v2.5 02-Apr-2019 15:37:29
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @main_gui_OpeningFcn, ...
'gui_OutputFcn', @main_gui_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 main_gui is made visible.
function main_gui_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 main_gui (see VARARGIN)
% set( handles.axes1, 'visible', 'off' );
% set( handles.axes1, 'box', 'on' );
global W W0;
load W
load W0
% Choose default command line output for main_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes main_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = main_gui_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 mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, 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 draw_enable; %定义一个标志,1表示绘图,0表示停止绘图
global x;
global y;
draw_enable=1;
if draw_enable
position=get(gca,'currentpoint'); %gca(获取当前坐标轴的句柄)
x(1)=position(1);
y(1)=position(3);
end
% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, 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 draw_enable;
global x;
global y;
global h1;
if draw_enable==1
position=get(gca,'currentpoint');
x(2)=position(1);
y(2)=position(3);
h1=line(x,y,'LineWidth',25,'color','k','Linestyle','-.');
x(1)=x(2);
y(1)=y(2); %鼠标移动,随时更新数据
end
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(hObject, eventdata, 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 draw_enable;
draw_enable=0;
function num_Callback(hObject, eventdata, handles)
% hObject handle to num (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 num as text
% str2double(get(hObject,'String')) returns contents of num as a double
% --- Executes during object creation, after setting all properties.
function num_CreateFcn(hObject, eventdata, handles)
% hObject handle to num (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 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)
global W
global W0
img=getframe(handles.axes1);
img=imresize(img.cdata,[28,28]);
img=im2bw(img,graythresh(img));
img=~img;
axes(handles.axes2);
img1=~img;
imshow(img1);%img1为预览图像
%计算概率
for cnt=1:4
for cnt2=1:4
Atemp=sum(img(((cnt*7-6):(cnt*7)),((cnt2*7-6):(cnt2*7))));%10*10box
lett((cnt-1)*4+cnt2)=sum(Atemp);
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
三、运行结果
四、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/114698413
- 点赞
- 收藏
- 关注作者
评论(0)