人脸识别作为图像处理和机器视觉的经典应用,已经广泛应用于实际生活中了。本文重点讲述采用PCA降维提取特征向量的方法进行人脸识别,在ORL Database of Faces中进行训练和识别,并进行了Matlab仿真和可视化。
http://www.javashuo.com/article/p-odxzsxtx-ns.html
https://www.cnblogs.com/zmshy2128/p/6180756.html
The ORL Database of Faces:https://www.cl.cam.ac.uk/research/dtg/attarchive/facedatabase.html
本文只提供了系统界面实现部分,PCA训练和识别的核心函数有需要的可以通过左侧公告栏中QQ图标联系我。
function varargout = FaceRecognitionDemo(varargin) % FACERECOGNITIONDEMO M-file for FaceRecognitionDemo.fig % FACERECOGNITIONDEMO, by itself, creates a new FACERECOGNITIONDEMO or raises the existing % singleton*. % % H = FACERECOGNITIONDEMO returns the handle to a new FACERECOGNITIONDEMO or the handle to % the existing singleton*. % % FACERECOGNITIONDEMO('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in FACERECOGNITIONDEMO.M with the given input arguments. % % FACERECOGNITIONDEMO('Property','Value',...) creates a new FACERECOGNITIONDEMO or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before FaceRecognitionDemo_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to FaceRecognitionDemo_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 FaceRecognitionDemo % Last Modified by GUIDE v2.5 17-Apr-2019 23:52:52 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @FaceRecognitionDemo_OpeningFcn, ... 'gui_OutputFcn', @FaceRecognitionDemo_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before FaceRecognitionDemo is made visible. function FaceRecognitionDemo_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 FaceRecognitionDemo (see VARARGIN) % Choose default command line output for FaceRecognitionDemo handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes FaceRecognitionDemo wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = FaceRecognitionDemo_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 structure varargout{1} = handles.output; % --- Executes on button press in selectPCA. function selectPCA_Callback(hObject, eventdata, handles) % hObject handle to selectPCA (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.selectPCA,'value',1); % Hint: get(hObject,'Value') returns toggle state of selectPCA % --- Executes on button press in clickBeginSim. function clickBeginSim_Callback(hObject, eventdata, handles) % hObject handle to clickBeginSim (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) if get(handles.selectPCA,'value') gMetholdSelect=1; end if get(handles.selectSinglaImageRec,'value') gOneImgOrRate=1; end if get(handles.selectRecogRate,'value') gOneImgOrRate=2; end gTrainNum = str2double(get(handles.inputTrainNum,'string')); if gTrainNum<=0 || gTrainNum>10 disp('error!'); end %% face detect if gOneImgOrRate == 1%检测单幅图片 % if gMetholdSelect==1% pic = double(handles.pic); testImgData=reshape(pic,size(pic,1)*size(pic,2),1); trainPcaData = handles.trainPcaData; ImageIndex = recognizeSingleImgPCA(trainPcaData,testImgData); RealIndex = ceil(ImageIndex/gTrainNum); end path = strcat('att_faces\s',num2str(RealIndex)); matchImg=imread(strcat(path,'\',num2str(2),'.pgm')); axes(handles.axes2); imshow(matchImg); title('从人脸库中匹配出的人脸图像'); end if gOneImgOrRate == 2%计算识别概率 if gMetholdSelect == 1 processPCARecognizeRate(handles); end end % --- Executes on button press in selectRecogRate. function selectRecogRate_Callback(hObject, eventdata, handles) % hObject handle to selectRecogRate (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.selectSinglaImageRec,'value',0); set(handles.selectRecogRate,'value',1); set(handles.inputTrainNum,'Enable','off'); set(handles.buttonTrainSamples,'Enable','off'); set(handles.buttonSelectImg,'Enable','off'); % Hint: get(hObject,'Value') returns toggle state of selectRecogRate % --- Executes on button press in selectSinglaImageRec. function selectSinglaImageRec_Callback(hObject, eventdata, handles) % hObject handle to selectSinglaImageRec (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) set(handles.selectSinglaImageRec,'value',1); set(handles.selectRecogRate,'value',0); set(handles.inputTrainNum,'Enable','on'); set(handles.buttonTrainSamples,'Enable','on'); set(handles.buttonSelectImg,'Enable','on'); % Hint: get(hObject,'Value') returns toggle state of selectSinglaImageRec function inputTrainNum_Callback(hObject, eventdata, handles) % hObject handle to inputTrainNum (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of inputTrainNum as text % str2double(get(hObject,'String')) returns contents of inputTrainNum as a double % --- Executes during object creation, after setting all properties. function inputTrainNum_CreateFcn(hObject, eventdata, handles) % hObject handle to inputTrainNum (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in buttonSelectImg. function buttonSelectImg_Callback(hObject, eventdata, handles) % hObject handle to buttonSelectImg (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %打开文件对话框 [filename,pathname]=uigetfile({ '*.*','All Files(*.*)';}, '选择文件'); if isequal([filename,pathname],[0,0]) return else %读取图片 picPath = fullfile(pathname,filename); pic = imread(picPath); handles.pic = pic; end axes(handles.axes1); imshow(handles.pic); title('待识别的人脸图像'); % Update handles structure guidata(hObject, handles); % --- Executes on button press in buttonTrainSamples. function buttonTrainSamples_Callback(hObject, eventdata, handles) % hObject handle to buttonTrainSamples (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) %% if get(handles.selectPCA,'value') gMetholdSelect=1; end gTrainNum = str2double(get(handles.inputTrainNum,'string')); if gTrainNum<=0 || gTrainNum>10 disp('error!'); end %% load data base to train if gMetholdSelect == 1%for PCA handles.trainPcaData = trainSingleImgPCA(gTrainNum); end msgbox('Train Completed'); % Update handles structure guidata(hObject, handles); % --- Execute the simulation of recognize rate of PCA function processPCARecognizeRate(handles) regRate = zeros(1,9); for i=1:9 [trainData testData] = getPCATrainTest(i); rate = recognizeRateImgPCA(trainData, testData, i); regRate(i) = rate; end axes(handles.axes1); plot(1:9, regRate, '-.or','MarkerFaceColor','g') grid on; ylim([50 100]); xlabel('每个识别个体的训练样本数量'); ylabel('识别概率(%)'); title('PCA的识别概率'); % --- Executes on button press in pushbutton6. function pushbutton6_Callback(hObject, eventdata, handles) %%关闭图像 % hObject handle to pushbutton6 (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); cla reset; axes(handles.axes2); cla reset; guidata(hObject, handles); % --- Executes on button press in pushbutton7. function pushbutton7_Callback(hObject, eventdata, handles) %%退出系统 % hObject handle to pushbutton7 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) close;