【人臉識(shí)別】基于 Gabor+SVM和PCA+SVM實(shí)現(xiàn)人臉識(shí)別matlab源碼含 GUI
一、簡(jiǎn)介
Gabor+SVM:利用Gabor程序?qū)崿F(xiàn)對(duì)人臉的特征提取,然后用SVM進(jìn)行分類;
1 Gabor
Gabor 特征提取算法可以在不同方向上描述局部人臉特征,對(duì)光照、遮擋以及表情變換等情況具有較強(qiáng)的魯棒性,即Gabor算法在異常和危險(xiǎn)情況下具有較強(qiáng)的系統(tǒng)生存的能力。
1.1 一維Gabor核:
其由一個(gè)高斯核與一個(gè)復(fù)數(shù)波的乘積定義為如下公式:

其中w(t)是高斯函數(shù),s(t)是復(fù)數(shù)波,兩者的一維數(shù)學(xué)表達(dá)式定義如下:

我們將s(t)代入一維Gabor公式可得下式:

我們將上述一維情況推廣到二維
二維復(fù)數(shù)波定義如下,其中(x,y)表示空間域坐標(biāo),(u0,v0)表示頻率域坐標(biāo)。

二維高斯函數(shù)定義如下,其中σx,σy 分別為在x,y兩個(gè)方向上的尺度參數(shù),用來(lái)控制高斯函數(shù)在兩個(gè)方向上的“展布”形狀。(x0,y0)為高斯函數(shù)的中心點(diǎn)。K為高斯核的幅度的比例。

但是由于高斯函數(shù)還有旋轉(zhuǎn)的操作,所以我們對(duì)坐標(biāo)進(jìn)行如下的變換:

由此,我們得到了坐標(biāo)變換后的高斯函數(shù)公式,其中θ表示高斯核順時(shí)針旋轉(zhuǎn)的角度。

1.2 二維Gabor核
類似一維 Gabor 核,我們將二維高斯函數(shù)與二維復(fù)數(shù)波相乘,就得到了二維的Gabor核:

一個(gè)Gabor核能獲取到圖像某個(gè)頻率鄰域的響應(yīng)情況,這個(gè)響應(yīng)結(jié)果可以看做是圖像的一個(gè)特征。如果我們用多個(gè)不同頻率的Gabor核去獲取圖像在不同頻率鄰域的響應(yīng)情況,最后就能形成圖像在各個(gè)頻率段的特征,這個(gè)特征就可以描述圖像的頻率信息了。
下圖展示了一系列具有不同頻率的 Gabor 核,用這些核與圖像卷積,我們就能得到圖像上每個(gè)點(diǎn)和其附近區(qū)域的頻率分布情況。

經(jīng)過 Gabor 濾波獲到的人臉圖像信息包含實(shí)部和虛部?jī)刹糠?,分別代表不同局部的人臉特征信息,為了提取更加全面的人臉特征信息,一般會(huì)采用兩種特征值相結(jié)合的方法,比如幅值和相位信息。但 Gabor 的相位信息會(huì)因?yàn)槿四樋臻g位置發(fā)生改變而不太穩(wěn)定。Gabor 幅值信息變化相對(duì)穩(wěn)定,并且充分反映了人臉圖像的能量譜。因此采取 Gabor 幅值特征。經(jīng)過Gabor幅值特征處理,得到了人臉 Gabor 特征信息。5 個(gè)尺度,8 個(gè)方向的 Gabor 特征提取圖如下所示:

2 PCA+SVM:
2.1 PCA
主成分分析(Principal Component Analysis, 簡(jiǎn)稱PCA)是常用的一種降維方法.
算法步驟:

2.2 SVM介紹
支持向量機(jī)(Support Vector Machines, 簡(jiǎn)稱SVM)是一種二類分類模型.
劃分超平面為:


