A list,

Gabor+SVM: The use of Gabor program to achieve the feature extraction of face, and then classification by SVM; 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 nucleus:

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:



By substituting S (t) into one-dimensional Gabor formula, the following formula can be obtained:



We generalize the above one-dimensional case to two dimensions

Two-dimensional complex waves are defined as follows, where (x,y) represents spatial domain coordinates and (u0,v0) represents frequency domain coordinates.



The two-dimensional Gaussian function is defined as follows, where σx and σy are scale parameters in the x and Y directions respectively, which are used to control the “spread” shape of the Gaussian function in the two directions. (x0,y0) is the center of the Gaussian function. K is the proportion of the amplitude of the Gaussian kernel.



However, since the Gaussian function also has rotation operation, we carry out the following coordinate transformation:



Thus, we obtain the formula of gaussian function after coordinate transformation, where θ represents the Angle of clockwise rotation of gaussian kernel.



1.2 Two-dimensional Gabor nuclei

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 certain frequency neighborhood of the image, and the response result can be regarded as a feature of the image. If we use multiple Gabor nuclei with different frequencies to obtain the response of the image in different frequency neighborhoods, finally the image features in each frequency segment can be formed, which can describe the frequency information of the image.

The figure below shows a series of Gabor nuclei with different frequencies. By convolving these nuclei with the image, we can obtain the frequency distribution of each point on the image and its vicinity.



Face image information obtained by Gabor filtering consists of real part and imaginary part, representing different local face feature information respectively. In order to extract more comprehensive face feature information, the combination of two feature values, such as amplitude and phase information, is generally adopted. However, Gabor’s phase information is unstable due to changes in the spatial position of the face. The change of Gabor amplitude information is relatively stable, and fully reflects the energy spectrum of face image. Therefore, Gabor amplitude feature is adopted. After Gabor amplitude feature processing, face Gabor feature information 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 common dimension reduction method.

Algorithm steps:



2.2 the SVM is introduced

Support Vector Machines (SVM) is a binary classification model.

The hyperplane is divided into:





3 Face recognition steps

Each face picture (M,nm, N) is read and expanded into (M × N, 1M × N,1), assuming that there are always LL pictures, all arranged together, a column of a picture, and eventually form a (M × N, L)(M × N, L) matrix as the original data;

Data center: Calculate the average face, subtract an average face from all columns;

The covariance matrix/dispersion matrix of the matrix is calculated, the eigenvalues and eigenvectors are obtained, and the first K features are arranged from large to small. (At this point the feature is approaching K dimensions)

The projection of k-dimensional features of the centralized data is calculated;

One-vs-one Multiclass SVM model was trained based on the data in the previous step.

Face images used for testing were read and processed as training images.

The trained model is used to classify test images.

Calculation accuracy.

Ii. 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)".
%
% 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\人脸识别\人脸识别程序\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);%得到一个方向一个尺度的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\人脸识别\人脸识别程序\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);%得到一个方向一个尺度的Gabor图像
         x = conv2(p,GK,'same');%原图像与Gabor图像进行卷积   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个人
    for j = 1 : 7                                 %每个人7张照片
        a = imread(strcat('C:\Users\lenovo\Desktop\人脸识别\人脸识别程序\ORL\s', num2str(i),'\', num2str(j), '.pgm'));
        a = double(a);
        [m,n] = size(a);
        
        trainvector = GetOneImageVector(a);
        trainX(:, k) = trainvector;
        k = k + 1;
        %加标签    
        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);
%处理测试集
u = 1;
t = 1;
testLabel = [];
for i = 1:40
    for j = 8:10
        a = imread(strcat('C:\Users\lenovo\Desktop\人脸识别\人脸识别程序\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
Copy the code

3. Operation results