[expression recognition] facial expression recognition based on sparse representation feature template matching algorithm, including Matlab source code

1 Introduction

With the development of pattern recognition, image processing and machine learning, fast and effective automatic recognition and verification technology has attracted extensive attention because of its great theoretical and practical application value. As an important verification means in biometrics, face recognition has achieved great development in the fields of information security, criminal investigation and video surveillance. It is widely used in smart home, safe city, intelligent monitoring and intelligent building. The face recognition algorithm based on sparse representation classification (SRC) has become a research hotspot because of its robustness to noise and local occlusion. The basic idea of SRC is to use the linear representation of test samples on the training set to minimize the reconstruction error for classification. Firstly, the sparse representation coefficients of test samples on different types of training samples are obtained by solving the optimization problem, and the reconstructed samples are obtained through the linear combination of training samples. Finally, the class with the smallest reconstruction residual is obtained as the classification result.

SRC # is a typical algorithm in face recognition. The algorithm does not consider the local information of the samples, but the overall information. It is assumed that the test samples can be approximately expressed as a linear combination of all training samples, and the pixel information of each training sample is converted into column vectors to form a training matrix. Due to the non self adaptability of CS , the orthogonal basis of projection training matrix can obtain sparse matrix, and will not affect the solution of the original problem. This sparse matrix is the sparse representation of the training image, also known as the measurement matrix. The steps of SRC algorithm are as follows:

Part 2 code

