您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 人事档案/员工关系 > 基于人工神经网络的MATLAB手写数字识别系统
基于人工神经网络的MATLAB手写数字识别系统一、函数MouseDraw实现手写识别系统GUI界面的建立和鼠标手写的实现。(使用时保存为MouseDraw.m)functionMouseDraw(action)%MouseDraw本例展示如何以HandleGraphics来设定滑鼠事件%(MouseDrawEvents)的反应指令(Callbacks)%本程序在鼠标移动非常快时,不会造成画“断线”%global不能传矩阵globalInitialXInitialYFigHandlehb2hb3hb4counthb5hb6hb7count='E:\im.jpg';imSize=50;ifnargin==0,action='start';endswitch(action)%%开启图形视窗case'start',FigHandle=figure('WindowButtonDownFcn','MouseDrawdown','DeleteFcn','savebpnet');axis([1imSize1imSize]);%设定图轴范围%set(gca,'Position',[.25.20.7.7]);axisoff;gridoff;boxon;%将图轴加上图框title('手写体输入窗');tryevalin('base','loadbpnet')catchevalin('base','bpgdtrain');end%%fprintf('start');%%设定滑鼠按钮被按下时的反应指令为「MouseDrawdown」%set(gcf,'WindowButtonDownFcn','MouseDrawdown');hb1=uicontrol('Parent',FigHandle,'Units','Normalized',...'Position',[.3.01.13.07],'String','保存',...'Callback',['exa=rgb2gray(frame2im(getframe(gca)));','imwrite(exa,''E:\im.jpg'')']);hb2=uicontrol('Parent',FigHandle,'Style','popupmenu','Position',[50505030],...'String',{'26','24','22','20','18','16','14','12','10'});hb3=uicontrol('Parent',FigHandle,'Style','text',...'Position',[10909030],'String',['CurrentX()','CurrentY()']);hb4=uicontrol('Parent',FigHandle,'Style','popupmenu','Position',[50205030],...'String',{'Red','Blue','Black','Yellow','Green'});uicontrol('Parent',FigHandle,'Position',[27067030],'String','训练','Callback',...['exa=rgb2gray(frame2im(getframe(gca)));','sample=reshape(recgnition(exa),25,1);','clc;',...'t=inputdlg(''数字类别'',''样品训练'');','t=str2num(t{1,1})/10;',...'bpnet.trainParam.lr=str2num(get(hb6,''String''));','bpnet.trainParam.goal=str2num(get(hb7,''String''));',...'[bpnet]=train(bpnet,sample,t);','savebpnet']);uicontrol('Parent',FigHandle,'Position',[36067030],'String','识别','Callback',...['exa=rgb2gray(frame2im(getframe(gca)));','sample=reshape(recgnition(exa),25,1);',...'record=round(sim(bpnet,sample)*10);','clc;','set(hb5,''String'',num2str(record),''fontSize'',48);']);uicontrol('Parent',FigHandle,'Style','text','Position',[10603020],'String','字号');uicontrol('Parent',FigHandle,'Style','text','Position',[10303020],'String','颜色');hb5=uicontrol('Parent',FigHandle,'Style','text','Position',[101509090]);uicontrol('Parent',FigHandle,'Style','text','Position',[52605020],'String','学习速率');hb6=uicontrol('Parent',FigHandle,'Style','Edit','Position',[602603020],'String','0.01');uicontrol('Parent',FigHandle,'Style','text','Position',[52905020],'String','训练精度');hb7=uicontrol('Parent',FigHandle,'Style','Edit','Position',[602903020],'String','0.005');uicontrol('Parent',FigHandle,'Style','pushbutton','Position',[45067030],'String','清除','Callback','cla');%将函数变量导入到工作空间;assignin('base','hb5',hb5);assignin('base','hb6',hb6);assignin('base','hb7',hb7);%%%%%%%%%%%%%%%%%%%%%%%%%%%dlmwrite('IXT.txt',-10,'delimiter','\t','precision',6);dlmwrite('IYT.txt',-10,'delimiter','\t','precision',6);%%滑鼠按钮被按下时的反应指令case'down',ifstrcmp(get(FigHandle,'SelectionType'),'normal')%如果是左键set(FigHandle,'pointer','hand');CurPiont=get(gca,'CurrentPoint');InitialX=CurPiont(1,1);InitialY=CurPiont(1,2);dlmwrite('IXT.txt',InitialX,'-append','delimiter','\t','precision',6);dlmwrite('IYT.txt',InitialY,'-append','delimiter','\t','precision',6);%列印「MouseDrawdown!」讯息%%fprintf('MouseDrawdown!\n');%设定滑鼠移动时的反应指令为「MouseDrawmove」set(gcf,'WindowButtonMotionFcn','MouseDrawmove');set(gcf,'WindowButtonUpFcn','MouseDrawup');elseifstrcmp(get(FigHandle,'SelectionType'),'alt')%如果是右键set(FigHandle,'Pointer','arrow');set(FigHandle,'WindowButtonMotionFcn','')set(FigHandle,'WindowButtonUpFcn','')fprintf('MouseDrawrightbuttondown!\n');ImageX=importdata('IXT.txt');ImageY=importdata('IYT.txt');InputImage=ones(imSize);roundX=round(ImageX);roundY=round(ImageY);fork=1:size(ImageX,1)if0roundX(k)&&roundX(k)imSize&&0roundY(k)&&roundY(k)imSizeInputImage(roundX(k)-1:roundX(k)+2,roundY(k)-1:roundY(k)+2)=0;endendInputImage=imrotate(InputImage,90);%图像旋转90figure(2);imshow(InputImage);end%%滑鼠移动时的反应指令case'move',CurPiont=get(gca,'CurrentPoint');X=CurPiont(1,1);Y=CurPiont(1,2);set(hb3,'String',['CurrentX(',num2str(X),')','CurrentY(',num2str(Y),')']);%当鼠标移动较快时,不会出现离散点。%利用y=kx+b直线方程实现。x_gap=0.1;%定义x方向增量y_gap=0.1;%定义y方向增量ifXInitialXstep_x=x_gap;elsestep_x=-x_gap;endifYInitialYstep_y=y_gap;elsestep_y=-y_gap;end%定义x,y的变化范围和步长ifabs(X-InitialX)0.01%线平行于y轴,即斜率不存在时iy=InitialY:step_y:Y;ix=X.*ones(1,size(iy,2));elseix=InitialX:step_x:X;%定义x的变化范围和步长%当斜率存在,即k=(Y-InitialY)/(X-InitialX)~=0iy=(Y-InitialY)/(X-InitialX).*(ix-InitialX)+InitialY;endImageX=[ix,X];ImageY=cat(2,iy,Y);popup_index1=26-(get(hb2,'Value')-1)*2;popup_index2=get(hb4,'Value');switch(popup_index2)case1line(ImageX,ImageY,'marker','.','markerSize',popup_index1,...'LineStyle','-','LineWidth',4,'Color','Red');case2line(ImageX,ImageY,'marker','.','markerSize',popup_index1,...'LineStyle','-','LineWidth',4,'Color','Blue');case3line(ImageX,ImageY,'marker','.','markerSize',popup_index1,...'LineStyle','-','LineWidth',4,'Color','Black');case4line(ImageX,ImageY,'marker','.','markerSize',popup_index1,...'LineStyle','-','LineWidth',4,
本文标题:基于人工神经网络的MATLAB手写数字识别系统
链接地址:https://www.777doc.com/doc-4827874 .html