%% 图像数据提取
clc,clear
I1=imread('att_faces/s1/1.pgm');
I2 = imread('att_faces/s1/2.pgm');
I3 = imread('att_faces/s1/3.pgm');
I4 = imread('att_faces/s1/4.pgm');
I5 = imread('att_faces/s1/5.pgm');
I6 = imread('att_faces/s1/6.pgm');
I7 = imread('att_faces/s1/7.pgm');
I8 = imread('att_faces/s1/8.pgm');
I9 = imread('att_faces/s1/9.pgm');
I10 = imread('att_faces/s1/10.pgm');
[Sn Sm]=size(I1);
Scal=0.75; %图像缩放比例
Wid=round(Sm*Scal);
Hei=round(Sn*Scal);
Person1=[I1;I2;I3;I4;I5;I6;I7;I8;I9;I10];
Person1=imresize(Person1,[Hei*10 Wid]); %resize为每幅图像的宽度的10倍
figure(1)
imshow(Person1,'border', 'loose')
I1=imread('att_faces/s2/1.pgm');
I2 = imread('att_faces/s2/2.pgm');
I3 = imread('att_faces/s2/3.pgm');
I4 = imread('att_faces/s2/4.pgm');
I5 = imread('att_faces/s2/5.pgm');
I6 = imread('att_faces/s2/6.pgm');
I7 = imread('att_faces/s2/7.pgm');
I8 = imread('att_faces/s2/8.pgm');
I9 = imread('att_faces/s2/9.pgm');
I10 = imread('att_faces/s2/10.pgm');
Person2=[I1;I2;I3;I4;I5;I6;I7;I8;I9;I10];
Person2=imresize(Person2,[round(Sn*10*Scal) round(Sm*Scal)]);
% figure(2)
% imshow(Person2,'border', 'loose')
I1=imread('att_faces/s3/1.pgm');
I2 = imread('att_faces/s3/2.pgm');
I3 = imread('att_faces/s3/3.pgm');
I4 = imread('att_faces/s3/4.pgm');
I5 = imread('att_faces/s3/5.pgm');
I6 = imread('att_faces/s3/6.pgm');
I7 = imread('att_faces/s3/7.pgm');
I8 = imread('att_faces/s3/8.pgm');
I9 = imread('att_faces/s3/9.pgm');
I10 = imread('att_faces/s3/10.pgm');
Person3=[I1;I2;I3;I4;I5;I6;I7;I8;I9;I10];
Person3=imresize(Person3,[round(Sn*10*Scal) round(Sm*Scal)]);
% figure(3)
% imshow(Person3,'border', 'loose')
I1=imread('att_faces/s4/1.pgm');
I2 = imread('att_faces/s4/2.pgm');
I3 = imread('att_faces/s4/3.pgm');
I4 = imread('att_faces/s4/4.pgm');
I5 = imread('att_faces/s4/5.pgm');
I6 = imread('att_faces/s4/6.pgm');
I7 = imread('att_faces/s4/7.pgm');
I8 = imread('att_faces/s4/8.pgm');
I9 = imread('att_faces/s4/9.pgm');
I10 = imread('att_faces/s4/10.pgm');
Person4=[I1;I2;I3;I4;I5;I6;I7;I8;I9;I10];
Person4=imresize(Person4,[round(Sn*10*Scal) round(Sm*Scal)]);
% figure(4)
% imshow(Person4,'border', 'loose')
AllPerson=zeros(Hei*Wid,40); %40个人,40列
for i=1:1:Hei*10
for j=1:1:Wid
rows=mod((i-1)*69+j,84*69); %行
if rows==0
rows=84*69;
end
cols=ceil(i/Hei); %列
AllPerson(rows,cols)=Person1(i,j);
end
end
for i=1:1:Hei*10
for j=1:1:Wid
rows=mod((i-1)*69+j,84*69); %行
if rows==0
rows=84*69;
end
cols=ceil(i/Hei)+10; %列
AllPerson(rows,cols)=Person2(i,j);
end
end
for i=1:1:Hei*10
for j=1:1:Wid
rows=mod((i-1)*69+j,84*69); %行
if rows==0
rows=84*69;
end
cols=ceil(i/Hei)+20; %列
AllPerson(rows,cols)=Person3(i,j);
end
end
for i=1:1:Hei*10
for j=1:1:Wid
rows=mod((i-1)*69+j,84*69); %行
if rows==0
rows=84*69;
end
cols=ceil(i/Hei)+30; %列
AllPerson(rows,cols)=Person4(i,j);
end
end
figure(5)
AllPerson=AllPerson';
AllPerson=mat2gray(AllPerson); %0-1之间的double数 84*69 40个人
%AllPerson=AllPerson';
imshow(AllPerson,'border', 'loose')
AllPerson=AllPerson';
%% NMF分解
V=AllPerson;
[n m]=size(V);
r=30; %降维后的维数
W=double(rand(n,r));
H=double(rand(r,m));
VHT=V*H';
WHHT=W*(H*H');
WTV=W'*V;
WTWH=W'*W*H;
W1=zeros(n,r);
H1=zeros(r,m);
Temp=zeros(n,m);
for k=1:500
W=W.*(VHT./WHHT);
WTV=W'*V;
WTWH=W'*W*H;
H=H.*(WTV./WTWH);
VHT=V*H';
WHHT=W*(H*H');
i(k)=k;
Temp=V-W*H;
Temp=Temp.*Temp;
e(k)=sum(sum(Temp));
end
%% 还原特征图像
figure(6)
W=W';
imshow(mat2gray(W))
title('基图像')
W=W';
figure(10)
imshow(mat2gray(H));
imageD=W*H; %重构图像
figure(7);
imshow(mat2gray(imageD));
title('重构出的图像矩阵')
figure(8)
plot(i,e)
title('误差值变化趋势图')
xlabel('迭代次数')
ylabel('误差值')
ReImage=zeros(r*Hei,Wid);
for i=1:1:Hei*r
for j=1:1:Wid
rows=mod((i-1)*69+j,84*69); %行
if rows==0
rows=84*69;
end
cols=ceil(i/Hei); %列
ReImage(i,j)=W(rows,cols);
end
end
ReImage=ReImage';
figure(20);
imshow(mat2gray(ReImage));
title('从W中还原图像')
%纯粹是为了显示图像方便
ReImage=ReImage';
Image1=zeros(840,69);
Image2=zeros(840,69);
Image3=zeros(840,69);
for i=1:1:840
for j=1:1:69
Image1(i,j)=255-ReImage(i,j);
end
end
for i=1:1:840
for j=1:1:69
Image2(i,j)=255-ReImage(i+840,j);
end
end
for i=1:1:840
for j=1:1:69
Image3(i,j)=255-ReImage(i+1680,j);
end
end
figure(21)
subplot(1,3,1)
imshow(mat2gray(Image1),'border', 'loose')
subplot(1,3,2)
imshow(mat2gray(Image2),'border', 'loose')
subplot(1,3,3)
imshow(mat2gray(Image3),'border', 'loose')
%从ImageD中还原图像
PersonT=zeros(84,69);
for i=1:1:Hei
for j=1:1:Wid
rows=mod((i-1)*69+j,84*69);
if rows==0
rows=84*69;
end
cols=ceil(i/Hei);
PersonT(i,j)=imageD(rows,cols);
end
end
figure(22)
imshow(mat2gray(PersonT),'border', 'loose')
title('还原出的某一个脸');
% Person1=Person1';
% subplot(4,1,1)
% imshow(Person1,'border', 'loose')
% Person2=Person2';
% subplot(4,1,2)
% imshow(Person2,'border', 'loose')
% Person3=Person3';
% subplot(4,1,3)
% imshow(Person3,'border', 'loose')
% Person4=Person4';
% subplot(4,1,4)
% imshow(Person4,'border', 'loose')