现在有一段代码“function T = CreateDatabase(TrainDatabasePath)
%%%%%%%读取训练库路径,并统计样本个数
TrainFiles = dir(TrainDatabasePath);
Train_Number = 0;
for i = 1:size(TrainFiles,1)
if not(strcmp(TrainFiles(i).name,'.')|strcmp(TrainFiles(i).name,'..')|strcmp(TrainFiles(i).name,'Thumbs.db'))
Train_Number = Train_Number + 1; % Number of all images in the training database
end
end
%%%%%%%%将2维图像转化为一维向量
T = [];
for i = 1 : Train_Number
%获取图像的数字代号
str = int2str(i);
str = strcat('\',str,'.jpg');
str = strcat(TrainDatabasePath,str);
img = imread(str);
img = rgb2gray(img);
[irow icol] = size(img);
temp = reshape(img',irow*icol,1); % 将二维矩阵转换为一维矩阵
T = [T temp];
end
function [m, A, Eigenfaces,D] = EigenfaceCore(T)
%%%求样本的平均向量
m = mean(T,2); % Computing the average face image m = (1/P)*sum(Tj's) (j = 1 : P)
Train_Number = size(T,2);
%%%%计算每个样本与平均向量的差向量
A = [];
for i = 1 : Train_Number
temp = double(T(:,i)) - m; %计算每个样本的减去平均人脸
A = [A temp]; % 合成一个N*N矩阵
end
%%%%%%%%%%%%%%%%%%%%%%%% 求解特征值和特征向量
L = A'*A; %.计算协方差矩阵
[V D] = eig(L); %.求特征向量和特征
% 选取特征向量
L_eig_vec = [];
for i = 1 : size(V,2)
if( D(i,i)>1 )
L_eig_vec = [L_eig_vec V(:,i)];
end
end
% 降维
Eigenfaces = A * L_eig_vec; % 特征脸获取
function OutputName = Recognition(TestImage, m, A, Eigenfaces)
ProjectedImages = [];
Train_Number = size(Eigenfaces,2);
for i = 1 : Train_Number
temp = Eigenfaces'*A(:,i); %e将每个样本投影到特征空间
ProjectedImages = [ProjectedImages temp];
end
%%%%%%%%%%%%%%%%%%%%%%%% 从测试人脸库计算提取特征
InputImage = imread(TestImage);
temp = InputImage(:,:,1);
[irow icol] = size(temp);
InImage = reshape(temp',irow*icol,1);
Difference = double(InImage)-m;
ProjectedTestImage = Eigenfaces'*Difference; % 将待测样本投影到特征空间
%%%%%%%%%%%%%%%%%%%%%%%% 按照欧式距离取最小的原则得出匹配的人脸
Euc_dist = [];
for i = 1 : Train_Number
q = ProjectedImages(:,i);
temp = ( norm( ProjectedTestImage - q ) )^2;
Euc_dist = [Euc_dist temp];
end
%选取最小欧式距离的人脸
[Euc_dist_min , Recognized_index] = min(Euc_dist);
%获取该人脸的数字代号
OutputName = strcat(int2str(Recognized_index),'.jpg');
function varargout = untitled2(varargin)
% UNTITLED2 MATLAB code for untitled2.fig
% UNTITLED2, by itself, creates a new UNTITLED2 or raises the existing
% singleton*.
%
% H = UNTITLED2 returns the handle to a new UNTITLED2 or the handle to
% the existing singleton*.
%
% UNTITLED2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in UNTITLED2.M with the given input arguments.
%
% UNTITLED2('Property','Value',...) creates a new UNTITLED2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before untitled2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to untitled2_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 untitled2
% Last Modified by GUIDE v2.5 01-May-2019 15:22:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @untitled2_OpeningFcn, ...
'gui_OutputFcn', @untitled2_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 untitled2 is made visible.
function untitled2_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 untitled2 (see VARARGIN)
% Choose default command line output for untitled2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = untitled2_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
% --- 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)
% You can customize and fix initial directory paths
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)
global TestDatabasePath;
global TrainDatabasePath;
%读取人脸库的路径
prompt = {'输入库内测试人脸数字代表'};
dlg_title = 'Input of PCA-Based Face Recognition System';
num_lines= 1;
def = {'1'};
TestImage = inputdlg(prompt,dlg_title,num_lines,def);
temp = TestImage;
TestImage = strcat(TestDatabasePath,'\',char(TestImage),'.jpg');
im = imread(TestImage);
T = CreateDatabase(TrainDatabasePath);
[m, A, Eigenfaces,D] = EigenfaceCore(T);
%画出特征值得图像
global OutputName;
OutputName = Recognition(TestImage, m, A, Eigenfaces);
SelectedImage = strcat(TrainDatabasePath,'\',OutputName);
SelectedImage = imread(SelectedImage);
%输出测试图片
axes(handles.axes1);
imshow(im);
%输出匹配图片
axes(handles.axes2);
imshow(SelectedImage);
%结果显示名字
season = 'null';
switch OutputName
case {'1.jpg','2.jpg'}
if(char(temp) == '1')
season='成龙';
else
msg = msgbox('未找到账户!');
season='null';
end
case {'3.jpg','4.jpg'}
if(char(temp) == '2')
season='胡军';
else
msg = msgbox('未找到账户!');
season='null';
end
case {'5.jpg','6.jpg'}
if(char(temp) == '3')
season='张子枫';
else
msg = msgbox('未找到账户!');
season='null';
end
case {'7.jpg','8.jpg'}
if(char(temp) == '4')
season='庄达菲';
else
msg = msgbox('未找到账户!');
season='null';
end
case {'9.jpg','10.jpg'}
if(char(temp) == '5')
season='李云龙';
else
msg = msgbox('未找到账户!');
season='null';
end
case {'11.jpg','12.jpg'}
if(char(temp) == '6')
season='楚云飞';
else
msg = msgbox('未找到账户!');
season='null';
end
case {'13.jpg','14.jpg'}
if(char(temp) == '7')
season='科比';
else
msg = msgbox('未找到账户!');
season='null';
end
case {'15.jpg','16.jpg'}
if(char(temp) == '8')
season='Green';
else
msg = msgbox('未找到账户!');
season='null';
end
case {'17.jpg','18.jpg'}
if(char(temp) == '9')
season='Jack';
else
msg = msgbox('未找到账户!');
season='null';
end
case {'19.jpg','20.jpg'}
if(char(temp) == '10')
season='Tom';
else
msg = msgbox('未找到账户!');
season='null';
end
otherwise
season='null'
end
str = strcat('库内匹配结果 : ',OutputName);
set(handles.result,'string',str);
%结果显示图片序号
str = strcat('库内匹配结果 : ',season);
set(handles.result,'string',str);
%画出特征值得图像
% figure;
% x=1:20;
% D=diag(D);
% y = flipud(D);
% plot(x,y,'.')
% xlabel('数');
% ylabel('特征值大小');
function result_Callback(hObject, eventdata, handles)
% hObject handle to result (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 result as text
% str2double(get(hObject,'String')) returns contents of result as a double
global OutputName;
axes(handles.result);
str = strcat('Matched image is : ',OutputName);
set(handles.result,'string',str);
% --- Executes during object creation, after setting all properties.
function result_CreateFcn(hObject, eventdata, handles)
% hObject handle to result (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
x=('人脸');
fprintf(x);
figure;
title('人脸');”
请按照此要求修改,“1.通过摄像头可实现对人脸图像的实时获取。
2. 对人脸图像进行预处理。
3. 对处理后的人脸图像进行准确定位,并用方框对人脸区域进行标注。
4. 在图像中有多个人脸时可进行区分和识别。
5. 在实时检测到人脸时有声音提示”要求调动电脑自带摄像头。
最新发布