dsort = flipud(d1);
vsort = fliplr(v);
%以下选择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
p = 199;
% (训练阶段)计算特征脸形成的坐标系
%while (i<=p && dsort(i)>0)
% base(:,i) = dsort(i)^(-1/2) * xmean' * vsort(:,i); % base 是N×p 阶矩阵,除以
dsort(i)^(1/2)是对人脸图像的标准化,详见《基于PCA 的人脸识别算法研究》p31 % i = i + 1; % xmean' * vsort(:,i)是小矩阵的特征向量向大矩
阵特征向量转换的过程
%end
base = xmean' * vsort(:,1:p) * diag(dsort(1:p).^(-1/2));
% 生成特征脸
for (k=1:p),
temp = reshape(base(:,k), 112,92);
newpath = ['d:\test\' int2str(k) '.jpg'];
imwrite(mat2gray(temp), newpath);
end
avg = reshape(samplemean, 112,92);
imwrite(mat2gray(avg), 'd:\test\average.jpg');
% 将模型保存
save('e:\ORL\model.mat', 'base', 'samplemean');
人脸重建
% Reconstruct.m
function [] = reconstruct()
load e:\ORL\model.mat;
% 计算新图片在特征子空间中的系数
img = 'D:\test2\10.jpg'
a=imread(img);
b=a(1:112*92); % b 是行矢量 1×N,其中N=10304,提取顺序是先列后行,即从上到下, 从左到右
b=double(b);
b=b-samplemean;
c = b * base; % c 是图片a 在子空间中的系数, 是1*p 行矢量
% 根据特征系数及特征脸重建图
% 前15 个
t = 15;
temp = base(:,1:t) * c(1:t)';
temp = temp + samplemean';
imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t1.jpg');
% 前50 个
t = 50;
temp = base(:,1:t) * c(1:t)';
temp = temp + samplemean';
imwrite(mat2gray(reshape(temp, 112,92)),'d:\test2\t2.jpg');
% 前100 个
百度搜索“70edu”或“70教育网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,70教育网,提供经典教育范文人脸识别matlab程序(3)在线全文阅读。
相关推荐: