PCA人脸识别算法中特征面的Matlab图

时间:2017-10-15 10:49:39

标签: matlab machine-learning pca face-recognition

我是机器学习和matlab领域的新手。我已经为PCA编写了面部识别代码。但是当我试图显示特征面时,我会卡住。获得特征面后,我将如何显示它们。如果有人能指出我的错误,我将非常感激。我有150张训练集的图像。每个图像是231 * 195矩阵。当我变成专栏时,它变成45045 * 1矩阵。所以我的意思是减少所有图像的matix是A = 45045 * 150。取PCA后我的特征向量矩阵45045 * 36(36个特征向量取主要特征)。所以我的本征面矩阵是36 * 150。

    function trialtrialface

trainingdata = uigetdir('F:\Pattern recognition\face recognition\Yale','select path of training images');
testdata = uigetdir('F:\Pattern recognition\face recognition\Yale','select path of test images');

prompt = {'Enter test image name :'};
dlg_title = 'Input of PCA-Based Face Recognition System';
num_lines= 1;
def = {' '};
TestImage = inputdlg(prompt,dlg_title,num_lines,def);
TestImage = strcat(testdata,'\',char(TestImage),'.pgm');

%%call function
recog_img = facerecog(trainingdata,TestImage);
selected_img = strcat(trainingdata,'\',recog_img);
figure;
select_img = imread(selected_img);
imshow(select_img);
title('Recognized Image');
test_img = imread(TestImage);
figure,imshow(test_img);
title('Test Image');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
result = strcat('the recognized image is : ',recog_img);
disp(result);

function [recognized_img]=facerecog(trainingdata,testimg)

D = dir(trainingdata);  
imgcount = 0;
for i=1 : size(D,1)
    if not(strcmp(D(i).name,'.')|strcmp(D(i).name,'..')|strcmp(D(i).name,'Thumbs.db'))
        imgcount = imgcount + 1; % Number of all images in the training database
    end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%  creating the image matrix X  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

X = [];
for i = 1 : imgcount
    str = strcat(trainingdata,'\',int2str(i),'.pgm');
    img = imread(str);
    [r c] = size(img);
    temp = reshape(img',r*c,1);  
%%                               
    X = [X temp];                
end

%%
%
%          m           -    (MxN)x1  Mean of the training images
%          A           -    (MxN)xP  Matrix of image vectors after each vector getting subtracted from the mean vector m
%     eigenfaces       -    (MxN)xP' P' Eigenvectors of Covariance matrix (C) of training database X
%                                    

%%%%% calculating mean image vector %%%%%

m = mean(X,2); % Computing the average face image m = (1/P)*sum(Xj's)    (j = 1 : P)
imgcount = size(X,2);

%%%%%%%%  calculating A matrix, i.e. after subtraction of all image vectors from the mean image vector %%%%%%

A = [];
for i=1 : imgcount
    temp = double(X(:,i)) - m;
    A = [A temp];
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% COV=cov(A');
L= A' * A;
[Ve,Va]=eig(L)
% [Ve,Va]=eig(COV);


 L_eig_vec = [];

DD=diag(Va);
sorted=sort(DD,'descend');
D=sorted/sum(sorted);
% AA = Va;
sumA = cumsum(D);
kk = find(sumA < 0.9)
sum1 = sumA(kk)
for ii=1:length(kk)
    L_eig_vec=[L_eig_vec Va(:,kk(ii))];
end

eigenfaces = A * L_eig_vec;   %eigenfaces 

for i=1:size(eigenfaces,2)
    img=reshape(eigenfaces(:,i),c,r);
    img=img';
    img=histeq(img,255);
    subplot(ceil(sqrt(imgcount)),ceil(sqrt(imgcount)),i)
    imshow(img)
    drawnow;
    if i==1
        title('Eigenfaces','fontsize',18)
    end
end

projectimg = [ ];  % projected image vector matrix
for i = 1 : size(A,2)
    temp = eigenfaces' * A(:,i);
    projectimg = [projectimg temp];
end

F     = projectimg;




%%
%%%%% extractiing PCA features of the test image %%%%%

test_image = imread(testimg);
test_image = test_image(:,:,1);
[r c] = size(test_image);
temp = reshape(test_image',r*c,1); % creating (MxN)x1 image vector from the 2D image
temp = double(temp)-m; % mean subtracted vector
projtestimg = eigenfaces'*temp; % projection of test image onto the facespace

%%%%% calculating & comparing the euclidian distance of all projected trained images from the projected test image %%%%%

euclide_dist = [ ];

for i=1 : size(eigenfaces,2)
    temp = (norm(projtestimg-projectimg(:,i)));
    euclide_dist = [euclide_dist temp];
end


[euclide_dist_min recognized_index] = min(euclide_dist);
recognized_img = strcat(int2str(recognized_index),'.pgm');
end



end

0 个答案:

没有答案
相关问题