您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > AI人工智能 > 实验七基于神经网络的模式识别实验
实验七:基于神经网络的模式识别实验一、实验目的理解BP神经网络和离散Hopfield神经网络的结构和原理,掌握反向传播学习算法对神经元的训练过程,了解反向传播公式。通过构建BP网络和离散Hopfield网络模式识别实例,熟悉前馈网络和反馈网络的原理及结构。综合掌握模式识别的原理,了解识别过程的程序设计方法。二、实验内容熟悉模式识别的理论方法,用选择一种合适的识别方法,对图像中的字符(英文字母)进行识别,能够区分出不同的形态的26个字母。在Matlab中,采用BP神经网络,对读取的数据进行训练,进而识别。1.程序设计(1)程序各流程图实验中主程序流程图如图4-1所示:1图4-1主程序流程图其中图像预处理的流程如图4-2所示:图4-2图像预处理的流程神经网络训练的具体流程如图4-3所示:图像输入灰度转化图像二值化图像分割归一化调整调整比例显示预处理结果获取图像数据创建神经网络训练存储训练好的神经网络2图4-3神经网络训练流程(2)程序清单%形成用户界面clearall;%添加图形窗口H=figure('Color',[0.850.850.85],...'position',[400300500400],...'Name','基于BP神经网络的英文字母识别',...'NumberTitle','off',...'MenuBar','none');%画坐标轴对象,显示原始图像h0=axes('position',[0.10.60.30.3]);%添加图像打开按钮h1=uicontrol(H,'Style','push',...'Position',[401008060],...'String','选择图片',...'FontSize',10,...'Call','op');%画坐标轴对象,显示经过预处理之后的图像h2=axes('position',[0.50.60.30.3]);%添加预处理按钮3h3=uicontrol(H,'Style','push',...'Position',[1401008060],...'String','二值化',...'FontSize',10,...'Call','preprocess');%添加识别按钮h4=uicontrol(H,'Style','push',...'Position',[2401008060],...'String','字母识别',...'FontSize',10,...'Call','recognize');%添加显示识别结果的文本框%添加训练神经网络按钮h6=uicontrol(H,'Style','push',...'Position',[3401008060],...'String','网络训练',...'FontSize',10,...'Call','Example1Tr');%预处理%preprocessp1=ones(16,16);4bw=im2bw(X,0.5);%转换成二值图像%用矩形框截取图像[i,j]=find(bw==0);imin=min(i);imax=max(i);jmin=min(j);jmax=max(j);bw1=bw(imin:imax,jmin:jmax);%调整比例,变换成16*16图像rate=16/max(size(bw1));bw1=imresize(bw1,rate);[i,j]=size(bw1);i1=round((16-i)/2);j1=round((16-j)/2);p1(i1+1:i1+i,j1+1:j1+j)=bw1;p1=-1.*p1+ones(16,16);%显示预处理的结果axes(h2);imshow(p1);%Example1Tr,训练网络5M=1;%人数N=26*M;%样本数%获取26个大写字母图像的数据forkk=0:N-1p1=ones(16,16);%初始化16*16的二值图像(全白)m=strcat(int2str(kk),'.bmp');%形成文件名x=imread(m,'bmp');%读取图像bw=im2bw(x,0.5);%转换成二值图像数据%用矩形框截取[i,j]=find(bw==0);%查找像素为黑的坐标%取边界坐标imin=min(i);imax=max(i);jmin=min(j);jmax=max(j);bw1=bw(imin:imax,jmin:jmax);%截取%调整比例,缩放成16*16的图像rate=16/max(size(bw1));bw1=imresize(bw1,rate);%会存在转换误差%将bw1转换成标准的16*16图像p1[i,j]=size(bw1);i1=round((16-i)/2);6j1=round((16-j)/2);p1(i1+1:i1+i,j1+1:j1+j)=bw1;p1=-1.*p1+ones(16,16);%将p1转换成输入向量form=0:15p(m*16+1:(m+1)*16,kk+1)=p1(1:16,m+1);endend%形成目标向量forkk=0:M-1forii=0:25t(kk+ii+1)=ii;endend%设置输入向量范围pr(1:256,1)=0;pr(1:256,2)=1;%创建两层BP神经网络,隐层有25个节点net=newff(pr,[251],{'logsig''purelin'},'traingdx','learngdm');net.trainParam.epochs=2500;net.trainParam.goal=0.001;net.trainParam.show=10;7net.trainParam.lr=0.05;%训练神经网络net=train(net,p,t);%存储训练好的神经网络%recognize,字符识别%生成向量形式M=figure('Color',[0.750.750.75],...'position',[200200400200],...'Name','基于BP神经网络的英文字母识别结果',...'NumberTitle','off',...'MenuBar','none');M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','请先训练网络',...'FontSize',12,...'call','delete(M(1))');form=0:15q(m*16+1:(m+1)*16,1)=p1(1:16,m+1);end%识别8[a,Pf,Af]=sim(net,q);a=round(a);switchacase0,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是A',...'FontSize',12,...'call',...'delete(M(1))');case1,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是B',...'FontSize',12,...'call',...'delete(M(1))');case2,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是C',...'FontSize',12,...'call',...'delete(M(1))');case3,M0=uicontrol(M,'Style','push',...9'Position',[1508013040],...'String','这个字母是D',...'FontSize',12,...'call',...'delete(M(1))');case4,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是E',...'FontSize',12,...'call',...'delete(M(1))');case5,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是F',...'FontSize',12,...'call',...'delete(M(1))');case6,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是G',...'FontSize',12,...'call',...10'delete(M(1))');case7,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是H',...'FontSize',12,...'call',...'delete(M(1))');case8,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是I',...'FontSize',12,...'call',...'delete(M(1))');case9,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是J',...'FontSize',12,...'call',...'delete(M(1))');case10,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是K',...11'FontSize',12,...'call',...'delete(M(1))');case11,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是L',...'FontSize',12,...'call',...'delete(M(1))');case12,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是M',...'FontSize',12,...'call',...'delete(M(1))');case13,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是N',...'FontSize',12,...'call',...'delete(M(1))');case14,M0=uicontrol(M,'Style','push',...12'Position',[1508013040],...'String','这个字母是O',...'FontSize',12,...'call',...'delete(M(1))');case15,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是P',...'FontSize',12,...'call',...'delete(M(1))');case16,M0=uicontrol(M,'Style','push',...'Position',[1508013040],...'String','这个字母是Q',...'FontSize',12,...'call',...'delete(M(1))');case17
本文标题:实验七基于神经网络的模式识别实验
链接地址:https://www.777doc.com/doc-2458027 .html