-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddparts.m
150 lines (128 loc) · 4.28 KB
/
addparts.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
function model = addparts(model, c, numparts)
% model = addparts(model, c, numparts)
% Initialize parts from root filter.
% interpolate root filter
ridx = model.components{c}.rootindex;
weights = imresize(model.rootfilters{ridx}.w, 2, 'bicubic');
% we select parts using energy of positive weights
energy = sum(max(weights, 0).^2, 3);
% force the model to be symmetric with respect to the y axis
energy = energy + energy(:,end:-1:1);
% pick part sizes so they can approximatelly cover the object
area = prod(model.rootfilters{ridx}.size) * 4 / numparts;
% possible part shapes
k = 0;
for h = 3:1:size(weights, 1)-2
for w = 3:1:size(weights, 2)-2
if (w*h <= area*1.2 && w*h >= area && ...
w >= h-4 && w <= h+4 && h >= w-4 && h <= w+4)
k = k+1;
template{k} = fspecial('average', [h w]);
end
end
end
% picking parts: at each iteration we pick a pair of symmetric parts
% or a part in the center of the template
numadded = 0;
while numadded < numparts
% stop if we covered 80% of the object
% covered = sum(sum(energy == 0));
% if covered > prod(size(energy))*0.80
% break;
% end
for i = 1:k
score = conv2(energy, template{i}, 'valid');
tmp = score;
% we don't want symmetric parts to overlap
% so we "zero" the score of certain placements
center = size(energy, 2)/2;
z = max(center-size(template{i}, 2)+2, 1);
score(:,z:end) = -inf;
% we don't want symmetric parts if we only have one left to pick
if numadded == numparts-1
score(:,:) = -inf;
end
% we always allow centered parts
m = size(template{i}, 2)/2;
if m == round(m)
score(:,center-m+1) = tmp(:,center-m+1);
end
% pick best part with this shape
[v, Y] = max(score);
[v, x] = max(v);
y = Y(x);
ys(i) = y;
xs(i) = x;
vs(i) = v;
end
% pick best part, over all shapes
[foo, i] = max(vs);
% determine if the part has a symmetric partner
xsp = size(weights, 2)-(xs(i)+size(template{i}, 2)-1)+1;
haspartner = (xsp ~= xs(i));
% add part
[model, energy, pidx] = add(model, energy, weights, template{i}, ...
xs(i), ys(i), c, haspartner, false);
numadded = numadded + 1;
% add symmetric part
if haspartner
[model, energy, partner] = add(model, energy, weights, template{i}, ...
xsp, ys(i), c, haspartner, true);
numadded = numadded + 1;
% indicate that these parts mirror eachother
model.partfilters{pidx}.partner = partner;
model.partfilters{partner}.partner = pidx;
end
end
function [model, energy, pidx] = add(model, energy, w, template, ...
x, y, c, haspartner, fake)
% add partfilter
pidx = length(model.partfilters) + 1;
model.partfilters{pidx}.w = ...
w(y:y+size(template,1)-1, x:x+size(template,2)-1, :);
model.partfilters{pidx}.fake = fake;
model.partfilters{pidx}.partner = 0;
height = size(template,1);
% parts without symmetric partners are defined using symmetric filters
if haspartner
width = ceil(size(template,2));
else
width = ceil(size(template,2)/2);
end
% add feature block
if ~fake
model.numblocks = model.numblocks + 1;
model.partfilters{pidx}.blocklabel = model.numblocks;
model.blocksizes(model.numblocks) = width * height * 31;
wsize = model.blocksizes(model.numblocks);
model.regmult(model.numblocks) = 1;
model.learnmult(model.numblocks) = 1;
model.lowerbounds{model.numblocks} = -100*ones(wsize,1);
end
% add deformation model
didx = length(model.defs) + 1;
model.defs{didx}.anchor = [x y];
model.defs{didx}.w = [0.1 0 0.1 0];
if ~fake
model.numblocks = model.numblocks + 1;
model.defs{didx}.blocklabel = model.numblocks;
model.blocksizes(model.numblocks) = 4;
model.regmult(model.numblocks) = 10;
model.learnmult(model.numblocks) = 0.1;
model.lowerbounds{model.numblocks} = [0.01 -100 0.01 -100];
end
% link part to component
j = length(model.components{c}.parts) + 1;
model.components{c}.parts{j}.partindex = pidx;
model.components{c}.parts{j}.defindex = didx;
if ~fake
model.components{c}.dim = ...
model.components{c}.dim + 6 + wsize;
model.components{c}.numblocks = model.components{c}.numblocks + 2;
end
% zero energy in root
for xp = x:x+size(template,2)-1
for yp = y:y+size(template,1)-1
energy(yp, xp) = 0;
end
end