資源簡介
基于PCA的人臉識別算法,matlab實現,使用的是ORL數據庫,需要配合使用
代碼片段和文件信息
function?FaceRecognition??
clear??%?calc?xmeansigma?and?its?eigen?decomposition????
close?all??
allsamples=[];%所有訓練圖像???
for?i=1:40??????
????for?j=1:5??????????
????????if(i<10)??
???????????a=imread(strcat(‘E:\ORL_92x112\00‘num2str(i)‘0‘num2str(j)‘.bmp‘));???????
????????else??
????????????a=imread(strcat(‘E:\ORL_92x112\0‘num2str(i)‘0‘num2str(j)‘.bmp‘));????
????????end????????????
????????b=a(1:112*92);?%?b是行矢量?1×N,其中N=10304,提取順序是先列后行,即從上到下,從左到右??????????
????????b=double(b);??????????
????????allsamples=[allsamples;?b];??%?allsamples?是一個M?*?N?矩陣,allsamples?中每一行數據代表一張圖片,其中M=200?????
????end??
end??
samplemean=mean(allsamples);?%?平均圖片,1?×?N????
figure%平均圖??
imshow(mat2gray(reshape(samplemean11292)));??
for?i=1:200???
????xmean(i:)=allsamples(i:)-samplemean;?%?xmean是一個M?×?N矩陣,xmean每一行保存的數據是“每個圖片數據-平均圖片”???
end;?????
%?figure%平均圖??
%?imshow(mat2gray(reshape(xmean(1:)11292)));??
sigma=xmean*xmean‘;???%?M?*?M?階矩陣???
[vd]=eig(sigma);??
d1=diag(d);???
[d2index]=sort(d1);?%以升序排序???
cols=size(v2);%?特征向量矩陣的列數??
??
for?i=1:cols????????
????vsort(:i)?=?v(:?index(cols-i+1)?);?%?vsort?是一個M*col(注:col一般等于M)階矩陣,保存的是按降序排列的特征向量每一列構成一個特征向量????????
????dsort(i)???=?d1(?index(cols-i+1)?);??%?dsort?保存的是按降序排列的特征值,是一維行向量???
end??%完成降序排列?%以下選擇90%的能量???
dsum?=?sum(dsort);???????
dsum_extract?=?0;?????
p?=?0;???????
while(?dsum_extract/dsum?0.9)?????????
????p?=?p?+?1;????????????
????dsum_extract?=?sum(dsort(1:p));???????
end??
a=1:1:200;??
for?i=1:1:200??
y(i)=sum(dsort(a(1:i))?);??
end??
figure??
y1=ones(1200);??
plot(ay/dsumay1*0.9‘linewidth‘2);??
grid??
title(‘前n個特征特占總的能量百分比‘);??
xlabel(‘前n個特征值‘);??
ylabel(‘占百分比‘);??
figure??
plot(adsort/dsum‘linewidth‘2);??
grid??
title(‘第n個特征特占總的能量百分比‘);??
xlabel(‘第n個特征值‘);??
ylabel(‘占百分比‘);??
i=1;??%?(訓練階段)計算特征臉形成的坐
評論
共有 條評論