PCA检测人脸的简单示例_matlab实现
PCA检测人脸的简单示例,matlab R2009b上实现
训练:
训练用的20副人脸:
%训练
%Lx=X'*X
clear;
clc;
train_path='..\Data\TrainingSet\';
phi=zeros(64*64,20);
for i=1:20
path=strcat(train_path,num2str(i),'.bmp');
Image=imread(path);
Image=imresize(Image,[64,64]);
phi(:,i)=double(reshape(Image,1,[])');
end;
%mean
mean_phi=mean(phi,2);
mean_face=reshape(mean_phi,64,64);
Image_mean=mat2gray(mean_face);
imwrite(Image_mean,'meanface.bmp','bmp');
�mean
for i=1:19
X(:,i)=phi(:,i)-mean_phi;
end
Lx=X'*X;
tic;
[eigenvector,eigenvalue]=eigs(Lx,19);
toc;
%normalization
for i=1:19
UL(:,i)=X*eigenvector(:,i)/sqrt(eigenvalue(i,i));
end
%display Eigenface
for i=1:19
Eigenface=reshape(UL(:,i),[64,64]);
figure(i);
imshow(mat2gray(Eigenface));
end
得到的均值图像mean_face:
前19个最大主元对应的“特征脸”
测试:
测试用样本:
%使用测试样本进行测试
clc;
test_path='..\Data\TestingSet\';
error=zeros([1,4]);
for i=1:4
path=strcat(test_path,num2str(i),'.bmp');
Image=imread(path);
Image=double(imresize(Image,[64,64]));
phi_test=zeros(64*64,1);
phi_test(:,1)=double(reshape(Image,1,[])');
X_test=phi_test-mean_phi;
Y_test=UL'*X_test;
X_test_re=UL*Y_test;
Face_re=X_test_re+mean_phi;
calculate error rate
e=Face_re-phi_test;
%%display figure
Face_re_2=reshape(Face_re(:,1),[64,64]);
figure(i);
imshow(mat2gray(Image));
title('Original');
figure(10+i);
imshow(mat2gray(Face_re_2));
title('Reconstruct');
error(1,i)=norm(e);
%dispaly error rate
error_rate=error(1,i);
display(error_rate);
end
重建出的测试样本与原样本的对比:
四副测试样本的重建误差分别为:
1.4195e+003
1.9564e+003
4.7337e+003
7.0103e+003
可见测试样本为人脸的样本的重建误差显然小于非人脸的重建误差。
最新文章
- ndoutils2.2.0(ndo2db)中文乱码问题解决
- 在.sln文件中设置Visual Studio默认启动项目的简单方法
- Android消息处理机制
- Windows网络共享权限设置
- PowerShell处理RSS信息
- anroid
- C++中复制构造函数与重载赋值操作符总结
- ios中UIButton选中状态切换
- 封装一个MongoDB的 asp.net 链接类
- 神奇的 BlocksKit(1):源码分析(下)
- 快速创建InfoPath表单
- 5.VBS的一些约定,提高可读性
- Robberies hdu 2955 01背包
- sqlite db数据的导出
- 『集群』005 Slithice 基于 集群 的 自动容错
- jqgrid的增删改查
- Windows下使用TeamViewer连接远程服务器,以及解决“远程桌面关闭后TeamViewer不能连接”的问题
- git之reset图解
- cdnbest架设cdn同一个源用不同的端口访问如何设置
- How to configure ESXi to boot via Software iSCSI?