基于PCA的人脸识别
- 格式:doc
- 大小:128.00 KB
- 文档页数:6
本文基于PCA算法,实现人脸识别。
首先,我们来看一下测试结果:由于本文只是简单地对训练人脸进行降维处理,并没有考虑其他影响因素,所以识别率相对来说比较低。
下面给出具体代码:function varargout = FaceRec(varargin)% FACEREC M-file for FaceRec.fig% FACEREC, by itself, creates a new FACEREC or raises the existing % singleton*.%% H = FACEREC returns the handle to a new FACEREC or the handle to% the existing singleton*.%% FACEREC('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in FACEREC.M with the given input arguments.%% FACEREC('Property','Value',...) creates a new FACEREC or raises the% existing singleton*. Starting from the left, property valuepairs are% applied to the GUI before FaceRec_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application% stop. All inputs are passed to FaceRec_OpeningFcn via varargin. %% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one% instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help FaceRec% Last Modified by GUIDE v2.5 29-Nov-2013 19:23:02% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @FaceRec_OpeningFcn, ...'gui_OutputFcn', @FaceRec_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before FaceRec is made visible.function FaceRec_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% varargin command line arguments to FaceRec (see VARARGIN)set(handles.axes1,'xtick',[],'ytick',[])set(handles.axes2,'xtick',[],'ytick',[])% Choose default command line output for FaceRechandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes FaceRec wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line. function varargout = FaceRec_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout{1} = handles.output;% --- Executes on button press in btnFaceDataBase.function btnFaceDataBase_Callback(hObject, eventdata, handles)% 选择训练人脸库% hObject handle to btnFaceDataBase (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) DBPath = uigetdir( 'E:/','Select training database path' );if DBPath == 0msgbox('为选择任何文件夹!')return;endfile = dir(DBPath);img = [];imgray = [];dataBase = [];imageName = {};j = 1;for i = 1:size(file,1)ifnot(strcmp(file(i).name,'.')|strcmp(file(i).name,'..')|strcmp(file(i).name,'Thumbs.db'))imageName{j} = {file(i).name};%记录每张脸的名字j = j+1;img = imread(strcat(DBPath,'\',file(i).name));if size(img,3) == 3imgray = double(rgb2gray(img));elseimgray = double(img);enddataBase=[dataBase ; imgray(:)'];endendbase = DimentionDEC(dataBase);handles.faceDataBase = dataBase;handles.PCABase = base;handles.faceName = imageName;guidata(hObject, handles);% --- Executes on button press in btnTestFace.function btnTestFace_Callback(hObject, eventdata, handles)%选择测试人脸% hObject handle to btnTestFace (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) strPath = 'E:/';%'D:\Documents\MATLAB';[fname pname] = uigetfile({'*.jpg ; *.bmp ; *.pgm'},'choose a test face',strPath);%没选择文件处理,异常机制if isequal(fname,0)||isequal(pname,0)errordlg('没有选中文件','打开');return;endstring = strcat(pname,fname);img = imread(string);xy = size(img);btnReset_Callback(hObject, eventdata, handles)axes(handles.axes1)imshow(img)set(handles.textTest,'string',fname(1:length(fname)-4))imgray =[];if size(img , 3) == 3imgray = rgb2gray(img);elseimgray = img;endimTest = double(imgray);handles.MN = xy;handles.testFace = imTest;guidata(hObject,handles)% --- Executes on button press in btnMatching.function btnMatching_Callback(hObject, eventdata, handles)% hObject handle to btnMatching (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA) [matchingFace index] = Test(handles.faceDataBase , handles.testFace , handles.PCABase , handles.MN);axes(handles.axes2)imshow(matchingFace)matchingFaceName = cell2mat(handles.faceName{index(1)});set(handles.textMatching,'string',matchingFaceName(1:length(match ingFaceName)-4))% --- Executes on button press in btnReset.function btnReset_Callback(hObject, eventdata, handles)% hObject handle to btnReset (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)axes(handles.axes1)claset(handles.axes1,'visible','on','xtick',[],'ytick',[]);set(handles.textTest,'string','')axes(handles.axes2)claset(handles.axes2,'visible','on','xtick',[],'ytick',[]);set(handles.textMatching,'string','')2.通过PCA降维,提取主成分function base = DimentionDEC(sampleFace)%parameter:input--sampleFace:训练人脸% output--base:空间变换基meansam = mean(sampleFace);dvalue = sampleFace - repmat(meansam,size(sampleFace,1),1);covMatT = dvalue * dvalue';% the convected matrix of covariance matrix of sample[V D] = eig(covMatT);%sort the eigenvector and eigenvalue in the order of decsend_eigenvalue D = diag(D);dsort = flipud(D);vsort = fliplr(V);%extract the 90% of the PCdsum = sum(dsort);dsum_extract = 0;p=0;while dsum_extract/dsum<0.9p = p+1;dsum_extract = sum(dsort(1:p));endvbase = vsort(:,1:p);base = dvalue' * vbase*diag(dsort(1:p).^(-1/2));%PCAFace = sampleFace * base;end3.输入测试人脸,匹配训练人脸库function [result index]= Test(faceDataBase , image , base , xy)%parameter:input--faceDataBase:训练人脸% image:待测试人脸% base:空间变换基% xy:图像大小% output--result:匹配的图像% index:2范数距离下排序的索引imTest = image(:)';PCADataBase = faceDataBase * base;coor = imTest * base;for k = 1:size(faceDataBase,1)kdist(k) = norm(coor-PCADataBase(k,:));end[~, index] = sort(kdist);result = reshape(uint8(faceDataBase(index(1),:)),xy(1),xy(2));end。