当前位置: 代码迷 >> 综合 >> 【图像识别】基于PCA+LDA算法人脸识别matlab代码
  详细解决方案

【图像识别】基于PCA+LDA算法人脸识别matlab代码

热度:96   发布时间:2023-12-03 22:44:56.0

1 简介

随着社会的进步和科技的发展,生物特征的识别应用越来越多.如海关及公安系统等需要提供准确的身份认证,日常生活中,人们的安全,财产,隐私等都需要利用先进的科技手段进行保护.因此如何能够高效,快速,可靠识别生物身份的系统被人们普遍研究.本文寻求了一种主成分分析(PCA)和线性判别分析(LDA)相结合的方法对人脸进行识别.

主成分分析(Principal Component Analysis)算法简称 PCA,是一种有效的特征提取方法,算法将人脸图像转换成一个列向量,经过变换后可以有效降低其维数,又能保留有效的识别信息,这些有效信息构成了一组特征图像,并且这些信息对人脸的光照、表情、姿态等具有相当的不敏感性。PCA 变换的目的是通过线性变换,找到一组最优的单位正交向量基(即主成分),用线性组合来重构与原样本均方差的误差最小的一种变换方法。数学上,PCA 通过求解投影矩阵来获得低维度的特征向量。对于某 K 维向量来说,在原有的K 维坐标中它的特征向量在各个维度上的变化均不明显,我们可以通过改变其所处的坐标轴,来使它的特征向量在某些坐标轴上的变化更加明显。例如,在X 轴、Y 轴平面上,将一个椭圆倾斜放置在第一象限内,X、Y 轴上的投影变化度都不是很大,但在将椭圆的长轴作为 X 轴后,椭圆在 X 轴的投影范围变得更大,方差也更大,但在 Y 轴的投影范围变得更小,方差也更小。这时我们就可以消除掉 Y 轴的特征信息,只保留 X 轴的特征信息,这便完成了高维信息向低维的转换。?

2 部分代码

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});
endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
elsegui_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));[OutputName,indx] = Recognition(im, m, V_PCA, V_Fisher, ProjectedImages_Fisher);if indx==j*2-1||indx==2*js=s+1;endend
end
shibielv=s/img_num*100;%识别率
set(handles.text3,'String',['识别率=',num2str(shibielv),'%']);% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject   handle to pushbutton10 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles   structure with handles and user data (see GUIDATA)

3 仿真结果

4 参考文献

[1]樊春玲, and 陈秀霆. "基于PCA和LDA的人脸识别系统设计." 控制工程 19.4(2012):4.

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

5 MATLAB代码与数据下载地址

见博客主页

  相关解决方案