一、简介
1 PCA
1.1 数据降维
降维的方法包括:主成分分析(PCA)、因子分析(FA)、和独立成分分析(ICA)
主成分分析:寻找向量,使各个样本到该向量的投影之和最小。
因子分析:
独立成分分析:
1.2 PCA:目的是降维,降维的实际原理是最大化目标函数(数据投影后的方差最大)
强推原理博文:blog.csdn.net/fendegao/ar…
(1)假设有m个n维样本: {Z1,Z2,…,Zm}
(2)样本中心 u 为: 所有样本观测值之和/(mxn)
(3)去中心化后,得到矩阵 {X1,X2,…,Xm}={Z1-U,Z2-U,…,Zm-U}
(4)记含有n个元素的向量W,则样本X1在w方向上的投影为二者内积 X1 . W
(5)PCA的目标函数为最大化投影
目标方程可以化为矩阵形式求解,求解方法:
(1)构建拉格朗日算子,求导为0,解得投影最大的向量为特征值最大对应的特征向量。
根据特征值的累计贡献率可以指定选取多少个W向量作为K-L变换矩阵。若选择了4个主成分,则对于每一个n维度样本,经过矩阵变换后,都变为了(1xn)x(nx4)=1x4维向量,即达到了降维的目的。
(2)SVD奇异值分解:降维只需求出右奇异矩阵,即AA(T)的特征向量,不需要求A的协方差矩阵。对内存友好。
1.3 基于PCA的人脸识别
(1)基于人脸样本库,例如现实中人脸拍照(银行、车站)等数据采集方法,建立人脸库。
(2)求取训练人脸库的协方差阵的特征值和特征向量。
(3)对于需要判别的人脸,判断其在特征向量上的投影与哪个训练样本的投影最接近。
!!!注::需要注意,协方差矩阵是维度之间的协方差,故是nxn维,但是在实际应用时,例如图像降维(假设一幅图像有200*10个像素,有100幅图像),一个像素就是一个维度,则原本协方差阵XX’为 (2000x100)x (100x2000)维,计算机存储计算消耗过大,此时可以考虑使用替代矩阵P=X’X(100x2000)x(2000x100)替代:
P的特征值即原始协方差阵的特征值,P的特征向量左乘数据矩阵即为原始协方差阵的特征向量。
2 LDA
LDA:线性判别分析,也称为Fisher线性判别,是常用的降维技术。
基本思想:将高维的模式样本投影到最佳鉴别矢量空间,以达到抽取分类信息和压缩特征空间维数的效果,投影后保证模式样本在新的子空间有最大的类间距离和最小的类内距离,即模式在该空间中有最佳的可分离性。
LDA降维后的维度是直接和类别的个数相关的,与数据本身的维度没关系,比如原始数据是n维的,一共有C个类别,那么LDA降维之后,维数取值范围为(1,C-1),举个例子,假设图像分类,两个类别正例反例,每个图像用10000维特征表示,那么LDA之后,就只有1维特征,并且这维特征的分类能力最好。
对于很多两类分类的情况,LDA之后就剩下1维,找到分类效果最好的一个阈值貌似就可以了。
假设,x是一个N维的列向量,要使x通过LDA降到C维,只需要找到一个投影矩阵w,也即,一个N*C的矩阵,让w的转置矩阵和x相乘,便成为C维的了。(投影在数学上的表示就是用一个矩阵去相乘)
此时,问题的关键就在于:寻找一个投影矩阵!并且,该投影矩阵要保证模式样本在新的子空间有最大的类间距离和最小的类内距离。
2.2 LDA数学表示:
二、源代码
function varargout = faceCore(varargin)
% FACECORE M-file for faceCore.fig
% FACECORE, by itself, creates a new FACECORE or raises the existing
% singleton*.
%
% H = FACECORE returns the handle to a new FACECORE or the handle to
% the existing singleton*.
%
% FACECORE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FACECORE.M with the given input arguments.
%
% FACECORE('Property','Value',...) creates a new FACECORE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before faceCore_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to faceCore_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
% Copyright
% Edit the above text to modify the response to help faceCore
% Last Modified by GUIDE v2.5 11-Apr-2021 23:45:58
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @faceCore_OpeningFcn, ...
'gui_OutputFcn', @faceCore_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 faceCore is made visible.
function faceCore_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 faceCore (see VARARGIN)
% Choose default command line output for faceCore
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes faceCore wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = faceCore_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)
global TrainDatabasePath ;
TrainDatabasePath = uigetdir(strcat(matlabroot,'\work'), '训练库路径选择...' );
% --- 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 TestDatabasePath;
TestDatabasePath = uigetdir(strcat(matlabroot,'\work'), '测试库路径选择...');
% --- 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)
%[filename,pathname]=uigetfile({'*.jpg';'*.bmp'},'');
%str=[pathname filename];
%im=imread(str);
%axes(handles.axes1);
%imshow(im);
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global TrainDatabasePath ;
global TestDatabasePath;
global T;
T = CreateDatabase(TrainDatabasePath);
%[m V_PCA V_Fisher ProjectedImages_Fisher] = FisherfaceCore(T);
% --- Executes on button press in pushbutton5.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global im;
[filename,pathname]=uigetfile({'*.jpg';'*.bmp'},'选择测试图片...');
str=[pathname filename];
im=imread(str);
axes(handles.axes1);
imshow(im);
% --- 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)
%T = CreateDatabase(TrainDatabasePath);
global T;
global im;
global TrainDatabasePath ;
[m V_PCA V_Fisher ProjectedImages_Fisher] = FisherfaceCore(T);
OutputName = Recognition(im, m, V_PCA, V_Fisher, ProjectedImages_Fisher);
SelectedImage = strcat(TrainDatabasePath,'\',OutputName);
SelectedImage = imread(SelectedImage);
axes(handles.axes2);
imshow(SelectedImage);
%title('Equivalent Image');
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% clear all;
% clc
% close(gcf);
global T;
global TrainDatabasePath ;
[m V_PCA V_Fisher ProjectedImages_Fisher] = FisherfaceCore(T);
file_path = '.\TestDatabase\';% 图像文件夹路径
img_path_list = dir(strcat(file_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);%获取图像总数量
s=0;%识别成功个数
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
image_name = img_path_list(j).name;% 图像名
im = imread(strcat(file_path,image_name));
function [m_database V_PCA V_Fisher ProjectedImages_Fisher] = FisherfaceCore(T)
% Use Principle Component Analysis (PCA) and Fisher Linear Discriminant (FLD) to determine the most
% discriminating features between images of faces.
%1
% Description: This function gets a 2D matrix, containing all training image vectors
% and returns 4 outputs which are extracted from training database.
% Suppose Ti is a training image, which has been reshaped into a 1D vector.
% Also, P is the total number of MxN training images and C is the number of
% classes. At first, centered Ti is mapped onto a (P-C) linear subspace by V_PCA
% transfer matrix: Zi = V_PCA * (Ti - m_database).
% Then, Zi is converted to Yi by projecting onto a (C-1) linear subspace, so that
% images of the same class (or person) move closer together and images of difference
% classes move further apart: Yi = V_Fisher' * Zi = V_Fisher' * V_PCA' * (Ti - m_database)
%
% Argument: T - (M*NxP) A 2D matrix, containing all 1D image vectors.
% All of 1D column vectors have the same length of M*N
% and 'T' will be a MNxP 2D matrix.
%
% Returns: m_database - (M*Nx1) Mean of the training database
% V_PCA - (M*Nx(P-C)) Eigen vectors of the covariance matrix of the
% training database
% V_Fisher - ((P-C)x(C-1)) Largest (C-1) eigen vectors of matrix J = inv(Sw) * Sb
% ProjectedImages_Fisher - ((C-1)xP) Training images, which are projected onto Fisher linear space
%
% See also: EIG
% Original version by Amir Hossein Omidvarnia, October 2007
% Email: aomidvar@ece.ut.ac.ir
Class_number = ( size(T,2) )/2; % Number of classes (or persons) 矩阵 T 的列数 除以2,也就是 训练图片数量的一半
Class_population = 2; % Number of images in each class
P = Class_population * Class_number; % Total number of training images
%%%%%%%%%%%%%%%%%%%%%%%% calculating the mean image
m_database = mean(T,2); %样本均值
%%%%%%%%%%%%%%%%%%%%%%%% Calculating the deviation of each image from mean image 每张图片与均值图片的背离程度
A = T - repmat(m_database,1,P);
%%%%%%%%%%%%%%%%%%%%%%%% Snapshot method of Eigenface algorithm
L = A'*A; % L is the surrogate(代理人) of covariance matrix C=A*A'.协方差矩阵
[V D] = eig(L); % Diagonal elements of D are the eigenvalues for both L=A'*A and C=A*A'.
%%%%%%%%%%%%%%%%%%%%%%%% Sorting and eliminating small eigenvalues
L_eig_vec = [];
for i = 1 : P-Class_number
L_eig_vec = [L_eig_vec V(:,i)];
end
end
end
end