-
Notifications
You must be signed in to change notification settings - Fork 5
/
PCADictionary.m
executable file
·48 lines (39 loc) · 1.01 KB
/
PCADictionary.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
function [D] = PCADictionary(stack,energy_thresh,show,discard,do_LOOC)
%PCADICTIONARY Compute the PCA of the images and return the major principal
%vectors as column vectors of 'D'. By default, the first column of D is the mean signal
[Nr,Nc,N] = size(stack);
% Stack all signals in rows
Data = zeros(N,Nr*Nc);
for ii = 1 : N
dummy = stack(:,:,ii);
Data(ii,:) = dummy(:)';
end
if do_LOOC
Data(discard,:) = [];
end
data_mean = mean(Data);
[full_principal_components,~,pc_var] = pca(Data);
pc_var = pc_var/(sum(pc_var(:)));
t = 0;
flag = 0;
for ii = 1 : length(pc_var)
t = t+pc_var(ii);
if t <= energy_thresh
flag = flag+1;
else
break;
end
end
% to_add = repmat(data_mean',1,flag);
D = [data_mean' full_principal_components(:,1:flag)];
if show
% flag = 50;
k = ceil(sqrt(flag));
figure(1);
for ii = 1 : flag
data = D(:,ii+1);
img = reshape(data,Nr,Nc);
subplot(k,k,ii);imshow(img,[]);
end
end
end