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

可见测试样本为人脸的样本的重建误差显然小于非人脸的重建误差。

最新文章

  1. ndoutils2.2.0(ndo2db)中文乱码问题解决
  2. 在.sln文件中设置Visual Studio默认启动项目的简单方法
  3. Android消息处理机制
  4. Windows网络共享权限设置
  5. PowerShell处理RSS信息
  6. anroid
  7. C++中复制构造函数与重载赋值操作符总结
  8. ios中UIButton选中状态切换
  9. 封装一个MongoDB的 asp.net 链接类
  10. 神奇的 BlocksKit(1):源码分析(下)
  11. 快速创建InfoPath表单
  12. 5.VBS的一些约定,提高可读性
  13. Robberies hdu 2955 01背包
  14. sqlite db数据的导出
  15. 『集群』005 Slithice 基于 集群 的 自动容错
  16. jqgrid的增删改查
  17. Windows下使用TeamViewer连接远程服务器,以及解决“远程桌面关闭后TeamViewer不能连接”的问题
  18. git之reset图解
  19. cdnbest架设cdn同一个源用不同的端口访问如何设置
  20. How to configure ESXi to boot via Software iSCSI?

热门文章

  1. js框架:vue
  2. 服务器宕机,mysql无法启动,job for mysql.service failed because the process exited with error code,数据库备份与恢复
  3. windows 查看端口占用(转)
  4. MySQL 查询练习记录
  5. SQL生成日期维度(到小时)
  6. jquery的$().each和$.each的区别
  7. 事务的隔离级别和mysql事务隔离级别修改
  8. ES5数组遍历
  9. 单机版mongodb
  10. uvm_mem——寄存器模型(十二)