-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw7part2.m
118 lines (109 loc) · 3.38 KB
/
hw7part2.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
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
cd 'CelebrityDatabase';
directory = dir('*.jpg');
imcell = cell(1,numel(directory));
for i = 1:numel(directory)
imcell{i} = imread(directory(i).name);
end
cd '..'
load('mask.mat');
n = size(imcell,2);
[w, h, d] = size(imcell{1});
% Transform images to grayscale vectors and mask them to make the
% computation less heavy
mask = mask(:,:,1);
unmasked_pixels = find(mask);
images = zeros(n, size(unmasked_pixels,1));
for i = 1:n
image = rgb2gray(imcell{i});
im_vector = image(unmasked_pixels);
images(i,:) = im_vector;
end
% The result of eigenfaces isn't normalized to be a grayscale image, so
% mapminmax takes eigenfaces and rowwise normalizes between 0-255
[efs, S, mean_face] = eigenfaces(images);
disp_efs = mapminmax(efs, 0, 255);
% Plot and save the eigenfaces
full_im = zeros(size(mask));
figure;
for i = 1:10
full_im(unmasked_pixels) = disp_efs(i,:);
subplot(2, 5, i), imshow(full_im, []);
title(num2str(i));
end
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperPosition', [0, 0, 30, 20]);
saveas(gcf, '10celebeigenfaces', 'jpg');
% Pick 5 random faces to 'reconstruct', though more accurately we're
% going to project the faces onto the subspace defined by the eigenfaces
% and see what they look like
rand_indices = randperm(n, 5);
figure;
for i = 1:5
recon_face = reconstruct(images(rand_indices(i),:), mean_face, efs, 10);
full_im(unmasked_pixels) = recon_face;
subplot(2, 5, i), imshow(full_im, []);
title(num2str(rand_indices(i)));
end
for i = 1:5
orig_face = images(rand_indices(i),:);
full_im(unmasked_pixels) = orig_face;
subplot(2, 5, i+5), imshow(full_im, []);
title(num2str(rand_indices(i)));
end
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperPosition', [0, 0, 30, 20]);
saveas(gcf, '5celebrecon', 'jpg');
% Now lets compute the average L2 error over the 5 randomly chosen faces
% as a function of the number of eigenfaces used to reconstruct them
figure;
x_axis = zeros(1,n);
errors = zeros(1,n);
for p = 1:n
x_axis(p) = p;
total_error = 0.0;
for i = 1:5
orig_face = images(rand_indices(i),:);
recon_face = reconstruct(images(rand_indices(i),:), mean_face, efs, p);
total_error = total_error + norm(recon_face - orig_face);
end
errors(p) = total_error / 5.0;
end
semilogy(errors);
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperPosition', [0, 0, 40, 30]);
saveas(gcf, '5celeberrors', 'jpg');
% Section 3
% Reconstruct student faces from celebrity database
cd 'StudentDatabase';
directory = dir('*.jpg');
imcell = cell(1,numel(directory));
for i = 1:numel(directory)
imcell{i} = imread(directory(i).name);
end
cd '..'
n_students = size(imcell,2);
[w, h, d] = size(imcell{1});
images = zeros(n_students, size(unmasked_pixels,1));
for i = 1:n_students
image = rgb2gray(imcell{i});
im_vector = image(unmasked_pixels);
images(i,:) = im_vector;
end
figure;
x_axis = zeros(1,n);
errors = zeros(1,n);
rand_indices = randperm(n_students, 5);
for p = 1:n
x_axis(p) = p;
total_error = 0.0;
for i = 1:5
orig_face = images(rand_indices(i),:);
recon_face = reconstruct(images(rand_indices(i),:), mean_face, efs, p);
total_error = total_error + norm(recon_face - orig_face);
end
errors(p) = total_error / 5.0;
end
semilogy(errors);
set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperPosition', [0, 0, 40, 30]);
saveas(gcf, '5studenterrors', 'jpg');