%%提取所有图像主成分,计算各个主成分数量下的所有图像的重构误差的均值,画出平均重构误差—主成分数量图
clc
clear
close
tic
path(path,'D:\MATLAB\function\')
Num_subject=15; %样本人数
Num_image=11; %每人图片数
Total_num=165; %总图片数
Fig_row=100;
Fig_col=100;
% Fig_row=100;
% Fig_col=80;
% Fig_row=64;
% Fig_col=64;
% Fig_row=32;
% Fig_col=32;
Subfig_row=Fig_row/10;%对于64*64和32*32图片,10变为8和4
Subfig_col=Fig_col/10;%同上
Subfig_pixl=Subfig_row*Subfig_col;%每个子图的像素点数
Subfig_num=Fig_row*Fig_col/(Subfig_row*Subfig_col);%每张图分成的子图数量
Train_num=8; %每人训练样本数
Test_num=3; %每人测试样本数量
Noise_minrow=15; %最低噪声行数
Noise_mincol=10; %最低噪声列数
Noise_num=4; %每人训练样本中带噪声的图片数量
PCA_num=100; %需要显示的主成分个数1-min(Subfig_pixl,Subfig_num*Total_num)
Cycle_num=1; %每个主成分下,循环Cycle_num次计算重构误差,每次循环均重新加载数据
%%
for k=1:Cycle_num
if Fig_col==100
load(['D:\MATLAB\YaleFace\165mat\' 'Yale_100x100.mat']);
elseif Fig_col==80
load(['D:\MATLAB\YaleFace\165mat\' 'Yale_100x80.mat']);
elseif Fig_col==64
load(['D:\MATLAB\YaleFace\165mat\' 'Yale_64x64.mat']);
elseif Fig_col==32
load(['D:\MATLAB\YaleFace\165mat\' 'Yale_32x32.mat']);
end
X_fig=zeros(Fig_row,Fig_col,Total_num);
%将图片向量随机存储到X_fig中,其中人的顺序不变
for i=1:Num_subject
temp=randperm(Num_image);
for j=1:Num_image
X_fig(:,:,(i-1)*11+j)=reshape(fea((i-1)*11+temp(j),:),Fig_row,Fig_col);
end
end
%存储原始图片并标准化
X_org=[];%X_org表示不带噪声的图像数据
for i=1:Total_num
X_org = [X_org , Fig2Fig(X_fig(:,:,i),Subfig_row, Subfig_col,Subfig_row*Subfig_col, 1, 1,Subfig_num )];
end
X_mean=sum(X_org,2)/(Subfig_num*Total_num);
X_org=X_org-repmat(X_mean,1,Subfig_num*Total_num);%影响不大
%每人(前Train_num幅)中的随机Noise_num幅加入随机大小(不小于Noise_minrow*Noise_mincol)随机位置的噪声
for i=1:Num_subject
%产生带噪声的图像的序号(1-Train_num中任意Noise_num幅图像)
temp=randperm(Train_num);
temp=temp(1:Noise_num);
for j=1:Noise_num
X_fig(:,:,temp(j)+11*(i-1))=Matrix_Noise(X_fig(:,:,temp(j)+11*(i-1)),Noise_minrow,Noise_mincol,255);
end
end
%存储加噪图片并标准化
X=[];%X表示带噪声的图像数据
for i=1:Total_num
X = [X , Fig2Fig(X_fig(:,:,i),Subfig_row,Subfig_col,Subfig_row*Subfig_col , 1, 1 , Subfig_num )];
end
X_mean_N=sum(X_org,2)/(Subfig_num*Total_num);
X=X-repmat(X_mean_N,1,Subfig_num*Total_num);%影响不大
%采用PCA-L1提取主成分
U=PCA_L1(X,PCA_num);
%计算PCA_num从1—PCA_num时的各个图片的重构误差
X_org=X_org+repmat(X_mean,1,Subfig_num*Total_num);%如果X_org上面经过标准化,则此处应还原回去
X=X+repmat(X_mean_N,1,Subfig_num*Total_num);%如果X上面经过标准化,则此处应还原回去
e=zeros(PCA_num,Cycle_num);%重构误差,一维表示1-PCA_num个PCA情况,二维表示Cycle_num次循环情况
for i=1:PCA_num
for j=1:Total_num
%计算所有图片的重构误差的均值
temp=X_org(:,((j-1)*Subfig_num+1):(j*Subfig_num));%取一幅原图像
e_temp=zeros(1,Subfig_num);
for q=1:Subfig_num
e_temp(1,q)=norm(temp(:,q)-U(:,1:i)*(U(:,1:i)'*temp(:,q)),2)^2;
end
e(i,k)=e(i,k)+sqrt(sum(e_temp))/Total_num;
end
end
toc
disp(['循环次数:',num2str(k),'/',num2str(Cycle_num )])
tic
end
%显示平均重构误差—主成分数量图
figure(1)
hold on
grid on
plot(1:PCA_num,sum(e,2)/Cycle_num,'b')
save D:\MATLAB\BPCA_L1_Reconstruction\BPCA_L1_Reconstruction(PCA_100_Cyc_1).mat
saveas(gcf,['D:\MATLAB\BPCA_L1_Reconstruction\','BPCA_L1_Reconstruction'],'fig')
%显示所有原图、加噪原图、2DPCA_L1重构图
for i=1:Total_num
imwrite(mat2gray(Fig2Fig(X_org( : ,((i-1)*Subfig_num+1):i*Subfig_num) , 100 ,1 , 10 , 10 , 10 , 10 )) ,['D:\MATLAB\BPCA_L1_Reconstruction\Output_Figure\Total_Original_Figure\',num2str(i),'.bmp'])
imwrite(mat2gray(Fig2Fig(X( : , ((i-1)*Subfig_num+1):i*Subfig_num) , 100 ,1 , 10 , 10 , 10 , 10 )) ,['D:\MATLAB\BPCA_L1_Reconstruction\Output_Figure\Total_Noise_Figure\',num2str(i),'.bmp'])
imwrite(mat2gray(Fig2Fig(U*U'*X( : , ((i-1)*Subfig_num+1):i*Subfig_num) , 100 ,1 , 10 , 10 , 10 , 10 )) ,['D:\MATLAB\BPCA_L1_Reconstruction\Output_Figure\Total_Rconstruction_Figure\',num2str(i),'.bmp'])
end
toc