一、手写数字识别技术简介(附类似课程作业报告、lunwen)
1 案例背景
手写体数字识别是图像识别学科下的一个分支,是图像处理和模式识别研究领域的重要应用之一,并且具有很强的通用性。由于手写体数字的随意性很大,如笔画粗细、字体大小、倾斜角度等因素都有可能直接影响到字符的识别准确率,所以手写体数字识别是一个很有挑战性的课题。在过去的数十年中,研究者们提出了许多识别方法,并取得了一定的成果。手写体数字识别的实用性很强,在大规模数据统计如例行年检、人口普查、财务、税务、邮件分拣等应用领域都有广阔的应用前景"。
本案例讲述了图像中手写阿拉伯数字的识别过程,对手写数字识别的基于统计的方法进行了简要介绍和分析,并通过开发一个小型的手写体数字识别系统来进行实验。手写数字识别系统需要实现手写数字图像的读取功能、特征提取功能、数字的模板特征库的建立功能及识别功能。
2 BP算法与实现过程
2.1 BP算法基本原理
将已知输入向量和相应的输出向量(期望输出)作为训练样本,并假定即将学习的网络已被赋予一组权值。为消除梯度幅度的不利影响,利用弹性反向传播算法通过过如下步骤更新权值(图1):首先,使用初始权值(不管正确与否)从输入层开始向前传播,计算出所有神经元的输出,这样输出层的输出与期望输出(即输出值与目标值)之间存在较大的误差。然后,计算作为神经元权值函数的]误差函数(损失函数或目标函数、代价函数)的梯度,根据误差降低最快的方向来调整更新权值,通过将输出误差反向传播给隐含层来不断调整误差函数。在计算误差梯度的同时,使用与上面同样的方法更新隐含层的权值。反复迭代更新,直到损失函数达到预定的理想目标。在弹性反向传播算法的学习过程中,权值的修正值即为学习率,而梯度只影影响权值变化的方向,即正负。
图1 反向传播神经网络模型
1.2 感知器神经网络
感知器(multilayer perceptron, MLP) 神经网络是模式识别的简单二元分类人工网络, 它通过权值模仿神经细胞的突触,用激活函数模仿细胞体,偏置即为阈值。单层的感知器网络结构如图2所示。单层感知器可将外部输入x分成两类:当感知器的输出y为正数或零时,输入属于第一类;当感知器的输出为负数时,输入属于第二类。
1.3 实现过程
(1)图像读取
在本文中,设计并自建了样本的数据库,库中有0~9共10个阿拉伯数字的5000张不同的手写数字图像,均为白底黑色的bmp格式的文件, 每个数字对应500张图片。实验要从每一个数字中都随机选取450张手写图像作为训练样本,每一个数字剩下的50张作为测试样本。部分数字样张如图3所示。
图3 数字样张
(2)提取特征
本设计中的训练样本数量多,而而一般神经网络输入层的神经元数就是训练样本向量的维数,因此需要对训练样本向量做降维预处理。预处理过程就是通过灰度阈值函数将图像转换成二值图像。降维前需先将所有图像做一次缩放,以确保每个图像的输入向量都具有相同的像素。本设计选定图像缩放的高度和宽度分别为70像素点和50像素点,符合一般手写阿拉伯数字的高宽比。对这些缩放后的图像作纵横切割,如图4所示,每10×10个像素点作为一系列像素块,构成一张包含35个像素块的二值图像计算每一个像素块中0和1的占比,并用它作为模式的一个特征值,这样可以构成5x7的特征值矩阵。考虑到感知器神申经网络输入向量只能是一维,故需要将此矩阵转换成一维向量作为训练样本的输入,转置后共生成35个一维向量。
图4 缩放后的图像切割
(3)构造标签
无论是训练样本还是测试样本,都需要构造标签,前者用于映射的学习,后者用于判断训练网络的正确率。一般地,输出层神经元个数即为分类网络中的分类类别数。阿拉伯数字是10类,故输出神经元数为10。每个类由具体的500个图像构成,包含训练样本和测试样本。通过提取特征每个类均生成35个一维向量:用500个列向量(1000000000)T来标注模式1,即数字1;(0100000000)标注模式2,即数字2;(0010000000)标注模式3,即数字3;依此类推,最后的(0000000001)标注模式0,即数字0。运行代码如下:
(4)随机选定训练样本和测试样本测试
利用MATLAB中已有的rand()伪随机数生成函数来生成5000个介于0和1之间的伪随机数。将生成的伪随机数做升序排序,通过索引来记录随机数原来的位置,并将原来的位置组合成新的行向量。在本设计中,输入层的神经元有35个,输出层神经元有10个,选取25为中间隐含层神经元个数。
(5)数字识别与正确率的计算
对比测试前的标签和仿真后的输出,用测试前的标签值减去输出值,得到误差值,将误差为0的视为正确识别,求出神经网络的正确率。具体运行代码如下:
二、部分源代码
function varargout = findimg(varargin)
% FINDIMG MATLAB code for findimg.fig
% FINDIMG, by itself, creates a new FINDIMG or raises the existing
% singleton*.
%
% H = FINDIMG returns the handle to a new FINDIMG or the handle to
% the existing singleton*.
%
% FINDIMG('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FINDIMG.M with the given input arguments.
%
% FINDIMG('Property','Value',...) creates a new FINDIMG or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before findimg_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to findimg_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 findimg
% Last Modified by GUIDE v2.5 23-Apr-2021 16:06:05
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @findimg_OpeningFcn, ...
'gui_OutputFcn', @findimg_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 findimg is made visible.
function findimg_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 findimg (see VARARGIN)
% Choose default command line output for findimg
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes findimg wait for user response (see UIRESUME)
% uiwait(handles.figure1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%定义全局变量
global ButtonDown pos1;
ButtonDown = [];
pos1 = [];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Outputs from this function are returned to the command line.
function varargout = findimg_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;
axis([0 250 0 250]);
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
%取消显示axes的坐标轴
set((hObject),'xTick',[]);
set((hObject),'yTick',[]);
% --- 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)
[f,p]=uiputfile({'*.jpg'},'保存文件'); %保存所画的图
str=strcat(p,f);
pix=getframe(handles.axes1);
imwrite(pix.cdata,str,'jpg')
% --- 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)
cla(handles.axes1); %清楚axes中所画的图像
% --- 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)
pix=getframe(handles.axes1);
imwrite(pix.cdata,'imgtest.jpg');
newimage = imread('imgtest.jpg'); %保存新画的数字
newimgResult = identify(newimage) ; %通过识别函数进行比较
Result = BpRecognize(newimgResult);
msgbox(num2str(Result),'识别结果','help');
% --- 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)
BpTrain();
msgbox('Finish Train','提示','modal');
% --- 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 ButtonDown pos1;
if(strcmp(get(gcf,'SelectionType'),'normal'))%判断鼠标按下的类型,normal为左键
ButtonDown=1;
pos1=get(handles.axes1,'CurrentPoint');%获取坐标轴上鼠标的位置
end
function [] = BpTrain()
%UNTITLED5 Summary of this function goes here
% Detailed explanation goes here
clear all;
clc
ctime = datestr(now, 30);%取系统时间
tseed = str2num(ctime((end - 5) : end)) ;%将时间字符转换为数字
rand('seed', tseed) ;%设置种子,若不设置种子则可取到伪随机数
load Data2; %数据有10类数据,每类20行25列,有4列是标签。共200*29
c = 0;
data = [];
for i = 1:10
for j = 1:20
c = c + 1;
data(c,:) = pattern(i).feature(j,:);
end
end
%=============训练数据=============
Data = data(1:20, 1:25);
Data = [ Data ; data(21:40, 1:25)];
Data = [ Data ; data(41:60, 1:25)];
Data = [ Data ; data(61:80, 1:25)];
Data = [ Data ; data(81:100, 1:25)];
Data = [ Data ; data(101:120, 1:25)];
Data = [ Data ; data(121:140, 1:25)];
Data = [ Data ; data(141:160, 1:25)];
Data = [ Data ; data(161:180, 1:25)];
Data = [ Data ; data(181:200, 1:25)];
%0标签
Data(1:20, 26) = 0;
Data(1:20, 27) = 0;
Data(1:20, 28) = 0;
Data(1:20, 29) = 0;
%1标签
Data(21:40, 26) = 0;
Data(21:40, 27) = 0;
Data(21:40, 28) = 0;
Data(21:40, 29) = 1;
Data(41:60, 26) = 0;
Data(41:60, 27) = 0;
Data(41:60, 28) = 1;
Data(41:60, 29) = 0;
Data(61:80, 26) = 0;
Data(61:80, 27) = 0;
Data(61:80, 28) = 1;
Data(61:80, 29) = 1;
Data(81:100, 26) = 0;
Data(81:100, 27) = 1;
Data(81:100, 28) = 0;
Data(81:100, 29) = 0;
Data(101:120, 26) = 0;
Data(101:120, 27) = 1;
Data(101:120, 28) = 0;
Data(101:120, 29) = 1;
Data(121:140, 26) = 0;
Data(121:140, 27) = 1;
Data(121:140, 28) = 1;
Data(121:140, 29) = 0;
Data(141:160, 26) = 0;
Data(141:160, 27) = 1;
Data(141:160, 28) = 1;
Data(141:160, 29) = 1;
Data(161:180, 26) = 1;
Data(161:180, 27) = 0;
Data(161:180, 28) = 0;
Data(161:180, 29) = 0;
Data(181:200, 26) = 1;
Data(181:200, 27) = 0;
Data(181:200, 28) = 0;
Data(181:200, 29) = 1;
DN = size(Data, 1);
%输入层结点数
S1N = 25;
%第二层结点数
S2N = 50;
%输出层结点数
S3N = 4;
%学习率
sk = 0.5;
%随机初始化各层的W和B
W2 = -1 + 2 .* rand(S2N, S1N);
B2 = -1 + 2 .* rand(S2N, 1);
W3 = -1 + 2 .* rand(S3N, S2N);
B3 = -1 + 2 .* rand(S3N, 1);
%数据样本下标
di = 1;
for i=1:50000
%第三层输出
n3 = W3 * a2 + B3;
a3 = Logsig(n3); %第三层传输函数为logsig
%计算输出层误差
e = t - a3;
err = (e') * e;
Fd3 = diag((1 - a3) .* a3);
S3 = -2 * Fd3 * e;
Fd2 = diag((1 - a2) .* a2);
S2 = Fd2 * (W3') * S3;
W3 = W3 - sk*S3*(a2'); %梯度下降步长
B3 = B3 - sk*S3;
W2 = W2 - sk*S2*(a1');
B2 = B2 - sk*S2;
end
msgbox(num2str(err),'输出层误差','help');
save('W2.mat','W2');
save('W3.mat','W3');
save('B2.mat','B2');
save('B3.mat','B3');
end
三、运行结果
四、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.