【人脸识别】基于matlab GUI KL变换人脸识别【含Matlab源码 859期】

189 阅读2分钟

一、简介

基于matlab GUI KL变换人脸识别

二、源代码

function varargout = renlian(varargin)
% RENLIAN MATLAB code for renlian.fig
%      RENLIAN, by itself, creates a new RENLIAN or raises the existing
%      singleton*.
%
%      H = RENLIAN returns the handle to a new RENLIAN or the handle to
%      the existing singleton*.
%
%      RENLIAN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in RENLIAN.M with the given input arguments.
%
%      RENLIAN('Property','Value',...) creates a new RENLIAN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before renlian_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to renlian_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 renlian

% L 

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @renlian_OpeningFcn, ...
                   'gui_OutputFcn',  @renlian_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 renlian is made visible.
function renlian_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 renlian (see VARARGIN)

% Choose default command line output for renlian
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes renlian wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = renlian_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 selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
str=get(handles.listbox1,'string');
v=get(handles.listbox1,'value');
a=[str{v} '.bmp'];
axes(handles.axes1)
imshow(a);
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1


% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox 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


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
N=75;
q=5;
x=zeros(10000,15);
for i=1:N
    a=[num2str(i) '.bmp'];
h=imread(a);
[m n]=size(h);
x(:,i)=reshape(h,m*n,1);
end

pingjun=mean(x,2);
d=repmat(pingjun,1,N);
A=x-d;
y=A'*A;
[v u]=eig(y);
tezhengzhi=sum(u);
[tezhengzhi,xulie]=sort(tezhengzhi,2,'descend');


for i=1:q
tezhenglian(:,i)=A*v(:,xulie(i))*(tezhengzhi(i)^(-0.5));%特征脸
end



for i=1:N
P(:,i)=tezhenglian'*A(:,i);
end
str=get(handles.listbox1,'string');
v=get(handles.listbox1,'value');
a=[str{v} '.bmp'];
z=zeros(10000,1);
h=imread(a);
[m n]=size(h);
z(:,1)=reshape(h,m*n,1);
shibie=tezhenglian'*(z-pingjun);%投影


chonggou=tezhenglian*shibie+pingjun;%重构
fangcha=((z-chonggou)'*(z-chonggou))^0.5;
yuzhi=0;
for i=1:N
    for j=i:N
        zanshiyuzhi=((P(:,i)-P(:,j))'*(P(:,i)-P(:,j)))^0.5;
        if zanshiyuzhi>yuzhi
            yuzhi=zanshiyuzhi;
        end
    end
end
yuzhi=yuzhi/2;



juli=9e+009;
for i=1:N
    bijiao=((shibie-P(:,i))'*(shibie-P(:,i)))^0.5;
    if bijiao<juli;
        juli=bijiao;
        k=i;
    end
end

if fangcha>=yuzhi flag=1;
elseif fangcha<yuzhi&&juli>=yuzhi flag==2;
elseif fangcha<yuzhi&&juli<yuzhi flag=3;
end  
if flag==1
  a=[ '0.png'];
axes(handles.axes3)
imshow(a);
set(handles.edit1,'string','未被识别,请重新采集');
elseif flag==2
 a=[ '0.png'];
axes(handles.axes3)
imshow(a);
set(handles.edit1,'string','输入图像包含未知人脸');   
elseif flag==3
    if k>15
    ren=rem(k-1,15)+1;
    else ren=k;
   end
a=[num2str(k) '.bmp'];
axes(handles.axes3)
imshow(a);
set(handles.edit1,'string',ren)
end


    


% 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)

三、运行结果

在这里插入图片描述
在这里插入图片描述

四、备注

版本:2014a