您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > MATLAB下人脸图像的PCA重构matlab代码
MATLAB下人脸图像的PCA重构matlab代码主函数clc;clearall;newimage=double(zeros(112*6,92*5));[M,sort_V,avg_face]=averageface();%返回去均值的矩阵和协方差,以及排列好的特征向量k=1;new_sort_V=sort_V(1:10304,1:30);%取特征向量的前30个,并进行显示fori=1:112:5*112+1forj=1:92:4*92+1newimage(i:i+111,j:j+91)=reshape(new_sort_V(:,k),112,92);k=k+1;endendminum=min(min(newimage));%对特征向量进行标定maxum=max(max(newimage));d=maxum-minum;newimage(:,:)=newimage(:,:)-minum;newimage(:,:)=newimage(:,:)/d;newimage(:,:)=newimage(:,:)*255;newimage=uint8(newimage);subplot(1,3,1);title('特征向量图');imshow(newimage);%显示前30个特征向量avg_face=uint8(avg_face);subplot(1,3,2)title('平均脸');imshow(avg_face);subplot(1,3,3)[tyxs,re_newimage]=touyingxishu(sort_V,avg_face);imshow(re_newimage);子函数function[]=img2txt()maindir='D:\Image';subdir=dir(maindir);%先确定子文件夹m=1;k=1;fori=1:length(subdir)if(isequal(subdir(i).name,'.')||isequal(subdir(i).name,'..')||~subdir(i).isdir)%如果不是目录跳过continue;endsubdirpath=fullfile(maindir,subdir(i).name,'*.pgm');images=dir(subdirpath);%在这个子文件夹下找后缀为pgm的文件%遍历每张图片forj=1:length(images)imagepath=fullfile(maindir,subdir(i).name,images(j).name);imgdata=imread(imagepath);%这里进行你的读取操作[row,col]=size(imgdata);%将数据写入txt中fid=fopen(['C:\Users\Administrator\Desktop\编程实验材料\att_faces','\s',int2str(m),'\',int2str(k),'.txt'],'w');%新建txt文件存储图像的坐标和灰度值forI=1:rowforJ=1:colfprintf(fid,'%03d\t',imgdata(I,J));endfprintf(fid,'\n');endfclose(fid);k=k+1;endm=m+1;k=1;endfunction[M,sort_V,avg_face]=averageface()big_array=double(zeros(10304,400));new_big_array=double(zeros(10304,400));avg_face=double(zeros(112,92));i=1;form=1:40;fork=1:10FileName=['C:\Users\Administrator\Desktop\编程实验材料\att_faces','\s',int2str(m),'\',int2str(k),'.txt'];p=load(FileName);big_array(:,i)=reshape(p,10304,1);%将400张脸转换成10304*400的大矩阵new_p=reshape(p,112,92);avg_face(:,:)=avg_face(:,:)+new_p(:,:)/400;%求平均脸i=i+1;endendM=mean(big_array);%对矩阵的每一列求均值fori=1:400new_big_array(:,i)=big_array(:,i)-M(i);%去均值后的矩阵end%用SVD求特征向量C0=new_big_array'*new_big_array;%求低维度的协方差[V0,~]=eig(C0);k=0;V=new_big_array*V0;%特征向量fori=1:400%将特征向量进行归一化10304*400forj=1:10304k=k+V(j,i)^2;endk=k^(0.5);V(1:10304,i)=V(1:10304,i)/k;k=0;endsort_V=double(zeros(10304,400));k=400;fori=1:400%将特征向量的列按特征值从大到小排列好sort_V(1:10304,i)=V(1:10304,k);k=k-1;endfunction[tyxs,re_newimage]=touyingxishu(sort_V,avg_face)FileName=['C:\Users\Administrator\Desktop\编程实验材料\att_faces','\s',int2str(1),'\',int2str(1),'.txt'];p=load(FileName);avg_face=double(avg_face);big_array=reshape(p,1,10304);a_big_array=double(reshape(big_array,112,92));big_array=a_big_array(:,:)-avg_face(:,:);big_array=reshape(big_array,1,10304);tyxs=big_array*sort_V;re_newimage=double(zeros(10304,1));fori=1:400re_newimage(:,:)=re_newimage(:,:)+tyxs(i)*sort_V(1:10304,i);endre_newimage=reshape(re_newimage,112,92);re_newimage=re_newimage(:,:)+avg_face(:,:);minum=min(min(re_newimage));%对特征向量进行标定maxum=max(max(re_newimage));d=maxum-minum;re_newimage(:,:)=re_newimage(:,:)-minum;re_newimage(:,:)=re_newimage(:,:)/d;re_newimage(:,:)=re_newimage(:,:)*255;re_newimage=uint8(re_newimage);end
本文标题:MATLAB下人脸图像的PCA重构matlab代码
链接地址:https://www.777doc.com/doc-3022110 .html