-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheigenface.asv
145 lines (96 loc) · 3.78 KB
/
eigenface.asv
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
clear
clc
% PCA
a = dir('celeba_low/*.jpg')
for i = 1 : size(a,1)
% I = imread(['celeba_low/',a(i).name]);
I = rgb2gray(imread(['celeba_low/',a(i).name]));
featvecmat(i,:) = I(:); % Transform image into feature vector. Do this for each image
end
featvecmat = double(featvecmat); %Convert features to doubles so it can be used in MATLAB's pca function
[coeff, score, latent] = pca(featvecmat);
var_captured = 0; % Variance captured from features (between 0 and 1)
i = 1;
while (i<size(coeff,2) && var_captured<0.98) %Loop through until 98% of variance is captured by eigen vectors
var_captured = sum(latent(1:i))/sum(latent); % Proportion of variance up to the ith eigen vector of pca
i = i + 1;
end
num_components = i;
%% Show put all eigenfaces in vectors
% im = cell(1,size(coeff,2));
im = cell(1,100)
Ibasis = uint8(zeros(64,64,3));
coenew = zeros(1,12288);
for z = 1 : 100
m = 1;
% Unfold feature vector into image
coenew = coeff(:,z) - min((coeff(:,z)));
coenew = uint8(255*coenew/max(coenew));
% Different type of image normalization
% coenew(1:12288/3) = coeff(1:12288/3,z) - min(coeff(1:12288/3,z));
% coenew(1+12288/3:2*12288/3,z) = coeff(1+12288/3:2*12288/3,z) - min(coeff(1+12288/3:2*12288/3,z));
% coenew(1+2*12288/3:12288) = coeff(1+2*12288/3:12288,z) - min(coeff(1+2*12288/3:12288,z));
%
% coenew(1:12288/3) = 255*coenew(1:12288/3)/max(coenew(1:12288/3));
% coenew(1+12288/3:2*12288/3) = 255*coenew(1+12288/3:2*12288/3)/max(coenew(1+12288/3:2*12288/3));
% coenew(1+2*12288/3:12288) = 255*coenew(1+2*12288/3:12288)/max(coenew(1+2*12288/3:12288));
for i = 1:3
for j = 1:64
for k = 1:64
Ibasis(k,j,i) = uint8(coenew(m)); % Unfolded basis image
m = m + 1;
end
end
end
im{z} = Ibasis;
disp(z);
end
mean_sunset = uint8(mean(featvecmat))
m = 1;
for i = 1:3
for j = 1:64
for k = 1:64
im_mean_sunset(k,j,i) = uint8(mean_sunset(m)); % Unfolded basis image
m = m + 1;
end
end
end
% Show faces
imagearr = cat(4,im_mean_sunset,im{1:99});
montage(imagearr, 'DisplayRange',[0,255]);
%% Determining the maximum tolerable reconstruction error with a finite number of pca components
for O = 1:size(featvecmat,1)
% For the Oth image
mean_sunset = double(mean(featvecmat));
imvecO = featvecmat(O,:)'- double(mean_sunset');
proj_vecO = coeff(:,1:num_components)'*imvecO; % Project the i first principle components
newfeatvec(i,:) = proj_vecO; % Project features into new feature space
imavec_reconstructed = double(mean_sunset');
for mmm = 1:num_components
imavec_reconstructed = imavec_reconstructed + proj_vecO(mmm)*coeff(:,mmm);
end
error_reconstruct(O) = sum((featvecmat(O,:)'-imavec_reconstructed).^2);
if(mod(O,50)==0)
disp(O)
end
end
errmax = max(error_reconstruct)
%%
O = 1;
% For the Oth image
mean_sunset = uint8(mean(featvecmat))
imvecO = featvecmat(O,:)'- double(mean_sunset');
proj_vecO = coeff(:,num_components)'*imvecO; % Project the i first principle components
imavec_reconstructed = double(mean_sunset');
for mmm = 1:num_components
imavec_reconstructed = imavec_reconstructed + proj_vecO(mmm)*coeff(:,mmm);
end
error_reconstruct = sum((featvecmat(O,:)'-imavec_reconstructed).^2)
if error_reconstruct< error_threshold
face = 1; % Image is face if it was reconstructed with low error from the n principle components
else
face = 0;
end
% LDA classifier
% KNN Classifier
% SVM