-
Notifications
You must be signed in to change notification settings - Fork 20
/
fast_tree_to_matlab_score_bsearch
executable file
·259 lines (199 loc) · 6.54 KB
/
fast_tree_to_matlab_score_bsearch
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env bash
# This file is part of the FAST-ER machine learning system.
# Copyright (C) 2008 Edward Rosten and Los Alamos National Laboratory
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
file="$2"
name="$1"
if [ "$#" != 2 ]
then
echo `basename $0`: error: incorrect arguments.
echo "Usage: `basename $0` <function_name> <file>"
exit 1
fi
debug_use_test_rig=0
num_offsets=$(head -1 $file)
border=$(
awk '
NR==1{features = $1}
NR==2{
gsub(/[\[\],]/, "")
if(features != NF/2)
{
print "Malformed offsets line"
exit(1);
}
o=0
for(x=1; x <= NF; x++)
o = ($x > o? $x : o)
print o
exit(0);
} ' $file
)
if [ "$debug_use_test_rig" == 1 ]
then
cat<<END
function test()
image = imread('frame_1.pgm');
corners = ${name}(image, 20, 'nonmax');
imshow(image);
hold on
plot(corners(:,1), corners(:,2), 'r.')
END
fi
uppername=$(echo $name | tr '[a-z]' '[A-Z]')
cat << END
%$uppername perform an FAST corner detection from your FAST-ER generated tree
% [corners, scores] = $uppername(image, threshold) performs the detection on the image
% and returns the X coordinates in corners(:,1), the Y coordinares in corners(:,2) and
% optionally, the scores in scores(:). The score is computed using binary search over
% all possible thresholds.
%
% [corners, scores] = $uppername(image, threshold, nonmax) performs the corner
% detection and nonmaximal suppression if nonmax is not zero.
%
% If you use this in published work, please cite:
% Faster and better: A machine learning approach to corner detection, E. Rosten, R. Porter and T. Drummond, PAMI (to appear) 2008
% Machine learning for high-speed corner detection, E. Rosten and T. Drummond, ECCV 2006
% The Bibtex entries are:
%
% @inproceedings{rosten_2006_machine,
% title = "Machine learning for high-speed corner detection",
% author = "Edward Rosten and Tom Drummond",
% year = "2006",
% month = "May",
% booktitle = "European Conference on Computer Vision (to appear)",
% notes = "Poster presentation",
% url = "http://mi.eng.cam.ac.uk/~er258/work/rosten_2006_machine.pdf"
% }
%
% @article{rosten_2008_faster,
% title = "Faster and better: A machine learning approach to corner detection",
% author = "Edward Rosten and Reid Porter and Tom Drummond",
% year = "2008",
% month = "October",
% journal = "IEEE Transactions on Pattern Analysis and Machine Intelligence (to appear)",
% eprint = "arXiv:0810.2434 [cs.CV]",
% url = "http://lanl.arXiv.org/pdf/0810.2434"
% }
function [ corners scores ] = ${name}(image, threshold, do_nonmax)
corners = zeros(1024, 2);
num_corners=0;
for y=$((border+1)):size(image,1)-$((border+1))
for x=$((border+1)):size(image,2)-$((border+1))
cb = image(y, x) + threshold;
c_b = image(y, x) - threshold;
$(
awk '
NR==1{features = $1}
NR==2{
gsub(/[\[\],]/, "")
for(x=1; x <= features; x++)
{
offx[x-1] = $(2*x-1)
offy[x-1] = $(2*x)
}
}
{ ind = " "substr($0, 1, match($0, /[^ ]/)-1)}
{ accessor = "image(y+"offy[$2]",x+"offx[$2]")" }
/if_brighter/{print ind "if " accessor " > cb "}
/elsf_darker/ {print ind "elseif " accessor " < c_b "}
/if_darker/{print ind "if " accessor " < c_b"}
/if_either/{print ind "if " accessor " > cb | " accessor " < c_b "}
/else/{print ind "else"}
/end/{print ind "end"}
/background/{print ind "continue"}
' $file
)
num_corners = num_corners + 1;
corners(num_corners, 1) = x;
corners(num_corners, 2) = y;
if num_corners == length(corners)
corners(end*2,1)=0;
end
end
end
corners = corners(1:num_corners, :);
if ~exist('do_nonmax')
do_nonmax=0;
end
if nargout == 2 | do_nonmax
scores = zeros(num_corners, 1);
for i=1:num_corners
scores(i) = corner_score(image, corners(i, 1), corners(i, 2));
end
end
if do_nonmax
rows = size(image, 1);
offsets = [ rows+1 rows rows-1 1 -1 -rows+1 -rows -rows-1];
rcorners=zeros(size(corners));
rscores=zeros(size(scores));
num_nonmax=0;
score_image = -ones(size(image));
score_image(sub2ind(size(image), corners(:,2), corners(:,1))) = scores;
for i=1:num_corners
pos = sub2ind(size(image), corners(i,2), corners(i, 1));
if all(score_image(pos) > score_image(pos + offsets))
num_nonmax = num_nonmax+1;
rcorners(num_nonmax,:) = corners(i,:);
rscores(num_nonmax) = scores(i);
end
end
corners = rcorners(1:num_nonmax, :);
scores = rscores(1:num_nonmax);
end
function c = is_a_corner(i, posx, posy, b)
cb = i(posy, posx) + b;
c_b = i(posy, posx) - b;
$(
awk '
NR==1{features = $1}
NR==2{
gsub(/[\[\],]/, "")
for(x=1; x <= features; x++)
{
offx[x-1] = $(2*x-1)
offy[x-1] = $(2*x)
}
}
{ ind = " "substr($0, 1, match($0, /[^ ]/)-1)}
{ accessor = "i(posy+"offy[$2]",posx+"offx[$2]")" }
/if_brighter/{print ind "if " accessor " > cb "}
/elsf_darker/ {print ind "elseif " accessor " < c_b "}
/if_darker/{print ind "if " accessor " < c_b"}
/if_either/{print ind "if " accessor " > cb | " accessor " < c_b "}
/else/{print ind "else"}
/end/{print ind "end"}
/corner/{print ind "c=1; return"}
/background/{print ind "c=0; return"}
' $file
)
function bmin = corner_score(i, posx, posy)
bmin = 0;
bmax = 255;
b = floor((bmax + bmin)/2);
%Compute the score using binary search
while 1
if(is_a_corner(i, posx, posy, b))
bmin = b;
else
bmax = b;
end
if bmin == bmax - 1 | bmin == bmax
return
end
b = floor(bmin + bmax) / 2;
end
END