-
Notifications
You must be signed in to change notification settings - Fork 1
/
getFeatures.m
65 lines (57 loc) · 2.13 KB
/
getFeatures.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
% Load database.mat if database does not exist
if ~exist('database','var')
% get 2-D array database
load('database.mat');
end
% Get databaseOpen and databaseClosed
databaseOpen = database{1};
databaseOpen = double(databaseOpen);
databaseClosed = database{2};
databaseClosed = double(databaseClosed);
% Get the number of columns in both database
colDatabaseOpen = size(databaseOpen,2);
dboMean = mean(databaseOpen,1);
dboMA = bsxfun(@minus, databaseOpen, dboMean); %databaseOpen Mean Adjusted
colDatabaseClosed = size(databaseClosed,2);
dbcMean = mean(databaseClosed,1);
dbcMA = bsxfun(@minus, databaseClosed, dbcMean); %databaseClosed Mean Adjusted
% --- Find the principal components aka eigenhands.
% C = cov(A) returns the covariance.
% If A is a matrix whose columns represent random variables and whose rows
% represent observations, C is the covariance matrix with the corresponding
% column variances along the diagonal.
covarianceO=cov(dboMA');
[coeff1, latent1] = eig(covarianceO);
[latent1,idx] = sort(diag(latent1), 'descend'); %Sort eigenvalues
coeff1 = coeff1(:,idx); %Sort eigenvectors based on the eigenvalues
cumLatent1 = cumsum(latent1); % cumulative sum of eigenvalues
varianceO = cumLatent1/sum(latent1); % variance percentage for open
%Calculate the total number of eigenvectors required to represent 95% of
%the total variance of all hand images.
maxCoeff1 = 0;
for i = 1:length(varianceO)
if varianceO(i) < 0.95
maxCoeff1 = maxCoeff1 + 1;
else
break
end
end
covarianceC=cov(dbcMA');
[coeff2, latent2] = eig(covarianceC);
[latent2,idx] = sort(diag(latent2), 'descend'); %Sort eigenvalues
coeff2 = coeff2(:,idx); %Sort eigenvectors based on the eigenvalues
cumLatent2 = cumsum(latent2); % cumulative sum of eigenvalues
varianceC = cumLatent2/sum(latent2); % variance percentage for closed
maxCoeff2 = 0;
for i = 1:length(varianceC)
if varianceC(i) < 0.95
maxCoeff2 = maxCoeff2 + 1;
else
break
end
end
coeff1 = coeff1(:,1:maxCoeff1);
featuresOpen = coeff1'*dboMA;
coeff2 = coeff2(:,1:maxCoeff2);
featuresClosed = coeff2'*dbcMA;
save('features','coeff1','coeff2','featuresOpen','featuresClosed');