概述
matlab实现基于主成分分析的人脸识别
MATLAB代码如下
function varargout = face_recognition(varargin)
% FACE_RECOGNITION MATLAB code for face_recognition.fig
% FACE_RECOGNITION, by itself, creates a new FACE_RECOGNITION or raises the existing
% singleton*.
%
% H = FACE_RECOGNITION returns the handle to a new FACE_RECOGNITION or the handle to
% the existing singleton*.
%
% FACE_RECOGNITION('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in FACE_RECOGNITION.M with the given input arguments.
%
% FACE_RECOGNITION('Property','Value',...) creates a new FACE_RECOGNITION or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before face_recognition_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to face_recognition_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_recognition
% Last Modified by GUIDE v2.5 20-Sep-2017 23:33:17
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @face_recognition_OpeningFcn, ...
'gui_OutputFcn', @face_recognition_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_recognition is made visible.
function face_recognition_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_recognition (see VARARGIN)
% Choose default command line output for face_recognition
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
clear all;
global coeff
global scores
global training_count
global samples_mean
%%
%%训练,
% 人脸库中的15个人来自Yale人脸数据库,另外还采集了2个人的样本,每个人脸的样本数量为11,(可以根据需要再采集一些人脸样本,命名规则为 0xx/xx.jpg)。
%输入人脸样本数
people_count = input('请输入样本数,(输入0为默认值15,当前样本库有18个人) : ');
fprintf('您输入的样本数为: %d nn',people_count);
%每类样本数
face_count_per_people=11;
%输入每类训练样本比例
%training_ratio=.20;
training_ratio = input('请输入每类训练样本比例(输入范围为0-1): ');
if training_ratio< 1 && training_ratio>0
fprintf('您输入的每类训练样本比例为: %.1f nn',training_ratio);
else
fprintf('您输入的每类训练样本有误,系统取默认值为0.5 nn');
training_ratio = 0.5;
end
disp('Waiting for training.......');
% 能量,即较大特征值之和占所有特征值之和的比例。
energy=0.9;
% 每类训练样本数
training_count=floor(face_count_per_people*training_ratio); %floor向后取整,training_ratio=0.7时,training_count为7
%训练样本数据,每行是一个样本
training_samples=[];
% 训练
for i=1:people_count
for j=1:training_count
img=im2double(imread(['样本库' sprintf('%03d',i) '' sprintf('%02d',j) '.jpg']));%读取的地址为 0xx/xx.jpg
img=imresize(img,[10 10]); % 图像缩放至至10*10
%若是彩色图像,则灰度化
if ndims(img)==3
img=rgb2gray(img);
end
training_samples=[training_samples;img(:)'];%将缩放后的10*10图像转为一行,一行代表一副图片的信息,并将其放入training_samples中
end
end
%求取训练样本均值,每行一个样本,每列一类特征
samples_mean=mean(training_samples);%对每一列取平均,得到每个字段的平均值
%whos training_samples
%调用matlab的pca函数
%coeff是主成分系数矩阵,即变换(投影)矩阵
%scores是训练样本投影后的矩阵
%latent是协方差矩阵的特征值,降序排列
[coeff,scores,latent]=pca(training_samples);%对样本进行求协方差矩阵,求平均值和协方差
% whos coeff%100*100
%10*10的图片变换成一行后是100维,training_samples为 [people_count*training_count 100] 其协方差矩阵的特征向量即为coeff是100*100
% whos latent%100*1
%寻找占了energy比例的下标,即主成分就取到这么多维
idx=find(cumsum(latent)./sum(latent)>energy,1);
coeff=coeff(:,1:idx); %取出的主成分系数矩阵
scores=scores(:,1:idx); %训练样本投影矩阵
%figure;
%latent
%latent(1:idx,:)
%subplot 211;hist(latent);title('样本中的主特征值');
%subplot 212;hist(latent(1:idx,:));title('主成分中的主特征值');
disp('Training has Done');
%%
% UIWAIT makes face_recognition wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = face_recognition_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;
%%显示提示信息
global training_count
str = sprintf('当前每个人的训练样本数为:%d,测试样本数为:%d,请从后%d张人脸中选择测试',training_count,11-training_count,11-training_count);
set(handles.text2,'string',str);
%%
% --- 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 img_target
[filename, pathname] = uigetfile({'*.jpg';'*.bmp'},'选择人脸');
str = [pathname, filename];
img_target = imread(str);
axes( handles.axes1);
img_target=im2double(img_target);
%若是彩色图像,则灰度化
if ndims(img_target)==3
img_target=rgb2gray(img_target);
end
imshow(img_target,[]);
%缩放为10*10
img_target=imresize(img_target,[10 10]); % 图像缩放至至10*10
%%
% --- 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 img_target
global coeff
global scores
global training_count
global samples_mean
%测试样本减去训练样本均值,然后投影,得到投影后的样本表示
score=(img_target(:)'-samples_mean)*coeff; % [1 100]*[100 100]
%计算测试样本和每个训练样本之间的欧式距离(计算平方值)
%然后利用最近邻分类器对测试样本进行分类
score_repeat = repmat(score,size(scores,1),1);%把1*idx行的一张图片扩展成people_count*training_count*idx,每一行都一样
distance = (scores-score_repeat).^2;
disp(distance);
[~,idx]=min( sum(distance,2)) ;%对每一行求和,求得最小的一行,就是相当于找最近似的图片
%根据idx找出最近似的训练样本
i = ceil(idx/training_count);%向后取整
j = idx - (i-1)*training_count;
img_dst=im2double(imread(['样本库' sprintf('%03d',i) '' sprintf('%02d',j) '.jpg']));
%将寻找到的结果显示出来
if ndims(img_dst)==3
img_dst=rgb2gray(img_dst);
end
axes( handles.axes2);
imshow(img_dst,[]);
%%
结果截图:
具体源码和训练库在:
https://download.csdn.net/download/qq_44250808/18624399
最后
以上就是舒服豌豆为你收集整理的matlab实现基于主成分分析的人脸识别的全部内容,希望文章能够帮你解决matlab实现基于主成分分析的人脸识别所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复