ProjectedTestImage = Eigenfaces'*Difference; % 测试图像的特征向量 Euc_dist = [];
for i = 1 : Train_Number%对每列
q = ProjectedImages(:,i);%取出训练图像
temp = ( norm( ProjectedTestImage - q ) )^2;%欧氏距离 Euc_dist = [Euc_dist temp];% end
[Euc_dist_min , Recognized_index] = min(Euc_dist);%得到差值最小的图像的索引号 OutputName = strcat(int2str(Recognized_index),'.jpg');%得到文件名
Example.m
% A sample script, which shows the usage of functions, included in % PCA-based face recognition system (Eigenface method) %
% See also: CREATEDATABASE, EIGENFACECORE, RECOGNITION clear all clc
close all
% You can customize and fix initial directory paths
TrainDatabasePath = uigetdir('D:\\人脸库\\PCA_based Face Recognition System',... 'Select training database path' );
TestDatabasePath = uigetdir('D:\\人脸库\\\\PCA_based Face Recognition System',... 'Select test database path');
prompt = {'Enter test image name (a number between 1 to 10):'}; dlg_title = 'Input of PCA-Based Face Recognition System'; num_lines= 1; def = {'1'};
TestImage = inputdlg(prompt,dlg_title,num_lines,def);
TestImage = strcat(TestDatabasePath,'\\',char(TestImage),'.jpg'); im = imread(TestImage);
T = CreateDatabase(TrainDatabasePath); [m, A, Eigenfaces] = EigenfaceCore(T);
OutputName = Recognition(TestImage, m, A, Eigenfaces); SelectedImage = strcat(TrainDatabasePath,'\\',OutputName); SelectedImage = imread(SelectedImage); imshow(im) title('测试图像');
figure,imshow(SelectedImage); title('识别图像');
str = strcat('Matched image is : ',OutputName); disp(str)
11