Fix lỗi nhận dạng khuôn mặt trong matlab với thuật toán PCA

  • Thread starter nguyenhuusy
  • Ngày mở chủ đề
N

nguyenhuusy

Author
Mình thử làm nhận dạng khuôn mặt bằng matlab với thuật toán PCA theo link này: http://luanvan.co/luan-van/do-an-xu...ng[MEDIA=youtube]o-tren[/MEDIA]-matlab-31017/

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


% Last Modified by GUIDE v2.5 24-Apr-2017 01:32:15


% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @nhandangkhuonmat1_OpeningFcn, ...
'gui_OutputFcn', @nhandangkhuonmat1_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


end
% --- Executes just before nhandangkhuonmat1 is made visible.
function nhandangkhuonmat1_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 nhandangkhuonmat1 (see VARARGIN)


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


% Update handles structure
guidata(hObject, handles);
% UIWAIT makes nhandangkhuonmat1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end


% --- Outputs from this function are returned to the command line.
function varargout = nhandangkhuonmat1_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;
end
% --- 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)
load TestImage;
axes(handles.axes2);
TrainPath='train';
T = taoCSDL(TrainPath);
[m, A, Eigenfaces] = taoEF(T);
OutputName=nhandien(TestImage, m, A, Eigenfaces);


anhtim=strcat(TrainPath,'\',OutputName);
anhtim=imread(anhtim);


imshow(anhtim);
title('Anh tim duoc');
str=strcat('tenanh:',OutputName);
set(handles.tenanh,'String',str);
end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
[file_name file_path] = uigetfile('*.jpg','Chon anh kiem tra','test\2.jpg'); %L?y file v?i t?t c? ??nh d?ng
if file_path~=0
TestImage=imread([file_path,file_name]);
end
axes(handles.axes1);
if file_path~=0
imshow(TestImage);
end
save TestImage;
end

function T=taoCSDL(trainPath)
csdl=dir(trainPath);
soanh=0;
for i=1:size(csdl,1)
if not(strcmp(csdl(i).name,'.')|strcmp(csdl(i).name,'..')|strcmp(csdl(i).name,'Thumbs.db'))
soanh=soanh+1;
end
end
T=[];
for i=1: soanh
str=int2str(i);
str=strcat('\',str,'.jpg');
str=strcat(trainPath,str);
img=imread(str);
img=rgb2gray(img);
[dong cot]=size(img);
tam=reshape(img',dong*cot,1);
T=[T tam];
end




function[m, A, E]=taoEF(T)
m=mean(T,2);
soanh=size(T,2);
show(m,'Anh trung binh');pause;
A=[];
for i=1:soanh
temp=double(T:),i)) - m;
if i<6
show(temp,'Anh-AnhTB');
pause;
end
A=[A temp];
end
L=A'*A;
[V D]=eig(L);
D1=diag(D);
D1=sort(D1);
s=size(D1);s=s(1);
D1=D1(s-18);
LeigV=[];
for i=1:size(V,2)
if(D(i,i)>D1)
LeigV=[LeigV V:),i)];
end
end
E=A*LeigV;
for i=1:5
anh=E:),i);
show(anh,'Eigenface');
pause;
end
sovector=size(E,2);
for i=1:sovector
dodai=norm(E:),i));
E:),i)=E:),i)/dodai;
end
end

function show(m,t)
im=imread('anhmau.jpg');
try
im=rgb2gray(im);
catch
end
[dong,cot]=size(im);
tam=reshape(m,cot,dong);
tam=tam';
imshow(tam);
dem=1;
for i=1:dong
for j=1:cot
im(i,j)=tam(i,j);
end
end
imshow(im);
title(t)
end
end


function anhtim=nhandien(InputImage, m, A, E)
toado=[];
sovector=size(E,2);
for i=1:sovector
tam=E'*A:),i);
toado=[toado tam];
end
tam=rgb2gray(InputImage);
[dong cot]=size(tam);
InImage=reshape(tam',dong*cot,1);
dolech=double(InImage) - m;
toadoKT=E'*dolech;
hinhchieuKT=double(InImage)*0;
for i=1:sovector
hinhchieuKT=double(InImage)*0;
for i=1:sovector
hinhchieuKT=hinhchieuKT + toadoKT(i,1)*E:),i);
end
show(hinhchieuKT,'Hinh chieu anh len KG anh');pause;
kc=norm(double(InImage)- hinhchieuKT);
str=num2str(kc);
str=strcat('Khoang cach tu anh kiem tra toi khong gian khuon mat:',str);
disp(str);
khoangcach=[];
for i=1:sovector
q=toado:),i);
tam=(norm(toadoKT -q))^2;
khoangcach=[khoangcach tam];
end
[minKC,vitri]=min(khoangcach);
str=num2str(minKC);
str=strcat('Min khoang cach hai toa do hinh chieu:',str);
disp(str);
anhtim=strcat(int2str(vitri),',jpg');
end
end
----------------------------------------------------------------------------------------------------------

Tuy nhiên lúc chạy ấn nút search thì nó bị lỗi như sau:
Undefined function or variable 'taoEF'.


Error in nhandangkhuonmat1>pushbutton2_Callback (line 83)
[m, A, Eigenfaces] = taoEF(T);


Error in gui_mainfcn (line 95)
feval(varargin{:});


Error in nhandangkhuonmat1 (line 42)
gui_mainfcn(gui_State, varargin{:});

Hy vọng các bác chiếu cố giúp đỡ em với =.= mò hết mấy ngày nay rồi mà chưa sửa được huhu
 
Top