模式识别数字识别matlab

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

clearallclctongji=0;file1='E:\MATLABR2009aPortable\shuzi';%图片文件夹file2='.bmp';%文件类型r=0.5;%%%%%%%%%%%%%%%%%%训练样本$$$$$$$$$$$$$$$$$$$$fori=0:1:13;forj=1:30a=int2str(i);b=int2str(j);A=imread(strcat(file1,a,'\',b,file2));%读文件A_bw=im2bw(A,r);%二值化[m,n]=size(A_bw);%%%%%%%%%%%矩阵分成4块,并统计每块1的个数%%%%%%%%%%%%x1=0;fork=(1:m/2)forp=(1:n/2)ifA_bw(k,p)==1x1=x1+1;endendendx2=0;fork=(m/2+1:m)forp=(1:n/2)ifA_bw(k,p)==1x2=x2+1;endendendx3=0;fork=(1:m/2)forp=(n/2+1:n)ifA_bw(k,p)==1x3=x3+1;endendendx4=0;fork=(m/2+1:m)forp=(n/2+1:n)ifA_bw(k,p)==1x4=x4+1;endendends(j,:)=[x1x2x3x4];%形成4维向量endforq=1:4temp=mean(s(:,q));%求列平均值M(q)=temp';%类中心endS(:,i+1)=M';%类中心矩阵9*14end%%%%%%%%%%%%%%测试样本%%%%%%%%%%%%%%%%%%%%%error=0;forii=0:13forjj=31:40a=int2str(ii);b=int2str(jj);A=imread(strcat(fil1,a,'\',b,fil2));%读测试文件A_bw=im2bw(A,r);%二值化[m,n]=size(A_bw);%矩阵分块x1=0;fork=(1:m/2)forp=(1:n/2)ifA_bw(k,p)==1x1=x1+1;endendendx2=0;fork=(m/2+1:m)forp=(1:n/2)ifA_bw(k,p)==1x2=x2+1;endendendx3=0;fork=(1:m/2)forp=(n/2+1:n)ifA_bw(k,p)==1x3=x3+1;endendendx4=0;fork=(m/2+1:m)forp=(n/2+1:n)ifA_bw(k,p)==1x4=x4+1;endendendt=[x1,x2,x3,x4];%形成4维特征向量tt=[x1,x2,x3,x4];d_1=0;d_3=inf;fory=1:14d_1=sum((t'-S(:,y)).^2);%计算欧式距离%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%sum1=0;forj=1:4sum1=sum1+(tt(j)-S(j,y))^2;endDistance(y)=sqrt(sum1);%求测试样本与各个数字特征的欧式距离%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ifd_3d_1%计算最小距离d_3=d_1;ify=10d_2=int2str(y-1);endify==11n='A';d_2=strcat(n+y-11);endify==12n='C';d_2=strcat(n+y-12);endify==13n='D';d_2=strcat(n+y-13);endify==14n='E';d_2=strcat(n+y-14);enddd_1(ii+1,jj-30)=d_2;%输出判决结果end%ifendend%yend1:14%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%[C,class]=min(Distance);%欧氏距离最小的哪一类Class=class-1;if(ii==Class)%统计识别正确的个数tongji=tongji+1;elseerror=error+1;end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%end%jj31:40end%iiend0:13disp(dd_1)disp('识别率为=')recognize=(tongji/140)*100%识别率disp('错误率为=')rize=(error/140)*100%识别率tongjierror

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功