-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphase2.m
More file actions
77 lines (59 loc) · 1.98 KB
/
phase2.m
File metadata and controls
77 lines (59 loc) · 1.98 KB
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
% handy tools
imageSize = @() 50*50;
% reading train image dataset
trainFile = fopen('faces/train.txt', 'r');
trainFileData = textscan(trainFile, '%s %d');
trainAddresses = trainFileData{1};
trainAddressesLength = length(trainAddresses);
X = zeros(trainAddressesLength, imageSize());
% generating the X matrix
for i = 1:trainAddressesLength
tempMatrix = imread(trainAddresses{i});
% convert the matrix into a single column vector
tempVector = tempMatrix(:);
X(i, :) = tempVector;
end
% reading test image dataset
testFile = fopen('faces/test.txt');
testFileData = textscan(testFile, '%s %d');
testAddresses = testFileData{1};
testAddressesLength = length(testAddresses);
X_test = zeros(testAddressesLength, imageSize());
% generating the X_test matrix
for i = 1:testAddressesLength
tempMatrix = imread(testAddresses{i});
% convert the matrix into a single column vector
tempVector = tempMatrix(:);
X_test(i, :) = tempVector;
end
% computing average face
[avgVector, avgFace] = averageFace(X);
grayAvgFace = mat2gray(avgFace);
subplot(3, 5, 1), imshow(grayAvgFace);
title('Average face');
% computing mean subtraction face
XMeanSubtraction = zeros(trainAddressesLength, imageSize());
for i = 1:trainAddressesLength
XMeanSubtraction(i, :) = X(i,:) - avgVector;
end
subplot(3, 5, 2), imshow(reshape(mat2gray(XMeanSubtraction(100, :)), [sqrt(imageSize()), sqrt(imageSize())]));
title('100th Mean Subtraction face');
% computing eigenface
[U, S, V] = svd(X);
VT = V';
for i = 1:10
subplot(3, 5, 5 + i), imshow(reshape(mat2gray(VT(i, :)), [sqrt(imageSize()), sqrt(imageSize())]));
title(strcat(int2str(i), ' th Eigenface'));
end
% computing low-rank approximation
% this process take a while to proceed, so I've commented this part of code
% uncomment to see the result
% XMin = zeros(1, 200)
% for i=1:200
% XMin(i) = lowrank(X, i);
% end
%
% figure, plot(XMin)
% computing eigenface features
F = eigenfacefeature(X, 10);
F_test = eigenfacefeature(X_test, 10);