-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfast_tree_to_cxx_score_iterate
executable file
·235 lines (197 loc) · 6.04 KB
/
fast_tree_to_cxx_score_iterate
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
#!/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>
#include <climits>
using namespace CVD;
using namespace std;
static inline bool test_gt_set(int a, int b, int& min_diff)
{
if(a > b)
{
if(a-b < min_diff)
min_diff = a-b;
return 1;
}
return 0;
}
static inline int fast_corner_score(const byte* p, const int pixel[], int b)
{
b++;
//This function computes the score for a pixel which is known to be
//a corner at barrier b. So we start looking at b+1 and above to
//establish where it stops becoming a corner.
for(;;)
{
int cb = *p + b;
int c_b= *p - b;
int min_diff = INT_MAX;
$(
awk '
{ ind = " "substr($0, 1, match($0, /[^ ]/)-1)}
/if_brighter/{print ind "if(test_gt_set(p[pixel["$2"]], cb, min_diff))"}
/elsf_darker/ {print ind "else if(test_gt_set(c_b, p[pixel["$2"]], min_diff))"}
/if_darker/ {print ind "if(test_gt_set(c_b, p[pixel["$2"]], min_diff))"}
/if_either/{print ind "if(test_gt_set(p[pixel["$2"]], cb, min_diff) || test_gt_set(c_b, p[pixel["$2"]], min_diff))"}
/else/{print ind "else"}
/corner/{print ind "b += min_diff;"}
/background/{print ind "break;"}
' $file
)
}
return b-1;
}
static inline int corner_score(const byte* p, const int pixel[], int b)
{
b++;
//This function computes the score for a pixel which is known to be
//a corner at barrier b. So we start looking at b+1 and above to
//establish where it stops becoming a corner.
for(;;)
{
int cb = *p + b;
int c_b= *p - b;
int min_diff = INT_MAX;
$(
awk '
{ ind = " "substr($0, 1, match($0, /[^ ]/)-1)}
/if_brighter/{print ind "if(test_gt_set(p[pixel["$2"]], cb, min_diff))"}
/elsf_darker/ {print ind "else if(test_gt_set(c_b, p[pixel["$2"]], min_diff))"}
/if_darker/ {print ind "if(test_gt_set(c_b, p[pixel["$2"]], min_diff))"}
/if_either/{print ind "if(test_gt_set(p[pixel["$2"]], cb, min_diff) || test_gt_set(c_b, p[pixel["$2"]], min_diff))"}
/else/{print ind "else"}
/corner/{print ind "b += min_diff;"}
/background/{print ind "break;"}
' $file
)
}
return b-1;
}
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