-
Notifications
You must be signed in to change notification settings - Fork 0
/
eigenface_recognition.m
83 lines (69 loc) · 2.45 KB
/
eigenface_recognition.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
sunglass = imread(fullfile(pwd,'Sunglasses.png'));
celeb = imread(fullfile(pwd,'Celeb.png'));
%%
recognised =facial_recognition(sunglass);
%%
recognised1 = facial_recognition(celeb);
%%
function [recognised] = facial_recognition(inputIm)
%load data
data = load(fullfile(pwd,'faces.mat'));
faces = data.raw_images;
%vectorise images
[numRows,numCols] = size( cell2mat(faces(1)) );
[dataRows,dataCols] = size(faces);
images = zeros(numRows*numCols,dataCols);
for i = 1:dataCols
dat = cell2mat(faces(i));
dat = reshape(dat,[],1);
images(:,i) = dat;
end
% get eigenvectors,W and egien values D of the data set
[W,D] = pca_dimred(images);
% evaluate the number of principal components needed to represent 95% Total variance.
total_var = sum(D);
csum = 0 ;
for i = 1:(numRows*numCols)
csum = csum + D(i);
tv = csum/ total_var;
if tv > 0.95
min95= i;
break
end
end
W = W(:,1:min95);
meanFace = mean(images);
%calculate the weights of the principal components of 95% variance
weights = W'* (images - meanFace);
%get input image and process/vectorise it
inputIm = im2gray(inputIm);
[row, col] = size(inputIm);
inputIm= imresize(inputIm,'scale',[(numRows/row) (numCols/col)]);
inputIm = double(inputIm);
inputIm = reshape(inputIm,[],1);
%calculate weights for the input image
testWeights = W'* (inputIm - meanFace) ;
% Calculate the euclidean distances for input weights against the weights of the
% original image set
for b=1:dataCols
d(b) = sqrt(sum((testWeights(:,b)-weights(:,b)).^2));
end
%Get closest eigenface index
[euclide_dist_min recognized_index] = min(d);
%recover recognised image
inputIm = reshape(inputIm, numRows, numCols);
detectedIm = reshape(images(:,recognized_index(1)), numRows, numCols);
%calculate similiarity
ssimval = ssim(inputIm,detectedIm);
if ssimval> 0.6
recognised = 'ACCESS GRANTED';
else
recognised = 'ACCESS DENIED';
end
figure('NumberTitle', 'off', 'Name', 'Q2.3');
colormap gray
subplot(1,2,1) , imagesc(inputIm), title('Input Image'), axis off
subplot(1,2,2), imagesc(detectedIm), title(sprintf('Recognised Face: %s' , recognised))
axis off
return;
end