您好,欢迎访问三七文档
%**************************基于散布本征空间贝叶斯分类器的手写体数字识别系统************************clear;tic[train_images,train_labels]=loadMNIST('train-images.idx3-ubyte','train-labels.idx1-ubyte',30000,0);%将数据图像的像素值进行二值化处理fori=1:1:30000forj=1:1:784iftrain_images(i,j)0&&train_images(i,j)50;train_images(i,j)=1;elseiftrain_images(i,j)=50&&train_images(i,j)256;train_images(i,j)=2;elsetrain_images(i,j)=0;endendend%将28*28大小的像素快中临近的16个像素值合并为1个,每个数字的像素值用7*7矩阵表示Z=zeros(28,28,30000);X=zeros(7,7,30000);Y=zeros(30000,25);fork=1:1:30000fori=1:1:28forj=1:1:28Z(i,j,k)=train_images(k,j+28*(i-1));endendendfork=1:1:30000fori=1:1:7forj=1:1:7X(i,j,k)=Z(4*i-3,4*j-3,k)+Z(4*i-3,4*j-2,k)+Z(4*i-3,4*j-1,k)+Z(4*i-3,4*j,k)+Z(4*i-2,4*j-3,k)+Z(4*i-2,4*j-2,k)+Z(4*i-2,4*j-1,k)+Z(4*i-2,4*j,k)+Z(4*i-1,4*j-3,k)+Z(4*i-1,4*j-2,k)+Z(4*i-1,4*j-1,k)+Z(4*i-1,4*j,k)+Z(4*i,4*j-3,k)+Z(4*i,4*j-2,k)+Z(4*i,4*j-1,k)+Z(4*i,4*j,k);endendend%删掉第1行与第1列最后1行最后1列X=X(2:6,2:6,:);%将5*5矩阵变成一维向量,用Y表示fork=1:1:30000fori=1:1:5forj=1:1:5Y(k,j+5*(i-1))=X(i,j,k);endendend%去掉第15个像素特征值%Y(:,15)=[];%*****************************************************%统计训练样本中0-9数字的数目并将数字置于相应的分类矩阵中%*****************************************************N_0=0;N_1=0;N_2=0;N_3=0;N_4=0;N_5=0;N_6=0;N_7=0;N_8=0;N_9=0;%初始化0-9数字数目的变量Tr_0=zeros([],25);Tr_1=zeros([],25);Tr_2=zeros([],25);Tr_3=zeros([],25);Tr_4=zeros([],25);Tr_5=zeros([],25);Tr_6=zeros([],25);Tr_7=zeros([],25);Tr_8=zeros([],25);Tr_9=zeros([],25);fori=1:1:30000iftrain_labels(i,1)==0N_0=N_0+1;Tr_0(N_0,:)=Y(i,:);elseiftrain_labels(i,1)==1N_1=N_1+1;Tr_1(N_1,:)=Y(i,:);elseiftrain_labels(i,1)==2N_2=N_2+1;Tr_2(N_2,:)=Y(i,:);elseiftrain_labels(i,1)==3N_3=N_3+1;Tr_3(N_3,:)=Y(i,:);elseiftrain_labels(i,1)==4N_4=N_4+1;Tr_4(N_4,:)=Y(i,:);elseiftrain_labels(i,1)==5N_5=N_5+1;Tr_5(N_5,:)=Y(i,:);elseiftrain_labels(i,1)==6N_6=N_6+1;Tr_6(N_6,:)=Y(i,:);elseiftrain_labels(i,1)==7N_7=N_7+1;Tr_7(N_7,:)=Y(i,:);elseiftrain_labels(i,1)==8N_8=N_8+1;Tr_8(N_8,:)=Y(i,:);elseN_9=N_9+1;Tr_9(N_9,:)=Y(i,:);endendtrain_number=[N_0N_1N_2N_3N_4N_5N_6N_7N_8N_9];total_train={Tr_0,Tr_1,Tr_2,Tr_3,Tr_4,Tr_5,Tr_6,Tr_7,Tr_8,Tr_9};%*********************************测试***********************************[test_images,test_labels]=loadMNIST('t10k-images.idx3-ubyte','t10k-labels.idx1-ubyte',5000,0);%对测试数据处理fori=1:1:5000forj=1:1:784iftest_images(i,j)0&&test_images(i,j)50;test_images(i,j)=1;elseiftest_images(i,j)=50&&test_images(i,j)256;test_images(i,j)=2;elsetest_images(i,j)=0;endendendZ_r=zeros(28,28,30000);X_r=zeros(7,7,30000);Y_r=zeros(5000,25);test=zeros(5000,10);Re=zeros(5000,1);%将28*28大小的像素快中临近的16个像素值合并为1个,每个数字的像素值用7*7矩阵表示fork=1:1:5000fori=1:1:28forj=1:1:28Z_r(i,j,k)=test_images(k,j+28*(i-1));endendendfork=1:1:5000fori=1:1:7forj=1:1:7X_r(i,j,k)=Z_r(4*i-3,4*j-3,k)+Z_r(4*i-3,4*j-2,k)+Z_r(4*i-3,4*j-1,k)+Z_r(4*i-3,4*j,k)+Z_r(4*i-2,4*j-3,k)+Z_r(4*i-2,4*j-2,k)+Z_r(4*i-2,4*j-1,k)+Z_r(4*i-2,4*j,k)+Z_r(4*i-1,4*j-3,k)+Z_r(4*i-1,4*j-2,k)+Z_r(4*i-1,4*j-1,k)+Z_r(4*i-1,4*j,k)+Z_r(4*i,4*j-3,k)+Z_r(4*i,4*j-2,k)+Z_r(4*i,4*j-1,k)+Z_r(4*i,4*j,k);endendendX_r=X_r(2:6,2:6,:);fork=1:1:5000fori=1:1:5forj=1:1:5Y_r(k,j+5*(i-1))=X_r(i,j,k);endendend%%*****************************************************%%统计测试样本中0-9数字的数目并将数字置于相应的分类矩阵中%%*****************************************************N_0=0;N_1=0;N_2=0;N_3=0;N_4=0;N_5=0;N_6=0;N_7=0;N_8=0;N_9=0;%初始化0-9数字数目的变量Te_0=zeros([],25);Te_1=zeros([],25);Te_2=zeros([],25);Te_3=zeros([],25);Te_4=zeros([],25);Te_5=zeros([],25);Te_6=zeros([],25);Te_7=zeros([],25);Te_8=zeros([],25);Te_9=zeros([],25);fori=1:1:5000iftest_labels(i,1)==0N_0=N_0+1;Te_0(N_0,:)=Y_r(i,:);elseiftest_labels(i,1)==1N_1=N_1+1;Te_1(N_1,:)=Y_r(i,:);elseiftest_labels(i,1)==2N_2=N_2+1;Te_2(N_2,:)=Y_r(i,:);elseiftest_labels(i,1)==3N_3=N_3+1;Te_3(N_3,:)=Y_r(i,:);elseiftest_labels(i,1)==4N_4=N_4+1;Te_4(N_4,:)=Y_r(i,:);elseiftest_labels(i,1)==5N_5=N_5+1;Te_5(N_5,:)=Y_r(i,:);elseiftest_labels(i,1)==6N_6=N_6+1;Te_6(N_6,:)=Y_r(i,:);elseiftest_labels(i,1)==7N_7=N_7+1;Te_7(N_7,:)=Y_r(i,:);elseiftest_labels(i,1)==8N_8=N_8+1;Te_8(N_8,:)=Y_r(i,:);elseN_9=N_9+1;Te_9(N_9,:)=Y_r(i,:);endendtest_number=[N_0N_1N_2N_3N_4N_5N_6N_7N_8N_9];total_test={Te_0,Te_1,Te_2,Te_3,Te_4,Te_5,Te_6,Te_7,Te_8,Te_9};%对训练样本和测试样本进行将维处理,先求其协方差矩阵,然后求其特征值和特征向量并进行排序,得到转换矩阵Covariance_tr=zeros(25,25);eigenvector=zeros(25,25,10);form=1:10Covariance_tr(:,:)=cov(total_train{m});[eigenvector(:,:,m)eigenvalue]=eig(Covariance_tr,'nobalance');%特征值从小到大排列endlower_dimension=18;%25降维到12维,可自行设置lower_D_train=cell(10,1);lower_D_test=cell(10,1);form=1:10%对训练数据降低维度forn=1:train_number(m)lower_D_train{m}(n,:)=total_train{m}(n,:)*eigenvector(:,25-lower_dimension+1:25,m);end%对测试数据降低维度forn=1:test_number(m)lower_D_test{m}(n,:)=total_test{m}(n,:)*eigenvector(:,25-lower_dimension+1:25,m);endend%贝叶斯方法,由先验概率和类条件概率求后验概率S=zeros(lower_dimension,lower_dimension,10);X_R=zeros(10,lower_dimension);S_R=zeros(lower_dimension,lower_dimension,10);%求0-9数据每一类的协方差矩阵,用S表示S(:,:,1)=cov(lower_D_train{1});S(:,:,2)=cov(lower_D_train{2});S(:,:,3)=cov(lower_D_train{3});S(:,:,4)=cov(lower_D_train{4});S(:,:,5)=
本文标题:手写体数字识别程序
链接地址:https://www.777doc.com/doc-2414761 .html