-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchlentemplatesbrute2.m
198 lines (156 loc) · 7.63 KB
/
matchlentemplatesbrute2.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
function [ matchvotes,startintervalscalematch, qtcomboinfo] = matchlentemplatesbrute2( querytemplate, fullrotationtemplates, rotation )
%MATCHLENTEMPLATES Summary of this function goes here
% Detailed explanation goes here
% x = find(any(querytemplate,2),1,'first'):find(any(querytemplate,2),1,'last');
% y = find(any(querytemplate),1,'first'):find(any(querytemplate),1,'last');
% querytemplate = querytemplate(x, y);
smoothfactor = zeros(1,5);
smoothfactor(1) = parsing.gauss(2,0,1);
smoothfactor(11) = smoothfactor(1);
smoothfactor(2) = parsing.gauss(1.6,0,1);
smoothfactor(10) = smoothfactor(2);
smoothfactor(3) = parsing.gauss(1.2,0,1);
smoothfactor(9) = smoothfactor(3);
smoothfactor(4) = parsing.gauss(.8,0,1);
smoothfactor(8) = smoothfactor(4);
smoothfactor(5) = parsing.gauss(.4,0,1);
smoothfactor(6) = smoothfactor(5);
smoothfactor(7) = parsing.gauss(0,0,1);
minscale = log(.001);
maxscale = log(100);
scaleresolution = 250;
stepsize = (maxscale-minscale)/(scaleresolution-1);
[fullrowdim,fullcoldim, numrotations] = size(fullrotationtemplates);
matchvotes = zeros(1,numrotations);
startintervalscalematch = zeros(4,numrotations);
[numslices,querycoldim] = size(querytemplate);
nonzeroquery=0;
if ~ isempty(querytemplate)
nonzeroquery = nnz(querytemplate(:,1));
end
qtcomboinfo = cell(numrotations, numslices, 6);
bestscalevoting=[];
rotation
mostvotes = 0;
template = fullrotationtemplates(:,:,rotation);
x = find(any(template,2),1,'first'):find(any(template,2),1,'last');
y = find(any(template),1,'first'):find(any(template),1,'last');
trimmedtemplate = template(x, y);
[rowdim,coldim] = size(trimmedtemplate);
fullinterval = floor(rowdim/numslices);
padrowdim=int32(rowdim*1.5);
templatestart = int32(rowdim*.25);
templateend = templatestart+rowdim-1;
paddedtemplate = zeros(padrowdim, coldim);
paddedtemplate(templatestart:templateend,:) = trimmedtemplate;
trimmedtemplate=paddedtemplate;
[rowdim,coldim] = size(trimmedtemplate);
maxinterval = floor(rowdim/numslices);
newquerytemplate = zeros(numslices, coldim);
newquerytemplate(:,1:querycoldim) = querytemplate;
savedscalevoting = cell(numslices, rowdim);
maxcontributor = zeros(rowdim, numslices, scaleresolution+10);
comboinfo = cell(scaleresolution+10, rowdim, numslices, 6);
tic
for theslice=1:numslices
for therow=1:rowdim
tempscalevoting = zeros(1,scaleresolution+10);
if(therow>=templatestart && therow <=templateend && any(newquerytemplate(theslice,:)) && any(trimmedtemplate(therow,:))) %FIXED
errmeasures=errmeasure3(newquerytemplate(theslice,:), trimmedtemplate(therow,:));
cols = size(errmeasures,2);
%{
fprintf('=============COLS=============')
fprintf('newquerytemplate - the slice')
newquerytemplate(theslice,:)
newquerytemplate
fprintf('trimmedtemplate - therow')
trimmedtemplate(therow,:)
trimmedtemplate
errmeasure3(newquerytemplate(theslice,:), trimmedtemplate(therow,:))
%}
for col=1:cols
scaleresult = errmeasures{1,col};
querymod = errmeasures{2,col};
templatemod = errmeasures{3,col};
querycombo = errmeasures{4,col};
templatecombo = errmeasures{5,col};
qtprune = errmeasures{6,col};
totalpieces = length(querycombo) + length(templatecombo);
percentunmodified = (totalpieces-sum(querycombo)-sum(templatecombo));
%cols 2 and 3 have 1 piece of the template removed
percentunmodified = percentunmodified - (length(querycombo)-length(querymod)) - (length(templatecombo)-length(templatemod));
percentunmodified = percentunmodified / totalpieces;
votes = smoothfactor*parsing.gauss(scaleresult(2),0,.5)*parsing.gauss(1,percentunmodified,3/5);
index = round((log(scaleresult(1))-minscale)/stepsize)+6;
if(index >5 && index < scaleresolution+6)
%tempscalevoting(index-5:index+5)= max(tempscalevoting(index-5:index+5),votes);
for subindex=index-5:index+5
if votes(subindex -(index-5) + 1) > maxcontributor(therow, theslice, subindex)
tempscalevoting(subindex) = votes(subindex -(index-5) + 1);
maxcontributor(therow, theslice, subindex)=tempscalevoting(subindex);
comboinfo{subindex,therow, theslice, 1} = querymod;
comboinfo{subindex,therow, theslice, 2} = templatemod;
comboinfo{subindex,therow, theslice, 3} = querycombo;
comboinfo{subindex,therow, theslice, 4} = templatecombo;
comboinfo{subindex,therow, theslice, 5} = qtprune;
comboinfo{subindex,therow, theslice, 6} = trimmedtemplate(therow,:);
end
end
end
end
end
savedscalevoting{theslice,therow}= tempscalevoting;
end
end
% scalevalues(scalevalues==0)=[];
toc
for interval = 1:maxinterval
for startpoint = 1:rowdim-interval*numslices+1
%rowdim-interval*numslices+1
scalevoting = zeros(1,scaleresolution+10)+.000000001;
unmatchedcount = 0;
for theslice=1:numslices
scalevoting = scalevoting + savedscalevoting{theslice, startpoint+(theslice-1)*interval};
if(trimmedtemplate(startpoint+(theslice-1)*interval) == 0)
unmatchedcount = unmatchedcount + 1;
end
end
[topvote,index] = max(scalevoting);
topvote = topvote * parsing.gauss(interval/fullinterval,1,3/5) * parsing.gauss(unmatchedcount/numslices, 0, 3/5);
if(topvote>mostvotes)
bestscalevoting = scalevoting;
mostvotes = topvote;
matchvotes(1,rotation) = mostvotes;
startintervalscalematch(1,rotation)=startpoint;
startintervalscalematch(2,rotation)=interval;
startintervalscalematch(3,rotation)=exp((index-6)*stepsize + minscale);
startintervalscalematch(4,rotation) = nonzeroquery/numslices;
for theslice=1:numslices
therow = startpoint+(theslice-1)*interval;
cbi = comboinfo(index, startpoint+(theslice-1)*interval, theslice, :);
offset = 1;
if ~any(newquerytemplate(theslice,:))
comboinfo{index, startpoint+(theslice-1)*interval, theslice, 6}=trimmedtemplate(therow,:);
cbi = comboinfo(index, startpoint+(theslice-1)*interval, theslice, :);
elseif(therow>=templatestart && therow <=templateend)
while(isempty(cbi{1,1}))
%cbi
if(index+offset <=length(scalevoting))
cbi = comboinfo(index+offset, startpoint+(theslice-1)*interval, theslice, :);
end
if(isempty(cbi{1,1}) && index-offset > 0)
cbi = comboinfo(index-offset, startpoint+(theslice-1)*interval, theslice, :);
end
offset=offset+1;
%FORCE QUIT out of loop
if(offset > 10000)
break;
end
end
qtcomboinfo(rotation, theslice,:) = cbi;
end
end
end
end
toc
end