實(shí)驗(yàn)七:基于神經(jīng)網(wǎng)絡(luò)的模式識(shí)別實(shí)驗(yàn).doc
實(shí)驗(yàn)七:基于神經(jīng)網(wǎng)絡(luò)的模式識(shí)別實(shí)驗(yàn)一、 實(shí)驗(yàn)?zāi)康睦斫釨P神經(jīng)網(wǎng)絡(luò)和離散Hopfield神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)和原理,掌握反向傳播學(xué)習(xí)算法對(duì)神經(jīng)元的訓(xùn)練過(guò)程,了解反向傳播公式。通過(guò)構(gòu)建BP網(wǎng)絡(luò)和離散Hopfield網(wǎng)絡(luò)模式識(shí)別實(shí)例,熟悉前饋網(wǎng)絡(luò)和反饋網(wǎng)絡(luò)的原理及結(jié)構(gòu)。綜合掌握模式識(shí)別的原理,了解識(shí)別過(guò)程的程序設(shè)計(jì)方法。二、 實(shí)驗(yàn)內(nèi)容熟悉模式識(shí)別的理論方法,用選擇一種合適的識(shí)別方法,對(duì)圖像中的字符(英文字母)進(jìn)行識(shí)別,能夠區(qū)分出不同的形態(tài)的26個(gè)字母。在Matlab中,采用BP神經(jīng)網(wǎng)絡(luò),對(duì)讀取的數(shù)據(jù)進(jìn)行訓(xùn)練,進(jìn)而識(shí)別。1. 程序設(shè)計(jì)(1)程序各流程圖實(shí)驗(yàn)中主程序流程圖如圖4-1所示:圖4-1 主程序流程圖其中圖像預(yù)處理的流程如圖4-2 所示:圖像輸入灰度轉(zhuǎn)化圖像二值化圖像分割歸一化調(diào)整調(diào)整比例顯示預(yù)處理結(jié)果圖4-2 圖像預(yù)處理的流程神經(jīng)網(wǎng)絡(luò)訓(xùn)練的具體流程如圖4-3 所示:獲取圖像數(shù)據(jù)創(chuàng)建神經(jīng)網(wǎng)絡(luò)訓(xùn)練存儲(chǔ)訓(xùn)練好的神經(jīng)網(wǎng)絡(luò)圖4-3 神經(jīng)網(wǎng)絡(luò)訓(xùn)練流程(2)程序清單%形成用戶界面clear all;%添加圖形窗口H=figure(Color,0.85 0.85 0.85,. position,400 300 500 400,. Name,基于BP神經(jīng)網(wǎng)絡(luò)的英文字母識(shí)別,. NumberTitle,off,. MenuBar,none);%畫(huà)坐標(biāo)軸對(duì)象,顯示原始圖像h0=axes(position,0.1 0.6 0.3 0.3);%添加圖像打開(kāi)按鈕h1=uicontrol(H,Style,push,. Position,40 100 80 60,. String,選擇圖片,. FontSize,10,. Call,op);%畫(huà)坐標(biāo)軸對(duì)象,顯示經(jīng)過(guò)預(yù)處理之后的圖像h2=axes(position,0.5 0.6 0.3 0.3);%添加預(yù)處理按鈕h3=uicontrol(H,Style,push,. Position,140 100 80 60,. String,二值化,. FontSize,10,. Call,preprocess);%添加識(shí)別按鈕h4=uicontrol(H,Style,push,. Position,240 100 80 60,. String,字母識(shí)別,. FontSize,10,. Call,recognize);%添加顯示識(shí)別結(jié)果的文本框%添加訓(xùn)練神經(jīng)網(wǎng)絡(luò)按鈕h6=uicontrol(H,Style,push,. Position,340 100 80 60,. String,網(wǎng)絡(luò)訓(xùn)練,. FontSize,10,.Call,Example1Tr);%預(yù)處理%preprocessp1=ones(16,16);bw=im2bw(X,0.5);%轉(zhuǎn)換成二值圖像%用矩形框截取圖像i,j=find(bw=0);imin=min(i);imax=max(i);jmin=min(j);jmax=max(j);bw1=bw(imin:imax,jmin:jmax);%調(diào)整比例,變換成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);%顯示預(yù)處理的結(jié)果axes(h2);imshow(p1);%Example1Tr,訓(xùn)練網(wǎng)絡(luò)M=1;%人數(shù)N=26*M;%樣本數(shù)%獲取26個(gè)大寫(xiě)字母圖像的數(shù)據(jù)for kk=0:N-1 p1=ones(16,16);%初始化16*16的二值圖像(全白) m=strcat(int2str(kk),.bmp);%形成文件名 x=imread(m,bmp);%讀取圖像 bw=im2bw(x,0.5);%轉(zhuǎn)換成二值圖像數(shù)據(jù) %用矩形框截取 i,j=find(bw=0);%查找像素為黑的坐標(biāo) %取邊界坐標(biāo) imin=min(i); imax=max(i); jmin=min(j); jmax=max(j); bw1=bw(imin:imax,jmin:jmax);%截取 %調(diào)整比例,縮放成16*16的圖像 rate=16/max(size(bw1); bw1=imresize(bw1,rate);%會(huì)存在轉(zhuǎn)換誤差 %將bw1轉(zhuǎn)換成標(biāo)準(zhǔn)的16*16圖像p1 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); %將p1轉(zhuǎn)換成輸入向量 for m=0:15 p(m*16+1:(m+1)*16,kk+1)=p1(1:16,m+1); endend%形成目標(biāo)向量for kk=0:M-1 for ii=0:25 t(kk+ii+1)=ii; endend%設(shè)置輸入向量范圍pr(1:256,1)=0;pr(1:256,2)=1;%創(chuàng)建兩層BP神經(jīng)網(wǎng)絡(luò),隱層有25個(gè)節(jié)點(diǎn)net=newff(pr,25 1,logsig purelin,traingdx,learngdm);net.trainParam.epochs=2500;net.trainParam.goal=0.001;net.trainParam.show=10;net.trainParam.lr=0.05;%訓(xùn)練神經(jīng)網(wǎng)絡(luò)net=train(net,p,t);%存儲(chǔ)訓(xùn)練好的神經(jīng)網(wǎng)絡(luò)%recognize,字符識(shí)別%生成向量形式M=figure(Color,0.75 0.75 0.75,. position,200 200 400 200,. Name,基于BP神經(jīng)網(wǎng)絡(luò)的英文字母識(shí)別結(jié)果,. NumberTitle,off,. MenuBar,none);M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,請(qǐng)先訓(xùn)練網(wǎng)絡(luò),. FontSize,12,. call,delete(M(1) ); for m=0:15 q(m*16+1:(m+1)*16,1)=p1(1:16,m+1);end%識(shí)別a,Pf,Af=sim(net,q);a=round(a);switch a case 0,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是A,. FontSize,12,. call,. delete(M(1); case 1,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是B,. FontSize,12,. call,. delete(M(1); case 2,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是C,. FontSize,12,. call,. delete(M(1); case 3,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是D,. FontSize,12,. call,. delete(M(1); case 4,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是E,. FontSize,12,. call,. delete(M(1); case 5,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是F,. FontSize,12,. call,. delete(M(1); case 6,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是G,. FontSize,12,. call,. delete(M(1); case 7,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是H,. FontSize,12,. call,. delete(M(1); case 8,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是I,. FontSize,12,. call,. delete(M(1); case 9,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是J,. FontSize,12,. call,. delete(M(1); case 10,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是K,. FontSize,12,. call,. delete(M(1); case 11,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是L,. FontSize,12,. call,. delete(M(1); case 12,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是M,. FontSize,12,. call,. delete(M(1); case 13,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是N,. FontSize,12,. call,. delete(M(1); case 14,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是O,. FontSize,12,. call,. delete(M(1); case 15,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是P,. FontSize,12,. call,. delete(M(1); case 16,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是Q,. FontSize,12,. call,. delete(M(1); case 17,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是R,. FontSize,12,. call,. delete(M(1); case 18,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是S,. FontSize,12,. call,. delete(M(1); case 19,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是T,. FontSize,12,. call,. delete(M(1); case 20,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是U,. FontSize,12,. call,. delete(M(1); case 21,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是V,. FontSize,12,. call,. delete(M(1); case 22,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是W,. FontSize,12,. call,. delete(M(1); case 23,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是X,. FontSize,12,. call,. delete(M(1); case 24,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是Y,. FontSize,12,. call,. delete(M(1); case 25,M0=uicontrol(M,Style,push,. Position,150 80 130 40,. String,這個(gè)字母是Z,. FontSize,12,. call,. delete(M(1); End%op%讀取圖像文件filename,pathname=uigetfile(*.bmp;*.jpg;. *.gif;*.*,. Pick an Image File);X=imread(pathname,filename);%顯示圖像axes(h0);%將h0設(shè)置為當(dāng)前坐標(biāo)軸句柄imshow(X);%在h0上顯示原始圖像2. 實(shí)驗(yàn)結(jié)果圖5-1為實(shí)驗(yàn)的主窗口,用于選擇圖片,進(jìn)行二值化,網(wǎng)絡(luò)訓(xùn)練和字符識(shí)別。運(yùn)行程序后,將彈出次窗口,選擇圖片后,先進(jìn)行二值化處理,然后在進(jìn)行網(wǎng)絡(luò)訓(xùn)練,最后字符識(shí)別。圖5-1 主窗口圖5-2 網(wǎng)絡(luò)訓(xùn)練圖5-3 字符識(shí)別結(jié)果三、 實(shí)驗(yàn)體會(huì)理解了BP神經(jīng)網(wǎng)絡(luò)和離散Hopfield神經(jīng)網(wǎng)絡(luò)的結(jié)構(gòu)和原理,掌握了反向傳播學(xué)習(xí)算法對(duì)神經(jīng)元的訓(xùn)練過(guò)程,了解了反向傳播公式。通過(guò)構(gòu)建了BP網(wǎng)絡(luò)和離散Hopfield網(wǎng)絡(luò)模式識(shí)別的實(shí)例,熟悉了前饋網(wǎng)絡(luò)和反饋網(wǎng)絡(luò)的原理及結(jié)構(gòu)。掌握了模式識(shí)別的原理,了解了識(shí)別過(guò)程的程序設(shè)計(jì)方法。