使用PCA-matlab进行人脸识别

时间:2014-05-30 17:20:18

标签: matlab computer-vision pca face-recognition matlab-cvst

我的项目是“基于特征的脸部检测和识别”我完成了检测部分(根据肤色从图像中检测脸部)。现在我想使用检测到的脸部的PCA应用代码进行识别代码

handles = guidata(hObject);
handles.output = hObject;
dir1 = handles.d ;
filenames1=dir(fullfile(dir1,'*.tif'));
no_subs=numel(filenames1);
no_images=no_subs;
% Array of Images
t_array=[];
% Creating array of images by converting them to vector
for n=1:no_subs
sub=fullfile(dir1,filenames1(n).name);
img=imread(sub);
img=im2double(img);
img=imresize(img,[92 112]);
t_array(:,n)=img(:);
end
% Code for creating the Projected Images:
% Mean face image vector
mean_face=mean(t_array,2);
m=mean_face ;  
img=reshape(m,[92 112]);
varargout{1} = handles.output;
axes(handles.axes5);
imshow(img);
title('Eigen Image','fontsize',10)
% Shifted images of all the images
shifted_images_array=t_array-repmat(mean_face,1,no_images);
% Matrix with reduced eigen vectors
new_matrix=shifted_images_array'*shifted_images_array;
% Eigen vectors of the new matrix taking 6 out of 10
no_eigen_vectrs=5;
[eigen_vect,eigen_val]=eigs(new_matrix,no_eigen_vectrs);
% Eigen vector of the covariance matrix(matirx array formed by image set "t_array")
eigen_vect_covmat=shifted_images_array*eigen_vect;
% Array of Weights of the projected images of every image
omega_array=eigen_vect_covmat'*shifted_images_array;
% Code for creating the test input:

TestImage = handles.im;
input_img=imread(TestImage);
input_img=im2double(input_img);
input_img=imresize(input_img,[92 112]);
input_vect=input_img(:);
input_shift=input_vect-mean_face;
input_omega=eigen_vect_covmat'*input_shift;
% % Code for Calculating the Euclidean Distance :
% % Find the similarity score
ss=arrayfun(@(n) norm(omega_array(:,n)-input_omega),1:no_images);
% Find image with highest similarity or min value of ss
[match_score,match_idx]=min(ss);
ab = reshape(t_array(:,match_idx),92,112);
% Display Image and Statistics

varargout{1} = handles.output;
axes(handles.axes6);
imshow(ab);
title(   'Match image with database', 'fontsize',10);
handles.b = ('C:\Users\Student\Documents\MATLAB\GUI\Data\Test Database\Bags');
handles.f = ('C:\Users\Student\Documents\MATLAB\GUI\Data\Test Database\Face');
handles.m = ('C:\Users\Student\Documents\MATLAB\GUI\Data\Test Database\Flow');
a = string('Bags');
b = string('Face');
c = string('Flower');

if handles.dr == handles.b
    set (handles.edit1, 'String' ,a);
elseif handles.dr == handles.f
     set (handles.edit1, 'String' ,b);
elseif handles.dr == handles.m
   set (handles.edit1, 'String' ,c);
end

guidata(hObject, handles);



% --- Executes during object creation, after setting all properties.
function axes2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
b=imread('B.tif');
imshow(b);
% Hint: place code in OpeningFcn to populate axes2


% --- Executes during object creation, after setting all properties.
function axes3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
c=imread('F.tif');
imshow(c);
% Hint: place code in OpeningFcn to populate axes3


% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
dir1 = handles.d ;
handles = guidata(hObject);
handles.output = hObject;
% prompt = {'Enter test image name (a number between 1 to 40):'};
% dlg_title = 'Input ';
% num_lines= 1;
% def = {'1'};
% 
% TestImage  = inputdlg(prompt,dlg_title,num_lines,def);
% TestImage = strcat(dir1,'\',char(TestImage),'.tif');
% im = imread(TestImage);
% ime=imresize(im,[92 112]);
%  axes(handles.axes4);
%    imshow(ime);
%    title('input image of database','fontsize',10);

filenames1=dir(fullfile(dir1,'*.tif'));
no_subs=numel(filenames1);
no_images=no_subs;
% Array of Images
t_array=[];
% Creating array of images by converting them to vector
for n=1:no_subs
sub=fullfile(dir1,filenames1(n).name);
img=imread(sub);
img=im2double(img);
img=imresize(img,[92 112]);
t_array(:,n)=img(:);
end
% Code for creating the Projected Images:
% Mean face image vector
fimg=mean(t_array,2);
m=fimg ;  
img=reshape(m,[92 112]);
varargout{1} = handles.output;
axes(handles.axes5);
imshow(img);
title('Fisher Image','fontsize',10)
% Shifted images of all the images 
L=t_array-repmat(fimg,1,no_images);
% Matrix with reduced eigen vectors L = A'*A;
SW=L'*L;
% Calculate the within class variance (SW)
invsw=inv(SW);
sb=no_images*(L)'*(L);
SB=sb;
v=(invsw)*SB;

% find eigne values and eigen vectors of the (v)
[evec,eval]=eig(v);
no_eigen_vectrs=5;
[eigen_vect,eigen_val]=eigs(SW,no_eigen_vectrs);
% Eigen vector of the covariance matrix(matirx array formed by image set "t_array")
eigen_vect_covmat=L*eigen_vect;
% Array of Weights of the projected images of every image
omega_array=eigen_vect_covmat'*L;
% Code for creating the test input:
TestImage = handles.im;
input_img=imread(TestImage);
input_img=im2double(input_img);
input_img=imresize(input_img,[92 112]);
input_vect=input_img(:);
input_shift=input_vect-fimg;
input_omega=eigen_vect_covmat'*input_shift;
% % Code for Calculating the Euclidean Distance  :
% % Find the similarity score
ss=arrayfun(@(n) norm(omega_array(:,n)-input_omega),1:no_images);
% Find image with highest similarity or min value of ss
[match_score,match_idx]=min(ss);
ab = reshape(t_array(:,match_idx),92,112);
% Display Image and Statistics

varargout{1} = handles.output;
axes(handles.axes6);
imshow(ab);
title(   'Match image with database', 'fontsize',10);
handles.b = ('C:\Users\Student\Documents\MATLAB\GUI\Data\Test Database\Bags');
handles.f = ('C:\Users\Student\Documents\MATLAB\GUI\Data\Test Database\Face');
handles.m = ('C:\Users\Student\Documents\MATLAB\GUI\Data\Test Database\Flow');
a = string('Bags');
b = string('Face');
c = string('Flower');

if handles.dr == handles.b
    set (handles.edit1, 'String' ,a);
elseif handles.dr == handles.f
     set (handles.edit1, 'String' ,b);
elseif handles.dr == handles.m
   set (handles.edit1, 'String' ,c);
end

guidata(hObject, handles);

是不是?或者我可以用于我的项目。 我需要有关那个认识到的人的信息。 我如何用我的编码嵌入它?enter image description here

这是我认可的检测阶段 现在告诉我如何使用PCA强制识别阶段?

1 个答案:

答案 0 :(得分:1)

请参阅以下链接:

http://www.cs.ait.ac.th/~mdailey/matlab/

它解释了在包括matlab代码的人脸识别图像上逐步应用PCA。