PCA算法是人脸识别中最简单的一种识别算法。
1 PCA
PCA(Principal Component Analysis)是常用的数据分析方法。PCA是通过线性变换,将原始数据变换为一组各维度线性无关的数据表示方法,可用于提取数据的主要特征分量,常用于高维数据的降维。
1.1 降维问题
数据挖掘和机器学习中,数据以向量表示。例如某个淘宝店2012年全年的流量及交易情况可以看成一组记录的集合,其中每一天的数据是一条记录,格式如下:
(日期, 浏览量, 访客数, 下单数, 成交数, 成交金额)
其中"日期"是一个记录标志而非度量值,而数据挖掘关心的大多是度量值,因此如果我们忽略日期这个字段后,我们得到一组记录,每条记录可以被表示为一个五维向量,其中一条样本如下所示:
一般习惯上使用列向量表示一条记录,本文后面也会遵循这个准则。
机器学习的很多算法复杂度和数据的维数有着密切关系,甚至与维数呈指数级关联。这里区区5维的数据,也许无所谓,但是实际机器学习中处理成千上万甚至几十万维的数据也并不罕见,在这种情况下,机器学习的资源消耗是不可接受的,因此就会对数据采取降维的操作。降维就意味着信息的丢失,不过鉴于实际数据本身常常存在相关性,所以在降维时想办法降低信息的损失。
例如上面淘宝店铺的数据,从经验可知,"浏览量"和"访客数"往往具有较强的相关性,而"下单数"和"成交数"也具有较强的相关性。可以直观理解为"当某一天这个店铺的浏览量较高(或较低)时,我们应该很大程度上认为这天的访客数也较高(或较低)"。因此,如果删除浏览量或访客数,最终并不会丢失太多信息,从而降低数据的维度,也就是所谓的降维操作。如果把数据降维用数学来分析讨论,用专业名词表示就是PCA,这是一种具有严格数学基础并且已被广泛采用的降维方法。
1.2 向量与基变换
1.2.1 内积与投影
两个大小相同向量的内积被定义如下:
% applied to the GUI before face_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to face_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 face
% Last Modified by GUIDE v2.5 18-Dec-2014 12:02:18
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @face_OpeningFcn, ...
'gui_OutputFcn', @face_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 face is made visible.
function face_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 face (see VARARGIN)
% Choose default command line output for face
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes face wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = face_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;
% --- 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)
% read image to be recognize
global im;
[filename, pathname] = uigetfile({'*.bmp'},'choose photo');
str = [pathname, filename];
im = imread(str);
axes( handles.axes1);
imshow(im);
% --- 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 im
global reference
global W
global imgmean
global col_of_data
global pathname
global img_path_list
% 最小距离法,寻找和待识别图片最为接近的训练图片
for k = 1:col_of_data
temp = norm(objectone - reference(:,k));
if(distance>temp)
aimone = k;
distance = temp;
aimpath = strcat(pathname, '/', img_path_list(aimone).name);
axes( handles.axes2 )
imshow(aimpath)
end
end
% 显示测试结果
% aimpath = strcat(pathname, '/', img_path_list(aimone).name);
% axes( handles.axes2 )
% imshow(aimpath)
% --- Executes on button press in pushbutton3.
% 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)