-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerateclassifier.m
More file actions
72 lines (61 loc) · 2.89 KB
/
Copy pathgenerateclassifier.m
File metadata and controls
72 lines (61 loc) · 2.89 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
% generateclassifier function
% Read new image, find patches, collect features from patches
% Compute same and different dist, compute mutual info, pick top pn patches
function [classifier] = generateclassifier(sys, image)
global params
% Find all patches of a given size
patches = repmat(struct('x', 0, 'y', 0, 'sx', 0, 'sy', 0, 'data', []), 1, 1);
dist = repmat(struct('same', [], 'diff', []), 1, 1);
information = zeros(1, 1);
features = zeros(1, params.featcount);
disp('Collecting features ...');
imsy = size(image.data, 1);
imsx = size(image.data, 2);
pid = 1;
for p = 1:length(params.patchsize)
sy = params.patchsize(p, 1);
sx = params.patchsize(p, 2);
for y = 1:params.patchspacing(p, 2):imsy
for x = 1:params.patchspacing(p, 1):imsx
xf = x + sx - 1;
yf = y + sy - 1;
if yf <= imsy && xf <= imsx
% Compute normalized features for each patch
[patch, feat] = computefeatures(image, x, y, sx, sy);
features(pid, :) = normalizefeatures(feat, sys.normalization.mean, sys.normalization.std);
% Calculate mutual information for patch
patches(pid) = patch;
dist(pid).same = 1 ./ max(1e-4, ([1 features(pid, sys.samefeatfilter)] * sys.sameb));
dist(pid).diff = 1 ./ max(1e-4, ([1 features(pid, sys.difffeatfilter)] * sys.diffb));
ds = 0.01:0.001:2;
samep = gampdf(ds, dist(pid).same(2), dist(pid).same(1) / dist(pid).same(2));
diffp = gampdf(ds, dist(pid).diff(2), dist(pid).diff(1) / dist(pid).diff(2));
% Normalize for the entropy calculation
samep = samep / max(samep);
diffp = diffp / max(diffp);
information(pid) = entropy(samep) + entropy(diffp) - entropy(samep .* diffp);
pid = pid + 1;
end
end
end
end
% Pick top patches to generate the classifier
toppatches = repmat(struct('x', 0, 'y', 0, 'sx', 0, 'sy', 0, 'data', []), 1, 1);
topdist = repmat(struct('same', [], 'diff', []), 1, 1);
disp('Generating classifier from most informative patches ...');
for i = 1:params.patchcount
[info, index] = max(information);
toppatches(i) = patches(index);
topdist(i) = dist(index);
% Decrease the information for dependent patches
%for j = 1:length(information)
%mi = 1 / exp(sqrt(sum((features(index, :) - features(j, :)).^2)) / 8);
%mi = 1 / exp(sqrt((patches(index).x - patches(j).x).^2 + (patches(index).y - patches(j).y).^2) / 50);
%information(j) = information(j) - mi;
%end
information(index) = -1e8;
end
disp('Classifier generated!');
classifier.image = image.original;
classifier.patches = toppatches;
classifier.dist = topdist;