function varargout = FacialExpressionRecognitiontool(varargin)% FACIALEXPRESSIONRECOGNITIONTOOL MATLAB code for FacialExpressionRecognitiontool.fig%      FACIALEXPRESSIONRECOGNITIONTOOL, by itself, creates a new FACIALEXPRESSIONRECOGNITIONTOOL or raises the existing%      singleton*.%%      H = FACIALEXPRESSIONRECOGNITIONTOOL returns the handle to a new FACIALEXPRESSIONRECOGNITIONTOOL or the handle to%      the existing singleton*.%%      FACIALEXPRESSIONRECOGNITIONTOOL('CALLBACK',hObject,eventData,handles,...) calls the local%      function named CALLBACK in FACIALEXPRESSIONRECOGNITIONTOOL.M with the given input arguments.%%      FACIALEXPRESSIONRECOGNITIONTOOL('Property','Value',...) creates a new FACIALEXPRESSIONRECOGNITIONTOOL or raises the%      existing singleton*.  Starting from the left, property value pairs are%      applied to the GUI before FacialExpressionRecognitiontool_OpeningFcn gets called.  An%      unrecognized property name or invalid value makes property application%      stop.  All inputs are passed to FacialExpressionRecognitiontool_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 FacialExpressionRecognitiontool​% Last Modified by GUIDE v2.5 23-Oct-2014 12:52:25​% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name',       mfilename, ...                   'gui_Singleton',  gui_Singleton, ...                   'gui_OpeningFcn', @FacialExpressionRecognitiontool_OpeningFcn, ...                   'gui_OutputFcn',  @FacialExpressionRecognitiontool_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 FacialExpressionRecognitiontool is made visible.function FacialExpressionRecognitiontool_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 FacialExpressionRecognitiontool (see VARARGIN)​% Choose default command line output for FacialExpressionRecognitiontoolset(handles.togglebutton1,'visible','off')set(handles.togglebutton2,'visible','off');set(handles.text2,'visible','off');set(handles.edit2,'visible','off');set(handles.text3,'visible','off');axes(handles.axes2)claaxes(handles.axes1)clahandles.output = hObject;​% Update handles structureguidata(hObject, handles);​% UIWAIT makes FacialExpressionRecognitiontool wait for user response (see UIRESUME)% uiwait(handles.figure1);​​% --- Outputs from this function are returned to the command line.function varargout = FacialExpressionRecognitiontool_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;​​​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​​% --- 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)global A m1 n1 No_Files_In_Class_Folder Class_Count Training_Set_Folder​Training_Set_Folder = [uigetdir(''),'\'];m1 =9;n1 =9;TS_Vector = dir(Training_Set_Folder);No_Folders_In_Training_Folder = length(TS_Vector);File_Count = 1;Class_Count = 1;h = waitbar(0,'Reading Test Images,Please wait...');for k = 3:No_Folders_In_Training_Folder    waitbar(k/(No_Folders_In_Training_Folder-2))    Class_Folder = [Training_Set_Folder '\' TS_Vector(k).name,'\'];    CF_Tensor = dir(Class_Folder);    No_Files_In_Class_Folder(Class_Count) = length(CF_Tensor)-2;    %     strr = sprintf('Reading Test Images...!, # of Classes = %d, Now Reading %d ',No_Folders_In_Training_Set_Folder-2,Class_Count);    %     set(handles.edit3,'String',strr);    drawnow;    for p = 3:No_Files_In_Class_Folder(Class_Count)+2        Tmp_Image_Path = Class_Folder;        Tmp_Image_Name = CF_Tensor(p).name;        Tmp_Image_Path_Name = [Tmp_Image_Path,Tmp_Image_Name];        if strcmp(Tmp_Image_Name,'Thumbs.db')            break        end        test = imread(Tmp_Image_Path_Name);        if length(size(test))==3            Tmp_Image = rgb2gray(test);        else            Tmp_Image = test;        end        Tmp_Image_Down_Sampled = double(imresize(Tmp_Image,[m1 n1]));        Image_Data_Matrix(:,File_Count) = Tmp_Image_Down_Sampled(:);        File_Count = File_Count+1;            end    Class_Count = Class_Count+1;    endclose(h)A = Image_Data_Matrix;A = A/(diag(sqrt(diag(A'*A))));set(handles.edit5,'visible','off');set(handles.edit1,'visible','off'); ​% --- 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)global A m1 n1 No_Files_In_Class_Folder Class_Count Training_Set_Folderset(handles.togglebutton1,'visible','off')set(handles.togglebutton2,'visible','off');set(handles.text2,'visible','off');[Test_File Test_File_Path] = uigetfile('*.jpg;*.pgm;*.png;*.tiff;*.bmp','Select a Test Image');test_image_path = [Test_File_Path Test_File];axes(handles.axes1)claaxes(handles.axes2)claaxes(handles.axes1)imshow(test_image_path);title('test picture ',' color ',' Black ',' fontsize ', 15); drawnow;%  set(handles.edit2,'string',test_image_path);%  set(handles.text5,'Visible','Off'); Test_ File = [Test_File_Path Test_File]; test = imread(Test_File); if length(size(test))==3    Test_ Image = rgb2gray(test); else    Test_ Image = test; endTest_ Image_ Down_ Sampled = double(imresize(Test_Image,[m1 n1])); y = Test_ Image_ Down_ Sampled(:); n = size(A,2);%  minimize norm(x1,1)% subject to% A*x1 == y;%  cvx_ end% figure,plot(x1); f=ones(2*n,1); Aeq=[A -A]; lb=zeros(2*n,1); x1 = linprog(f,[],[],Aeq,y,lb,[],[],[]); x1 = x1(1:n)-x1(n+1:2*n); In the figure bar (x1 / 1000,0.2)% parameter, Y represents the data, and 0.2 represents the width of the column in the histogram, legend('sparsity coefficient ',' WestOutside');nn = No_Files_In_Class_Folder;nn = cumsum(nn);tmp_var = 0;k1 = Class_Count-1;for i = 1:k1    delta_xi = zeros(length(x1),1);    if i == 1        delta_xi(1:nn(i)) = x1(1:nn(i));    else        tmp_var = tmp_var + nn(i-1);        begs = nn(i-1)+1;        ends = nn(i);        delta_xi(begs:ends) = x1(begs:ends);    end    tmp(i) = norm(y-A*delta_xi,2);    tmp1(i) = norm(delta_xi,1)/norm(x1,1);endSparse_Conc_Index = (k1*max(tmp1)-1)/(k1-1);clss = find(tmp==min(tmp));cccc = dir([Training_Set_Folder]);Which_Folder = dir([Training_Set_Folder,cccc(clss+2).name,'\']);Which_Image = randsample(3:length(Which_Folder),1);Image_Path = [Training_Set_Folder,cccc(clss+2).name,'\',Which_Folder(Which_Image).name];Class_Image = (Image_Path);axes(handles.axes2);imshow(Class_Image)title('training picture ',' Color','red','FontSize',15)set(handles.edit1,'visible','on');set(handles.edit1,'string',[cccc(clss+2).name]);set(handles.edit5,'visible','off'); ​% --- 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)g​​​% --- Executes on button press in togglebutton1.function togglebutton1_Callback(hObject, eventdata, handles)% hObject    handle to togglebutton1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)set(handles.togglebutton1,'value',1)% Hint: get(hObject,'Value') returns toggle state of togglebutton1​​% --- Executes on button press in togglebutton2.function togglebutton2_Callback(hObject, eventdata, handles)% hObject    handle to togglebutton2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    structure with handles and user data (see GUIDATA)set(handles.togglebutton2,'value',1); ​% Hint: get(hObject,'Value') returns toggle state of togglebutton2​​% --- Executes during object creation, after setting all properties.function text4_CreateFcn(hObject, eventdata, handles)% hObject    handle to text4 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called​​​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​​​function edit5_Callback(hObject, eventdata, handles)% hObject    handle to edit5 (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 edit5 as text%        str2double(get(hObject,'String')) returns contents of edit5 as a double​​% --- Executes during object creation, after setting all properties.function edit5_CreateFcn(hObject, eventdata, handles)% hObject    handle to edit5 (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 during object creation, after setting all properties.function text2_CreateFcn(hObject, eventdata, handles)% hObject    handle to text2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called​​% --- Executes during object creation, after setting all properties.function text3_CreateFcn(hObject, eventdata, handles)% hObject    handle to text3 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called​​% --- Executes during object creation, after setting all properties.function axes2_CreateFcn(hObject, eventdata, handles)% hObject    handle to axes2 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called​% Hint: place code in OpeningFcn to populate axes2​​% --- Executes during object creation, after setting all properties.function axes1_CreateFcn(hObject, eventdata, handles)% hObject    handle to axes1 (see GCBO)% eventdata  reserved - to be defined in a future version of MATLAB% handles    empty - handles not created until after all CreateFcns called​% Hint: place code in OpeningFcn to populate axes1​

3 simulation results

4 references

[1] Kong Weiting, Gao Jun, Ding zechao, et al Research on facial expression recognition algorithm based on sparse representation [J] Software guide, 2016, 15 (6): 3

Blogger profile: good at matlab simulation in intelligent optimization algorithm, neural network prediction, signal processing, cellular automata, image processing, path planning, UAV and other fields. Relevant matlab code problems can be exchanged through private letters.

Some theories cite online literature. If there is infringement, contact the blogger and delete it.

Keywords: MATLAB Algorithm image processing

Added by bidntrade on Fri, 04 Mar 2022 19:58:53 +0200