# [face recognition] face recognition based on Gabor+SV and PCA+SVM matlab source code, including GUI

## 1, Introduction

Gabor+SVM: Gabor program is used to extract the features of human face, and then SVM is used for classification;
1 Gabor
Gabor feature extraction algorithm can describe local face features in different directions, and has strong robustness to illumination, occlusion and expression transformation, that is, Gabor algorithm has strong system survival ability in abnormal and dangerous situations.

1.1 one dimensional Gabor core:
It is defined by the product of a Gaussian kernel and a complex wave as follows: Where w(t) is a Gaussian function and s(t) is a complex wave. The one-dimensional mathematical expressions of the two are defined as follows: We substitute s(t) into one-dimensional Gabor formula to obtain the following formula: We extend the above one-dimensional case to two-dimensional case
The definition of two-dimensional complex wave is as follows, where (x,y) represents space domain coordinates and (u0,v0) represents frequency domain coordinates. The two-dimensional Gaussian function is defined as follows, where σ x, σ y are the scale parameters in X and y directions respectively, which are used to control the "distribution" shape of Gaussian function in two directions. (x0,y0) is the center point of Gaussian function. K is the proportion of the amplitude of the Gaussian kernel. However, since the Gaussian function also has the operation of rotation, we transform the coordinates as follows: Therefore, we get the Gaussian function formula after coordinate transformation, where θ Represents the clockwise rotation angle of the Gaussian kernel. 1.2 two dimensional Gabor kernel
Similar to the one-dimensional Gabor kernel, we multiply the two-dimensional Gaussian function by the two-dimensional complex wave to obtain the two-dimensional Gabor kernel: A Gabor nuclear energy obtains the response of a frequency neighborhood of the image, and the response result can be regarded as a feature of the image. If we use several Gabor cores with different frequencies to obtain the response of the image in the neighborhood of different frequencies, we can finally form the characteristics of the image in each frequency band, which can describe the frequency information of the image.

The following figure shows a series of Gabor kernels with different frequencies. By convoluting these kernels with the image, we can get the frequency distribution of each point on the image and its nearby area. The face image information obtained by Gabor filtering includes real part and imaginary part, which represent different local face feature information respectively. In order to extract more comprehensive face feature information, the combination of two eigenvalues is generally used, such as amplitude and phase information. However, Gabor's phase information will be unstable due to the change of face spatial position. Gabor amplitude information changes relatively stably, and fully reflects the energy spectrum of face image. Therefore, Gabor amplitude feature is adopted. After Gabor amplitude feature processing, the Gabor feature information of human face is obtained. Gabor feature extraction of 5 scales and 8 directions is shown as follows: 2 PCA+SVM:
2.1 PCA
Principal Component Analysis (PCA) is a commonly used dimensionality reduction method
Algorithm steps: 2.2 introduction to SVM
Support Vector Machines (SVM) is a class II classification model
The hyperplane is divided into:  3 face recognition steps
Read and expand each face picture (m,nm,n) into (m) × n,1m × n. 1) suppose there are always ll pictures, all of which are arranged together, and one column is a picture, and finally one (m) is formed × n,l)(m × n. L) matrix as raw data;
Data centralization: calculate the average face, and subtract the average face from all columns;
Calculate the covariance matrix / dispersion matrix of the matrix, calculate the eigenvalues and eigenvectors, and arrange them from large to small to take the first K features; (by this step, the feature has been moved to dimension K)
Calculate the projection of the centralized data in the K-dimensional feature;
Train one vs one multiclass SVM model based on the data in the previous step;
Read the face image used for the test and process it the same as the training image;
Using the trained model to classify the test images;
Calculation accuracy

## 2, Source code

```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)".
%

% Edit the above text to modify the response to help pjimage

% 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\Face recognition\Face recognition program\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);%Get one dimension in one direction Gabor image
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\Face recognition\Face recognition program\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);%Get one dimension in one direction Gabor image
x = conv2(p,GK,'same');%Original image and Gabor Image convolution 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 pictures in total
for i = 1 : 40                                  %40 personal
for j = 1 : 7                                 %7 photos per person
a = imread(strcat('C:\Users\lenovo\Desktop\Face recognition\Face recognition program\ORL\s', num2str(i),'\', num2str(j), '.pgm'));
a = double(a);
[m,n] = size(a);

trainvector = GetOneImageVector(a);
trainX(:, k) = trainvector;
k = k + 1;
%Label
trainLabel = [trainLabel v];            %1X280
end
v = v + 1;
end
%Normalized mean vector variance vector
trainx = Normalize(trainX);       %6440X280

% ct =str2double(get(handles.edit3,'String'));
% gam = str2double(get(handles.edit4,'String'));
%use SVM Get model

model = svmtrain(trainLabel', trainx','-s 0 -t 2 -c 1000 -g 0.0001');
% set(handles.edit1,'string',model);
%Processing test sets
u = 1;
t = 1;
testLabel = [];
for i = 1:40
for j = 8:10
a = imread(strcat('C:\Users\lenovo\Desktop\Face recognition\Face recognition program\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
```

## 3, Operation results    ## 4, Remarks

Complete code or write on behalf of QQ1575304183

Keywords: image identification

Added by ericw on Fri, 18 Feb 2022 07:13:11 +0200