-
Notifications
You must be signed in to change notification settings - Fork 20
/
fast_tree_to_cxx_score_bsearch
executable file
·207 lines (168 loc) · 4.93 KB
/
fast_tree_to_cxx_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
#!/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
)
cat << END
#include <cvd/image.h>
#include <cvd/byte.h>
#include <vector>
using namespace CVD;
using namespace std;
static inline bool is_a_corner(const byte* p, const int pixel[], int b)
{
int cb = *p + b;
int c_b= *p - b;
$(
awk '
{ ind = " "substr($0, 1, match($0, /[^ ]/)-1)}
/if_brighter/{print ind "if( p[pixel["$2"]] > cb)"}
/elsf_darker/ {print ind "else if( p[pixel["$2"]] < c_b)"}
/if_darker/{print ind "if( p[pixel["$2"]] < c_b)"}
/if_either/{print ind "if( p[pixel["$2"]] > cb || p[pixel["$2"]] < c_b )"}
/else/{print ind "else"}
/corner/{print ind "return true;"}
/background/{print ind "return false;"}
' $file
)
}
static inline int corner_score(const byte* p, const int pixel[], int bstart)
{
int bmin = bstart;
int bmax = 255;
int b = (bmax + bmin)/2;
//Compute the score using binary search
for(;;)
{
if(is_a_corner(p, pixel, b))
bmin = b;
else
bmax = b;
if(bmin == bmax - 1 || bmin == bmax)
return bmin;
b = (bmin + bmax) / 2;
}
}
static void make_offsets(int pixel[], int row_stride)
{
$(
awk '
NR==1{features = $1}
NR==2{
gsub(/[\[\],]/, "")
for(x=1; x <= features; x++)
print " pixel[" (x-1) "] = "$(2*x-1) " + row_stride * " $(2*x)";"
exit(0);
} ' $file
)
}
void ${name}_score(const SubImage<byte>& i, const vector<ImageRef>& corners, int b, vector<int>& scores)
{
scores.resize(corners.size());
int pixel[$num_offsets];
make_offsets(pixel, i.row_stride());
for(unsigned int n=0; n < corners.size(); n++)
scores[n] = corner_score(&i[corners[n]], pixel, b);
}
void ${name}_detect(const SubImage<byte>& i, vector<ImageRef>& corners, int b)
{
corners.clear();
int pixel[$num_offsets];
make_offsets(pixel, i.row_stride());
for(int y=$border; y < i.size().y - $border; y++)
for(int x=$border; x < i.size().x - $border; x++)
{
const byte* p = i[y] + x;
int cb = *p + b;
int c_b= *p - b;
$(
awk '
{ ind = " "substr($0, 1, match($0, /[^ ]/)-1)}
/if_brighter/{print ind "if(p[pixel["$2"]] > cb)"}
/elsf_darker/ {print ind "else if(p[pixel["$2"]] < c_b)"}
/if_darker/ {print ind "if(p[pixel["$2"]] < c_b)"}
/if_either/{print ind "if(p[pixel["$2"]] > cb || p[pixel["$2"]]<c_b)"}
/else/{print ind "else"}
/corner/{print ind "{}"}
/background/{print ind "continue;"}
' $file
)
corners.push_back(ImageRef(x, y));
}
}
END
if [ "$debug_use_test_rig" == 1 ]
then
cat<<END
#include <cvd/fast_corner.h>
#include <cvd/image_io.h>
#include <algorithm>
int main(int argc, char** argv)
{
for(int i=1; i < argc; i++)
{
cerr << argv[i]<< endl;
Image<byte> im = img_load(argv[i]);
vector<ImageRef> c1, c2;
vector<int> s1, s2;
for(int t=0; t < 256; t++)
{
c1.clear();
c2.clear();
s1.clear();
s2.clear();
${name}_detect(im, c1, t);
${name}_score(im, c1, t, s1);
fast_corner_detect_9(im, c2, t);
fast_corner_score_9(im, c2, t, s2);
cerr << t << endl;
if(c1.size() != c2.size())
cerr << "Size mismatch\n";
else if(!equal(c1.begin(), c1.end(), c2.begin()))
cerr << "Detection mismatch\n";
else if(!equal(s1.begin(), s1.end(), s2.begin()))
cerr << "Score mismatch\n";
}
}
}
END
fi