数学建模论文作业(人脸识别程序代码)

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

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

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

资源描述

1附录附录1Matlab人脸定位程序%%%%%%%%%%%%%%%%R型聚类分析%%%%%%%%%%%%%%functionRer=Re()clearallcloseallclcnum_pic=1;path_pic=strcat('E:\01_LYC\00_WHUT\04_lab\',num2str(num_pic),'.jpg');I=imread(path_pic);figure;imshow(I);holdon;title('原始图片','FontWeight','Bold');O=rgb2ntsc(I);G=O(:,:,2);[mn]=size(G);U=zeros(m,n);fori=1:mforj=1:nifG(i,j)0.03&&G(i,j)0.20U(i,j)=1;endendendfigure;imshow(U);holdon;title('二值化图片','FontWeight','Bold');sr=strel('disk',4);C=imclose(U,sr);C_morph=bwmorph(C,'erod',0.8);[n1,n2]=size(C_morph);r=floor(n1/100);c=floor(n2/100);x1=1;x2=r;s=r*c;C1=C_morph;fori=1:100y1=1;y2=c;forj=1:100loc=find(C1(x1:x1+r-1,y1:y1+c-1)==0);[p,q]=size(loc);pr=p/s*100;ifpr=152C1(x1:x1+r,y1:y1+c)=1;elseC1(x1:x1+r,y1:y1+c)=0;endy1=y1+c;y2=y2+c;endx1=x1+r;x2=x2+r;endfigure;imshow(C1);holdon;title('增强图片','FontWeight','Bold');[L,num]=bwlabel(C1,4);B=regionprops(L,'area');Se=[B.Area];[Se1,Se2]=size(Se);stats=regionprops(L,'BoundingBox');Bd=cat(1,stats.BoundingBox);[n1,n2]=size(C_morph);mx=n1*n2;P=[];num_area=1;fori=1:Se2ifSe(1,i)/mx=0.005|Se(1,i)/(Bd(i,3)*Bd(i,4))=0.50;Se(1,i)=0;elseif(Bd(i,3)/Bd(i,4))1.8;P(num_area)=i;num_area=num_area+1;endendnum_area=num_area-1;figure;imshow(I);holdon;title('标记图像','FontWeight','Bold');I_gray=rgb2gray(I);fori=1:num_areai_position=P(i);rectangle('Position',Bd(i_position,:),'EdgeColor','r','LineWidth',3);endfori=1:num_areai_position=P(i);x1=floor(Bd(i_position,1));ifx1==0;x1=1;endx2=floor(Bd(i_position,1)+Bd(i_position,3));y1=floor(Bd(i_position,2));3ify1==0;y1=1;endy2=floor(Bd(i_position,2)+Bd(i_position,4));I_select=I_gray(y1:y2,x1:x2);I_select=imresize(I_select,[112,92]);imwrite(I_select,strcat('E:\ORL\A',num2str(num_pic),'\',num2str(i),'.pgm'));endend4附录2Matlab灰度投影程序functionprojection()clearallclccloseall%%读入图像数据I=imread('E:\ORL\A2\3.pgm');[mn]=size(I);figure;imshow(I);fory=1:nS(y)=sum(I(1:m,y));S(y)=S(y)/n;endy=1:n;figuresubplot(211),plot(y,S(y));title('垂直投影');forx=1:mS(x)=sum(I(x,:));S(x)=S(x)/m;endx=1:m;subplot(212),plot(x,S(x));title('水平投影');end5附录3Matlab人脸匹配程序clearallclccloseallallsamples=[];%所有训练图像fori=1:40forj=1:5a=imread(strcat('E:\ORL\s',num2str(i),'\',num2str(j),'.pgm'));iflength(size(a))2%彩色的图片先转换为黑白图片a=rgb2gray(a);endvecimg=double(reshape(a,1,size(a,1)*size(a,2)));allsamples=cat(1,allsamples,vecimg);%得到M*N矩阵T中每一行数据代表一张图片,其中M=100b=a(1:112*92);endendforj=1:13a1=imread(strcat('E:\ORL\A0\',num2str(j),'.pgm'));iflength(size(a1))2%彩色的图片先转换为黑白图片a1=rgb2gray(a1);endvecimg=double(reshape(a1,1,size(a1,1)*size(a1,2)));allsamples=cat(1,allsamples,vecimg);%得到M*N矩阵T中每一行数据代表一张图片,其中M=100b=a(1:112*92);endMeanFace=mean(allsamples,1);%平均图片,1×NTrainNumber=213;fori=1:TrainNumberMeanNormFaces(i,:)=allsamples(i,:)-MeanFace;end;%获取特征值和特征向量sigmA=MeanNormFaces*MeanNormFaces';%M*M阶矩阵求取A矩阵[v,d]=eig(sigmA);d1=diag(d);eigenValue=flipud(d1);%特征值沿纵向上下翻转以完成特征值降序排列vsort=fliplr(v);v1=MeanNormFaces'*v;eigenVector=fliplr(v1);%特征向量沿水平左右翻转%以下选择90%的能量不需要所有的人脸,提取出能体现90%的特征值就可以,此处提取出71个EigenFaces=[];SumOfAllEigenValue=sum(eigenValue(:));TmpSumOfEigenValue=0;fori=1:size(eigenValue,1)6TmpSumOfEigenValue=TmpSumOfEigenValue+eigenValue(i,1);ChooseEigenValueNum=i;if(TmpSumOfEigenValue/SumOfAllEigenValue0.90)%默认计贡献率达到90%以上即可break;endendfori=1:ChooseEigenValueNumEigenFaces(i,:)=eigenVector(:,i)';end%%%%%%%%%%%%%%%%%%%%%%%%ProjectingcenteredimagevectorsintofacespaceProjectedImages=[];%numberofeigenfacesTrain_Number=size(MeanNormFaces,1);fori=1:Train_Numbertemp=(EigenFaces*MeanNormFaces(i,:)')';%ProjectionofcenteredimagesintofacespaceProjectedImages(i,:)=temp;%eachrowcorrespondstoafeature%一行是一个特征值对应的系数200个图片每个图片有71个坐标end%%%%%%%%%%%%%%%%%%%%%%%%ExtractingthePCAfeaturesfromtestimageInputImage=imread('E:\ORL\A1\1.pgm');iflength(size(InputImage))2%彩色的图片先转换为黑白图片InputImage=rgb2gray(InputImage);endVecInput=reshape(InputImage,1,size(InputImage,1)*size(InputImage,2));MeanNormInput=double(VecInput)-MeanFace;%CenteredtestimageProjectedTestImage=(EigenFaces*MeanNormInput')';%Testimagefeaturevector%%%%%%%%%%%%%%%%%%%%%%%%CalculatingEuclideandistances%欧拉距离最小的测试图片与训练图片是同一个人Euc_dist=[];fori=1:Train_Numbertemp=(norm(ProjectedTestImage-ProjectedImages(i,:)))^2;%norm为求范数,默认是2范数及平方和开方Euc_dist=[Euc_disttemp];end[Euc_dist_min,Recognized_index]=min(Euc_dist);OutputName=strcat('s',int2str((Recognized_index-1)/5+1),'class');figure,subplot(121);imshow(InputImage,[]);title('输入人脸');subplot(122);ifmin(Euc_dist)1e5imshow(reshape((MeanNormFaces(Recognized_index,:)+MeanFace),112,92),[]);title('最相似人脸');else7nop=imread('E:\01_LYC\00_WHUT\04_lab\X.jpg');nop=rgb2gray(nop);imshow(nop);title('无最相似人脸');end

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

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

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

×
保存成功