8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
1/25
GANNON UNIVERSITY
Electrical & Computer Engineering Department
Digital Image Processing
GUI-Face Recognition using PCA on MATLAB
Report Prepared by:
xxxxx
Instructorxxxxx
May.6.2014
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
2/25
1. Summary
This is the summary of the basic idea about PCA and the papers about the face
recognition using PCA.
The Principal Component Analysis (PCA) is one of the most successful techniques
that have been used in image recognition and compression. PCA is a statistical
method under the broad title of factor analysis. The purpose of PCA is to reduce the
large dimensionality of the data space (observed variables) to the smaller intrinsic
dimensionality of feature space (independent variables), which are needed to describe
the data economically. This is the case when there is a strong correlation between
observed variables.The jobs which PCA can do are prediction, redundancy removal, feature extraction,
data compression, etc.
Because PCA is a classical technique which can do something in the linear domain,
applications having linear models are suitable, such as signal processing, image
processing, system and control theory, communications, etc.
Face recognition has many applicable areas. Moreover, it can be categorized into face
identication, face classication, or sex determination. The most useful applications
contain crowd surveillance, video content indexing, personal id entication (ex.
drivers license), mug shots matching, entrance security, etc.
The main idea of using PCA for face recognition is to express the large 1-D vector of
pixels constructed from 2-D facial image into the compact principal components of
the feature space. This can be called eigenspace projection.
Eigenspace is calculated by identifying the eigenvectors of the covariance matrix
derived from a set of facial images(vectors).
The details are described in the following section.
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
3/25
2. Block diagram of the process
Face Input(Get from Face Database orFace Capture using Camera)
Face Verification (matching) Module
- Calculate PCA features of Database imageand Input image (using covariance matrix andeigenfaces)
- Database images resizing for matching withinput image
- Find correlation between two feature vectorsusing standard formula (norm)
Matching test withusing above correlation
Output:1. Read & output face image
from databse2. Green signal show
Output:- No face in
database- Red signal show
No
Yes
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
4/25
3. Implementation
This projects is organized into 5 modules
3.1 Function to Load image from computer (Module1)
- MATLAB Codefunction pushbutton1_Callback(hObject, eventdata, handles)
% function to Load Image from computer option
[filename, user_canceled] = imgetfile; % ask user to select image
if user_canceledmsgbox( 'File not selected' , 'Error' ,'error' )
else
test_image=imread(filename); % read and load the selected image in matlab
axes(handles.axes1);
imshow(test_image) % display the test image
cla(handles.axes2); % clear previously opened images on GUI
cla(handles.axes3);
set(handles.text6, 'string' ,'') % display on GUI
end
- Function analysis in this module
Table 1. Module1 function description
Function name Descriptionimgetfile Open Image dialog boxmsgbox Create and open message box
imread Read image from graphics file
imshow Display image
cla Clear current axe
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
5/25
set Set Handle Graphics object properties
rgb2gray Convert RGB image or colormap to grayscalesize Image resize (Array Dimensions)
3.2 function for matching algorithm (Module2)
- MATLAB Codeglobal sze I_test % global variables
A=PCA_features(I_test, 2); % calculate PCA features
A=A(:); % Make it feature array
database_images = 41; % number of images in database images
% loop based procedure to load each image from database sequentially (Broad Design Steps 1b)
for k=1:database_images
Imd=imread(strcat( 'Database_large\' ,num2str(k), '.jpg' )); % read database image
Im=rgb2gray(Imd); % RGB to gray conversion
I1=double(imresize(Im, sze));
% resizing database image same as test image (Broad Design Steps 2)
% matching stage
B=PCA_features(I1, 2);
B=B(:); % Feature array
% Find correlation between two feature vectors using
% standard formula, (Broad Design Steps 3)
corr(k) = sum(A.*B)/(norm(A)*norm(B));
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
6/25
end
Max_Match=max(corr); % find maximum correlation
% Check if correlation >0.60 then match successful
if Max_Match>0.80
ind=find(corr==Max_Match);
Match_image=imread(strcat( 'Database_large\' ,num2str(ind), '.jpg' )); % read database
image
axes(handles.axes2); % axes handle for GUI
imshow(Match_image) % displaying the matched image
axes(handles.axes3); % 3rd axes control for displaying red/green LED
% display circle with green color in case we find a match
rectangle( 'Position' ,[1,2,0.5,1], 'Curvature' ,[1,1], 'FaceColor' ,'g')
% calculate confidence value with correlation 0.60 = 50% criterea
conf_value=abs((Max_Match-0.60))*60/50*100+50;
set(handles.text6, 'string' ,num2str(round(conf_value))); % display on GUI
else
axes(handles.axes3);
% display red LED in case no match is found
rectangle( 'Position' ,[1,2,0.5,1], 'Curvature' ,[1,1], 'FaceColor' ,'r')
set(handles.text6, 'string' ,'--' ); % display on GUI
end
- Function analysis in this module
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
7/25
Table 2. Module2 function descriptionFunction name Description
PCA_features This function extracts the PCA Features of the Input Image with 'k' newdimensionsk= eigen values reduction parameter
Corr Linear or tank correlation
Sum Sum of array elementsnorm Vector and matrix normsmax Largest elements in arrayFind Find indics and values of nonzero elementsaxes Create axes graphics objectRectangle Create 2-D rectangle object and showSet Set Handle Graphics properties
3.3 Function for camera set and control (Module 3)
- MATLAB Codeglobal c
c=1;
axes(handles.axes1);
% figure,
vidObj = videoinput( 'winvideo' ,1); % For windows users, videoinput
% vidObj = videoinput('macvideo',1); % For Mac users, videoinput
set(vidObj, 'ReturnedColorSpace' , 'RGB' ); % set returned frame in RGB format
videoRes = get(vidObj, 'VideoResolution' );
numberOfBands = get(vidObj, 'NumberOfBands' );
handleToImage = image( zeros([videoRes(2), videoRes(1), numberOfBands], 'uint8' ) );
preview(vidObj, handleToImage);
handles.vidObj = vidObj; % add the video data to GUI handle
guidata(hObject,handles)
- Function analysis in this module
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
8/25
Table 3. Module1 function description
Function name DescriptionVideoinput Create video input object
Get Query Handle Graphics object propertiesImage Display image objectPreview Preview of live video dataGuidata Store or retrieve GUI data
3.4 Function for Face snapshot on camera video (Module4)
- MATLAB Code
global sze I_test c
if c==1
sze=[512 512]; % set default size for image captured by webcam
frame = getsnapshot(handles.vidObj); % get snapshot from video
[H, W, a]=size(frame);
c1=0.2; % top and bottom clipping of snaphot image to get face area
c2=0.3; % left and right clipping of snaphot image to get face area
clip_H=round(c1*H);
clip_W=round(c2*W);
f1=frame(clip_H:H-clip_H, clip_W:W-clip_W,:); % clip the frame
axes=handles.axes1;
imshow(f1);
f1=rgb2gray(f1); % convert RGB image to gray scale image
I_test=double(imresize(f1,sze));
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
9/25
% clear all previously opened data/images on GUI
cla(handles.axes2); % clear previously opened images on GUI
cla(handles.axes3);
set(handles.text6, 'string' ,'') % display on GUI
c=0;
else end
- Function analysis in this module
Table 4. Module4 function descriptionFunction name Description
getsnapshot Immediately return single image frameframe(clip_H:H-clip_H, clip_W:W-clip_W,:)
Clip the frame
3.5 Calculate PCA features of the Input Image & Test Image with k newdimensions (Module5)
- MATLAB codefunction features=PCA_features(I, k)
if numel(size(I))==3 I = rgb2gray(I);
end Id = im2double(I);
meen = mean(Id); [a ~] = size(Id); I_Adjust = Id - repmat(meen,a,1); % mean subtraction
cov_data = cov(I_Adjust); % covariance matrix
[feature_vector,~] = eigs(cov_data,k); % eigen values of covariance matrix
features=feature_vector'*I_Adjust'; % Final Data
- Function analysis
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
10/25
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
11/25
4. Test and validation
Fig 1. Face Recognition Initial GUI
Case 1: Loading image from Computer
Fig 2. Load Image from Computer
The GUI will appear and looklike this
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
12/25
The selected image will be displayed on the left panel as shown below.
Fig 3. Loaded image show on the left panel
The selected image will be displayed on the left panel as shown below. Now click on ExecuteMatching Algorithm.
Fig 4. Excute Face Matching Algorithm Button Click
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
13/25
If the test image is in the database, itll be displayed in the next pane and green LED will bedisplayed. Match confidence will also be displayed.
Shown below is the case when a match is found and green LED is ON.
Fig 5. Matching result show (green: matching success!)
Shown below is the case when no match is found and red LED is ON.
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
14/25
Fig 6. Matching result show (red: matching failed!)
Case 2: Capture image from webcam
To match a particular face, its similar image must be in database folder else it may not berecognized. For example, I put my photos in the database for its testing. The images weretaken and cropped so that only face area is included only.
Fig 7. Standard Captured Face Image from camera
To test the matching algorithm for input image taken by webcam.
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
15/25
Fig 8. Capture Image from Camera button click for using Camera Capture
By pressing the button Capture Image from Camera, the webcam will be enabled anditll show video in the test image pane on the left. See figure below.
Fig 9. Camera video showing
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
16/25
Capture button takes the snapshot and after cropping, takes the middle portion.
Fig 10. Face Capture from Camera by Capture button click
Now run the Execute Face Matching Algorithm and see the result as shown below.
Fig 11. Matching result show using Camera (green: matching success!)
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
17/25
Note: For good matching results, you are required to pose your face in the mid-section of thevideo. The snapshot is cropped and only middle section is used for matching purpose. Do not take
snapshot while the face is not in middle portion. Please see the following example.
Fig 12. Face Capture guide where using camera
After capturing, the test image will not contain full face.
Fig. 13. Face Capture failed show(because you didnt face capture in capture windows middle)
Face not inmiddle
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
18/25
5. Discussion and conclusions
In this paper we researched Face Recognition using PCA and implemented it in MATLAB.
We considered this program in image get from local computer and using camera capture.
The test result images show advantage and disadvantage of this algorithm.
In the future, we will good methods for Face Recognition.
Thanks.
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
19/25
References
[1] M.A. Turk and A.P. Pentland, Face Recognition Using Eigenfaces, IEEE Conf. on
Computer Vision and Pattern Recognition, pp. 586-591, 1991.
[2] K. I. Diamantaras and S. Y. Kung, Principal Component Neural Networks: Theory andApplications, John Wiley & Sons,Inc., 1996.
[3] Alex Pentland, Baback Moghaddam, and Thad Starner, View -Based and Modular
Eigenspaces for Face Recognition, IEEE Conf. on Computer Vision and Pattern Recognition,
MITMedia Laboratory Tech. Report No. 245 1994
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
20/25
Appendix- Face_Match.m
function varargout = Face_Match(varargin) % FACE_MATCH MATLAB code for Face_Match.fig % FACE_MATCH, by itself, creates a new FACE_MATCH or raises the existing % singleton*. %
% H = FACE_MATCH returns the handle to a new FACE_MATCH or the handle to % the existing singleton*. % % FACE_MATCH('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in FACE_MATCH.M with the given input arguments. % % FACE_MATCH('Property','Value',...) creates a new FACE_MATCH or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before Face_Match_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to Face_Match_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_Match
% Last Modified by GUIDE v2.5 12-Apr-2014 13:44:56
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct( 'gui_Name' , mfilename, ...
'gui_Singleton' , gui_Singleton, ...
'gui_OpeningFcn' , @Face_Match_OpeningFcn, ... 'gui_OutputFcn' , @Face_Match_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_Match is made visible. function Face_Match_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_Match (see VARARGIN)
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
21/25
% Choose default command line output for Face_Match handles.output = hObject;
% Update handles structure guidata(hObject, handles); global c c=0; clc
% UIWAIT makes Face_Match wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = Face_Match_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)
% function to Load Image from computer option
[filename, user_canceled] = imgetfile; % ask user to select image
if user_canceled
msgbox( 'File not selected' , 'Error' ,'error' ) else test_image=imread(filename); % read and load the selected image in matlab axes(handles.axes1); imshow(test_image) % display the test image
cla(handles.axes2); % clear previously opened images on GUI cla(handles.axes3); set(handles.text6, 'string' ,'') % display on GUI
end
global sze I_test c I_test=double(rgb2gray(test_image)); % convert RGB image to gray scale image sze=size(I_test); % size of test image c=0;
% --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % function for matching algorithm button
global sze I_test % global variables
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
22/25
A=PCA_features(I_test, 2); % calculate PCA features A=A(:); % Make it feature array
database_images = 41; % number of images in database images
% loop based procedure to load each image from database sequentially (Broad Design Steps 1b) for k=1:database_images
Imd=imread(strcat( 'Database_large\' ,num2str(k), '.jpg' )); % read database image
Im=rgb2gray(Imd); % RGB to gray conversion
I1=double(imresize(Im, sze)); % resizing database image same as test image (Broad Design Steps 2)
% matching stage
B=PCA_features(I1, 2); B=B(:); % Feature array
% Find correlation between two feature vectors using % standard formula, (Broad Design Steps 3)
corr(k) = sum(A.*B)/(norm(A)*norm(B));
end
Max_Match=max(corr); % find maximum correlation % Check if correlation >0.60 then match successful
if Max_Match>0.80 ind=find(corr==Max_Match); Match_image=imread(strcat( 'Database_large\' ,num2str(ind), '.jpg' )); % read database image axes(handles.axes2); % axes handle for GUI imshow(Match_image) % displaying the matched image
axes(handles.axes3); % 3rd axes control for displaying red/green LED
% display circle with green color in case we find a match rectangle( 'Position' ,[1,2,0.5,1], 'Curvature' ,[1,1], 'FaceColor' ,'g')
% calculate confidence value with correlation 0.60 = 50% criterea conf_value=abs((Max_Match-0.60))*60/50*100+50;
set(handles.text6, 'string' ,num2str(round(conf_value))); % display on GUI
else axes(handles.axes3);
% display red LED in case no match is found rectangle( 'Position' ,[1,2,0.5,1], 'Curvature' ,[1,1], 'FaceColor' ,'r')
set(handles.text6, 'string' ,'--'); % display on GUI end
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
23/25
% --- Executes on button press in pushbutton3. function pushbutton3_Callback(hObject, eventdata, handles) % Function for camera button global c c=1; axes(handles.axes1); % figure, vidObj = videoinput( 'winvideo' ,1); % For windows users, videoinput % vidObj = videoinput('macvideo',1); % For Mac users, videoinput
set(vidObj, 'ReturnedColorSpace' , 'RGB' ); % set returned frame in RGB format
videoRes = get(vidObj, 'VideoResolution' ); numberOfBands = get(vidObj, 'NumberOfBands' );
handleToImage = image( zeros([videoRes(2), videoRes(1), numberOfBands], 'uint8' ) );
preview(vidObj, handleToImage);
handles.vidObj = vidObj; % add the video data to GUI handle guidata(hObject,handles)
% --- Executes on button press in pushbutton5. function pushbutton5_Callback(hObject, eventdata, handles)
global sze I_test c if c==1
sze=[512 512]; % set default size for image captured by webcam frame = getsnapshot(handles.vidObj); % get snapshot from video
[H, W, a]=size(frame); c1=0.2; % top and bottom clipping of snaphot image to get face area c2=0.3; % left and right clipping of snaphot image to get face area clip_H=round(c1*H); clip_W=round(c2*W);
f1=frame(clip_H:H-clip_H, clip_W:W-clip_W,:); % clip the frame axes=handles.axes1; imshow(f1);
f1=rgb2gray(f1); % convert RGB image to gray scale image
I_test=double(imresize(f1,sze));
% clear all previously opened data/images on GUI cla(handles.axes2); % clear previously opened images on GUI cla(handles.axes3); set(handles.text6, 'string' ,'') % display on GUI
c=0; else end
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
24/25
- PCA_features.m
%% This function extracts the PCA Features of the Input Image with 'k' new dimensions % k=eigen values reduction parameter
function features=PCA_features(I, k)
if numel(size(I))==3 I = rgb2gray(I);
end Id = im2double(I);
meen = mean(Id); [a ~] = size(Id); I_Adjust = Id - repmat(meen,a,1); % mean subtraction
cov_data = cov(I_Adjust); % covariance matrix
[feature_vector,~] = eigs(cov_data,k); % eigen values of covariance matrix
features=feature_vector'*I_Adjust'; % Final Data
8/10/2019 GUI-FaceRecognition Using Matlab Report-JinJin
25/25
- Face_Match.fig (Our Face Recognition Proejcts GUI)