使用matlab对训练样本图像降维,并对测试图像使用变换矩阵降维并重构

clear;
clc;
train_path='E:\TrainingSet\angry\positive\';
images = dir('E:\TrainingSet\angry\positive\*.bmp'); 
phi=zeros(30,64*64);

% 加载样本图像到 30*(64*64)的矩阵中,每一行代表一幅图像
for i=1:30
    path=strcat(train_path,images(i).name);
    Image=imread(path);
    Image=imresize(Image,[64,64]);
    phi(i,:)=double(reshape(Image,1,[]));
end;

% 计算平均脸,并保存用以查看
mean_phi=mean(phi,1);
mean_face=reshape(mean_phi,64,64);
Image_mean=mat2gray(mean_face);
imwrite(Image_mean,'meanface2.bmp','bmp');

% 使用matlab自带的pca进行降维
[coeff, score, latent, TSQUARED] = princomp(phi,'econ');

%display Eigenface
for i=1:29
    Eigenface=reshape(coeff(:,i),[64,64]);
    figure(i);
    imshow(mat2gray(Eigenface));
end

% 进行测试
%使用测试样本进行测试
clc;
test_path='E:\BIT\code\FER\meanface.bmp';
error=zeros([1,4]);

Image=imread(test_path);
Image=double(imresize(Image,[64,64]));
phi_test=zeros(1,64*64);
phi_test(1,:)=double(reshape(Image,1,[])); % 读入的测试图像保存为一行,行向量
X_test=phi_test-mean_phi;
Y_test=X_test*coeff;
X_test_re=Y_test*coeff';
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);
转载自 :

http://www.tuicool.com/articles/jMBjyu
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值