3 人臉識(shí)別步驟
將每張人臉圖片(m,nm,n)讀取并展開成(m×n,1m×n,1), 假設(shè)總有l(wèi)l張圖片, 所有排列到一起, 一列為一張圖片, 最終形成一個(gè)(m×n,l)(m×n,l) 的矩陣作為原始數(shù)據(jù);
數(shù)據(jù)中心化: 計(jì)算平均臉, 所有列都減去張平均臉;
計(jì)算矩陣的協(xié)方差矩陣/散布矩陣, 求出特征值及特征向量, 并將其從大到小排列取前K個(gè)特征; (到這步特征已將至K維)
計(jì)算中心化后的數(shù)據(jù)在K維特征的投影;
基于上一步的數(shù)據(jù)進(jìn)行 One-VS-One Multiclass SVM模型訓(xùn)練;
讀取用于測(cè)試的人臉圖片, 同訓(xùn)練圖片一樣處理;
利用訓(xùn)練出的模型對(duì)測(cè)試圖片進(jìn)行分類;
計(jì)算準(zhǔn)確率.
二、源代碼
function varargout = pjimage(varargin)
% PJIMAGE MATLAB code for pjimage.fig
% ? ? ?PJIMAGE, by itself, creates a new PJIMAGE or raises the existing
% ? ? ?singleton*.
%
% ? ? ?H = PJIMAGE returns the handle to a new PJIMAGE or the handle to
% ? ? ?the existing singleton*.
%
% ? ? ?PJIMAGE('CALLBACK',hObject,eventData,handles,...) calls the local
% ? ? ?function named CALLBACK in PJIMAGE.M with the given input arguments.
%
% ? ? ?PJIMAGE('Property','Value',...) creates a new PJIMAGE or raises the
% ? ? ?existing singleton*. ?Starting from the left, property value pairs are
% ? ? ?applied to the GUI before pjimage_OpeningFcn gets called. ?An
% ? ? ?unrecognized property name or invalid value makes property application
% ? ? ?stop. ?All inputs are passed to pjimage_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 pjimage
% Last Modified by GUIDE v2.5 11-Jun-2018 08:06:08
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', ? ? ? mfilename, ...
? ? ? ? ? ? ? ? ? 'gui_Singleton', ?gui_Singleton, ...
? ? ? ? ? ? ? ? ? 'gui_OpeningFcn', @pjimage_OpeningFcn, ...
? ? ? ? ? ? ? ? ? 'gui_OutputFcn', ?@pjimage_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 pjimage is made visible.
function pjimage_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 pjimage (see VARARGIN)
% Choose default command line output for pjimage
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes pjimage wait for user response (see UIRESUME)
% uiwait(handles.figure_pjimage);
% --- Outputs from this function are returned to the command line.
function varargout = pjimage_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;
% --------------------------------------------------------------------
function m_file_Callback(hObject, eventdata, handles)
% hObject ? ?handle to m_file (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function m_file_open_Callback(hObject, eventdata, handles)
% hObject ? ?handle to m_file_open (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function m_file_save_Callback(hObject, eventdata, handles)
% hObject ? ?handle to m_file_save (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function m_file_exit_Callback(hObject, eventdata, handles)
% hObject ? ?handle to m_file_exit (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton1 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
figure(1);
for i = 1:40
? ?a = imread(strcat('C:\Users\lenovo\Desktop\人臉識(shí)別\人臉識(shí)別程序\ORL\s', num2str(i), '\1.pgm'));
? ?subplot(5,8,i);
? ?imshow(a);
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton2 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
figure(2);
r = round(112 / 2);
c = round(92 / 2);
gamma = 0.5;
theta = pi / 8;
a = sqrt(2);
fmax = 0.22;
for u = 0 : 4
? ? f = a ^ (-u) * fmax;
? ? lambda = 1 / f;
? ? for v = 0 : 7
? ? ? ? sigma = 0.56 * lambda;
? ? ? ? GK = getGaborKernel(r ,c ,v * theta ,sigma ,lambda ,gamma);%得到一個(gè)方向一個(gè)尺度的Gabor圖像
? ? ? ? subplot(5,8, u*8 + v + 1);
? ? ? ? imshow(GK);
? ? end
end
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton3 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
p = imread('C:\Users\lenovo\Desktop\人臉識(shí)別\人臉識(shí)別程序\ORL\s1\1.pgm');
p = double(p);
[m , n] = size(p);
r = round(m / 2);
c = round(n / 2);
gamma = 0.5;
theta = pi / 8;
a = sqrt(2);
fmax = 0.22;
figure(3);
for u = 0 : 4
? ? f = a ^ (-u) * fmax;
? ? lambda = 1 / f;
? ? for v = 0 : 7
? ? ? ? sigma = 0.56 * lambda;
? ? ? ? GK = getGaborKernel(r ,c ,v * theta ,sigma ,lambda ,gamma);%得到一個(gè)方向一個(gè)尺度的Gabor圖像
? ? ? ? x = conv2(p,GK,'same');%原圖像與Gabor圖像進(jìn)行卷積 ? 112 92
? ? ? ? subplot(5, 8, u*8 + v +1);
? ? ? ? imshow(x);
? ? end
end
% --- Executes during object deletion, before destroying properties.
function axes1_DeleteFcn(hObject, eventdata, handles)
% hObject ? ?handle to axes1 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
function edit1_Callback(hObject, eventdata, handles)
% hObject ? ?handle to edit1 (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 edit1 as text
% ? ? ? ?str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject ? ?handle to edit1 (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
function edit2_Callback(hObject, eventdata, handles)
% hObject ? ?handle to edit2 (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 edit2 as text
% ? ? ? ?str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject ? ?handle to edit2 (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 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)
global ttlabel;
global prelabel;
% global ct;
% global gam;
trainLabel = [];
k = 1;
v = 1;
%共280張圖片
for i = 1 : 40 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?%40個(gè)人
? ?for j = 1 : 7 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? %每個(gè)人7張照片
? ? ? ?a = imread(strcat('C:\Users\lenovo\Desktop\人臉識(shí)別\人臉識(shí)別程序\ORL\s', num2str(i),'\', num2str(j), '.pgm'));
? ? ? ?a = double(a);
? ? ? ?[m,n] = size(a);
? ? ? ?
? ? ? ?trainvector = GetOneImageVector(a);
? ? ? ?trainX(:, k) = trainvector;
? ? ? ?k = k + 1;
? ? ? ?%加標(biāo)簽 ? ?
? ? ? ?trainLabel = [trainLabel v]; ? ? ? ? ? ?%1X280
? ?end
? ?v = v + 1;
end
%歸一化 ?均值向量 ?方差向量 ?
trainx = Normalize(trainX); ? ? ? %6440X280
% ct =str2double(get(handles.edit3,'String'));
% gam = str2double(get(handles.edit4,'String'));
%使用SVM得到模型
model = svmtrain(trainLabel', trainx','-s 0 -t 2 -c 1000 -g 0.0001');
% set(handles.edit1,'string',model);
%處理測(cè)試集
u = 1;
t = 1;
testLabel = [];
for i = 1:40
? ?for j = 8:10
? ? ? ?a = imread(strcat('C:\Users\lenovo\Desktop\人臉識(shí)別\人臉識(shí)別程序\ORL\s', num2str(i),'\', num2str(j), '.pgm'));
? ? ? ?a = double(a);
? ? ? ?[m,n] = size(a); ?
? ? ? ?
? ? ? ?testvector = GetOneImageVector(a);
? ? ? ?testX(:, u) = testvector;
? ? ? ?u = u + 1;
? ? ? ?testLabel = [testLabel t];
? ?end
? ? t = t + 1;
end
三、運(yùn)行結(jié)果




?

?