资源简介
matlab开发-用PCA检测平面图像的眼镜。用主成分分析法检测人脸图像中眼镜的存在
代码片段和文件信息
%main
clear all
clc
close all
%Author: Mahmoud Afifi - York University
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%STEP 1
%Load data and calculate the mean face
%load data
display(‘loading data...‘);
[eyeglassesnonEyeglassestrainingEtrainingNtestingEtestingN]=load_data();
%calculate the average face
display(‘calculate the average face...‘);
avg_face=getAvgFace(trainingN);
%get the size of the original images (all datasets have images with the
%same dimensions)
img_size=[size(trainingN2)size(trainingN3)];
%get the number of samples in the training data
n=size(trainingN1);
%Too much figures..ask for closing figures of step1
choice = questdlg(‘would you like to close all figures of step1?‘ ...
‘Step1‘ ...
‘Yes‘‘No‘‘No‘);
% Handle response
switch choice
case ‘Yes‘
close all
end
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%STEP 2
%get principle components W and the N latent coordinates
%resize data to be (height*width)xN matrix
train_imgs=(reshape(trainingN[size(trainingN1)size(trainingN2)*...
size(trainingN3)]))‘;
glasses_imgs=(reshape(trainingE[size(trainingE1)size(trainingE2)*...
size(trainingE3)]))‘;
test_imgsN=(reshape(testingN[size(testingN1)size(testingN2)*...
size(testingN3)]))‘;
test_imgsE=(reshape(testingE[size(testingE1)size(testingE2)*...
size(testingE3)]))‘;
avg_face=avg_face(:); %resize the mean to be (height*width)x1 vector
display(‘calculate the principal components...‘);
[WXL]=PCA_(train_imgsavg_face); %get the principle components
%get the bound of x axis in plots
m=length(L);
%Scree Plot
figure;
plot(L);
title(‘Scree Plot‘);
ylim([-200max(L(:))])
xlim([-50m])
xlabel(‘Index of Eigenvalues‘) % x-axis label
set(gca‘XTickLabel‘num2str(get(gca‘XTick‘).‘)) %to avoid x10^k
ylabel(‘Eigenvalues‘) % y-axis label
set(gca‘YTickLabel‘num2str(get(gca‘YTick‘).‘)) %to avoid x10^k
print(‘visualization\\ScreePlot‘‘-dpng‘); %save it
%Fraction of Variance (FVE)
FVE=cumsum(L(:))/sum(L(:)); %calc FVE
%plot FVE
figure;
plot(FVE);
title(‘Fraction of Variance Explained‘);
xlabel(‘c‘) % x-axis label
ylabel(‘FVE‘) % y-axis label
ylim([min(FVE(:))1])
print(‘visualization\\FVE‘‘-dpng‘); %save it
%Plot first 10 principle components (Eigenvectors)
figure(‘units‘‘normalized‘‘outerposition‘[0 0 1 1]) %full screen
for i=1:10
subplot(25i);
imshow(reshape(W(:i)img_size(1)img_size(2))[]);
colormap(jet);
title(sprintf(‘Eigenvector %d‘i));
end
print(‘visualization\\10EigenVectors‘‘-dpng‘); %save it
%reconstruct the data
%pick random image
num=round(rand*n-1)+1;
%reconstruct the original images using few number of basis vectors
figure(‘units‘‘normalized‘‘outerposition‘[0 0 1 1]) %full screen
j=2;
subplot(251);
imshow(reshape(train_imgs(:num)img_size(1)img_size(2))[]);
title(
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 12011 2019-01-03 21:10 demo.m
文件 1014 2019-01-03 21:10 for submission.txt
文件 1047 2019-01-03 21:10 getAvgFace.m
文件 2604 2019-01-03 21:10 load_data.m
文件 4091478 2019-01-03 21:10 logo.jpg
文件 1885 2019-01-03 21:10 PCA_.m
文件 913 2019-01-03 21:10 ReadMe.txt
文件 13845639 2019-01-03 21:10 Report.pdf
文件 1482 2019-01-03 21:10 license.txt
评论
共有 条评论