声明:本文转载自http://blog.csdn.net/light_lj/article/details/26747269
上一篇介绍了人脸识别的主要算法,为了便于操作和,使界面更加友善,在之前的基础上做了GUI开发,界面如下:
- global h_axes1;
- global h_axes2;
- global edit2;
- h_f=figure('name','人脸识别系统','position',[300,200,600,400]);
- clf reset;
- set(h_f, 'defaultuicontrolfontsize' ,12);
- set(h_f, 'defaultuicontrolfontname' , '宋体' );
- h_axes1=axes('parent',h_f,'position',[0.2 0.28 0.25 0.56],'Unit','normalized','visible','off');
- h_axes2=axes('parent',h_f,'position',[0.55 0.28 0.25 0.56],'Unit','normalized','visible','off');
- figcolor=get(h_f,'color');
- edit2=uicontrol(h_f,'style','text','position',[150,330,300,40],'backgroundcolor',figcolor);
- button_open=uicontrol(h_f,'style','push','string','选择照片','position',[250 50 100 50],'callback','GUIopen');
- button_recg=uicontrol(h_f,'style','push','string','测试准确率','position',[100 50 100 50],'callback','face');
- button_match=uicontrol(h_f,'style','push','string','图像匹配','position',[400 50 100 50],'callback','GUIrecg');
- clc,clear
- npersons=40;%选取40个人的脸
- global imgrow;
- global imgcol;
- global edit2
- imgrow=112;
- imgcol=92;
- set(edit2,'string','读取训练数据......')%显示在句柄为edit2的文本框里
- drawnow %更新窗口的内容,不然程序结束时才会显示,这样只能看到最后一句
- f_matrix=ReadFace(npersons,0);%读取训练数据
- nfaces=size(f_matrix,1);%样本人脸的数量
- set(edit2,'string','训练数据PCA特征提取......')
- drawnow
- mA=mean(f_matrix);
- k=20;%降维至20维
- [pcaface,V]=fastPCA(f_matrix,k,mA);%主成分分析法特征提取
- set(edit2,'string','训练数据规范化......')
- drawnow
- lowvec=min(pcaface);
- upvec=max(pcaface);
- scaledface = scaling( pcaface,lowvec,upvec);
- set(edit2,'string','SVM样本训练......')
- drawnow
- gamma=0.0078;
- c=128;
- multiSVMstruct=multiSVMtrain( scaledface,npersons,gamma,c);
- save('recognize.mat','multiSVMstruct','npersons','k','mA','V','lowvec','upvec');
- set(edit2,'string','读取测试数据......')
- drawnow
- [testface,realclass]=ReadFace(npersons,1);
- set(edit2,'string','测试数据特征降维......')
- drawnow
- m=size(testface,1);
- for i=1:m
- testface(i,:)=testface(i,:)-mA;
- end
- pcatestface=testface*V;
- set(edit2,'string','测试数据规范化......')
- drawnow
- scaledtestface = scaling( pcatestface,lowvec,upvec);
- set(edit2,'string','SVM样本分类......')
- drawnow
- class= multiSVM(scaledtestface,multiSVMstruct,npersons);
- set(edit2,'string','测试完成!')
- accuracy=sum(class==realclass)/length(class);
- msgbox(['识别准确率:',num2str(accuracy*100),'%。'])
- global h_axes1
- [filename,pathname]=uigetfile({'*.pgm';'*.jpg';'*.tif';'*.*'},'请选择一张用于识别的照片');
- if filename==0
- msgbox('请选择一张照片文件')
- else
- filepath=[pathname,filename];
- axes(h_axes1);
- imshow(imread(filepath));
- end
- global h_axes1
- global h_axes2
- global edit2
- load('recognize.mat');
- set(edit2,'string','读取测试数据......')
- drawnow
- disp('读取测试数据...')
- disp('.................................................')
- img=getimage(h_axes1);%获得之前选中的照片的信息
- if isempty(img)
- msgbox('请先选择一张图片!')
- break
- end
- testface=img(:)';
- set(edit2,'string','测试数据降维......')
- drawnow
- disp('测试数据特征降维...')
- disp('.................................................')
- Z=double(testface)-mA;
- pcatestface=Z*V;
- set(edit2,'string','测试特征数据规范化......')
- drawnow
- disp('测试特征数据规范化...')
- disp('.................................................')
- scaledtestface=-1+(pcatestface-lowvec)./(upvec-lowvec)*2;
- set(edit2,'string','SVM样本识别......')
- drawnow
- disp('SVM样本识别...')
- disp('.................................................')
- voting=zeros(1,npersons);
- for i=1:npersons-1
- for j=i+1:npersons
- class=svmclassify(multiSVMstruct{i}{j},scaledtestface);
- voting(i)=voting(i)+(class==1);
- voting(j)=voting(j)+(class==0);
- end
- end
- [~,class]=max(voting);
- set(edit2,'string','识别完成!')
- drawnow
- axes(h_axes2);
- imshow(imread(['E:\ORL_face\s',num2str(class),'\1.pgm']));
- msgbox(['样本识别为第',num2str(class),'个人'])
转载请您尊重作者的劳动,完整保留文章出处以及文章链接,谢谢您的支持! 欢迎您参与讨论~让我们共同进步~