-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpca.m
88 lines (57 loc) · 1.66 KB
/
cpca.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
function [ model ] = cpca(cfg)
currev = getCurRev ();
cfg = icaConfigLoad(cfg);
fprintf ('Starting simulation for %s [code: %s]\n', cfg.id, currev);
imageset = createImageSet (cfg.data);
tstart = tic;
fprintf('\nGenerating dataset...\n');
dataset = createDataSet (imageset, cfg);
fprintf(' done in %f\n', toc(tstart));
dim = dataset.dim;
cfgid = cfg.id;
model.ds = dataset.id;
model.cfg = cfgid;
model.creator = genCreatorId();
model.ctime = gen_ctime();
id = DataHash (model, struct ('Method', 'SHA-1'));
model.cfg = cfg;
model.ds = dataset;
model = setfield_idx(model, 'id', id, 1);
%patches = extractPatches(dataset, 1);
%patches = patches';
patches = genall(dataset);
fprintf('performing PCA\n')
C = cov(patches);
[U, D] = svd(C);
model.A = U;
end
function [patches] = genall(ds)
patchsize = double(ds.patchsize);
nimg = size(ds.imgdata, 4);
icapatch = cell(nimg, 1);
iidx = cell(nimg, 1);
imgdata_ds = ds.imgdata;
isize = zeros(2, nimg);
fprintf('Generating patchset\n');
for imgnr=1:nimg
%imgnr = 2;
fprintf(' %02d', imgnr);
imgdata = imgdata_ds(:,:,:,imgnr);
[~, m, n] = size(imgdata);
idx = imgallindicies(m, n, patchsize, 1);
icapatch{imgnr} = getpatches (imgdata, idx, patchsize)';
iidx{imgnr} = idx;
isize(:, imgnr) = [m; n];
fprintf('\b\b\b');
end
icaall = cell2mat(icapatch);
clear icapatch orgpatch iidx;
kall = size(icaall, 1);
icaall = icaall - repmat(mean(icaall), kall, 1);
patches = icaall / std(icaall(:));
end
function [patches] = getpatches(data, idx, patchsize)
patches = patchesFromImg(data, idx, patchsize);
patches = patches - mean(patches(:));
patches = patches / sqrt(var(patches(:)));
end