1 简介

随着模式识别、图像处理及机器学习的发展,快速有效的自动识别与验证技术由于其巨大的理论及实际应用价值吸引了广泛关注。作为生物识别中重要的验证手段,人脸识别在信息安全、刑事侦查和视频监控等领域获得了巨大发展,广泛用于智能家居、平安城市、智能监控及智能建筑等。基于稀疏表示分类(SRC)的人脸识别算法由于其对噪声和局部遮挡的鲁棒性成为了研究热点,SRC基本思想是利用测试样本在训练集上的线性表示来最小化重建误差进行分类。首先利用优化问题求解获得测试样本在不同类别训练样本上的稀疏表示系数,通过训练样本线性组合获得重建样本,最后求得重建残差最小的类为分类结果。

SRC 是人脸识别中一种典型算法。该算法不考虑样本的局部信息而是考虑整体信息,假设测试样本可近似表示为所有训练样本的线性组合,每个训练样本的像素信息转换成列向量从而组成训练矩阵。由于CS 的非自适应性,投影训练矩阵的正交基可以得到稀疏矩阵,并且不会影响原问题的解决。这个稀疏矩阵就是对训练图像的稀疏表示,也称为测量矩阵。SRC算法的步骤如下:

【表情识别】基于稀疏表示特征模板匹配算法实现人脸表情识别含Matlab源码_2d

【表情识别】基于稀疏表示特征模板匹配算法实现人脸表情识别含Matlab源码_ide_02

2 部分代码

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 EDIT
gui_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 FacialExpressionRecognitiontool
set(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)
cla
axes(handles.axes1)
cla
handles.output = hObject;
% Update handles structure
guidata(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 structure
varargout{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;
end
close(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_Folder
set(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)
cla
axes(handles.axes2)
cla
axes(handles.axes1)
imshow(test_image_path);
title('测试图片','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;
end
Test_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);
figure
bar(x1/1000,0.2) %参数中y表示数据,0.2表示柱状图中柱子的宽度
legend('稀疏系数','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);
end
Sparse_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('训练图片','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 仿真结果

【表情识别】基于稀疏表示特征模板匹配算法实现人脸表情识别含Matlab源码_2d_03

【表情识别】基于稀疏表示特征模板匹配算法实现人脸表情识别含Matlab源码_ide_04

4 参考文献

[1]孔玮婷, 皋军, 丁泽超,等. 基于稀疏表示的人脸表情识别算法研究[J]. 软件导刊, 2016, 15(6):3.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。

【表情识别】基于稀疏表示特征模板匹配算法实现人脸表情识别含Matlab源码_ide_05