-
Notifications
You must be signed in to change notification settings - Fork 102
/
distance_transform.h
348 lines (318 loc) · 9.35 KB
/
distance_transform.h
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#ifndef CVD_INC_DISTANCE_TRANSFORM_HPP
#define CVD_INC_DISTANCE_TRANSFORM_HPP
#include <cmath>
#include <cvd/vision_exceptions.h>
#include <algorithm>
#include <cmath>
#include <cvd/image.h>
#include <limits>
#include <vector>
namespace CVD
{
template <class Precision = double>
class DistanceTransformEuclidean
{
// Implements
// Distance Transforms of Sampled Functions
// Pedro F. Felzenszwalb Daniel P. Huttenlocher
public:
DistanceTransformEuclidean()
: sz(ImageRef(-1, -1))
, big_number(1'000'000'000) //Hmm, why doesn't HUGE_VAL work?
//Anyway, hilariously small number here so it works with int, too.
{
}
private:
inline void transform_row(const int n)
{
v[0] = 0; //std::numeric_limits<Precision>::infinity();
z[0] = -big_number; //std::numeric_limits<Precision>::infinity();
z[1] = +big_number; // std::numeric_limits<Precision>::infinity();
int k = 0;
for(int q = 1; q < n; q++)
{
Precision s = ((f[q] + (q * q)) - (f[v[k]] + (v[k] * v[k]))) / (2 * q - 2 * v[k]);
while(s <= z[k])
{
k--;
s = ((f[q] + (q * q)) - (f[v[k]] + (v[k] * v[k]))) / (2 * q - 2 * v[k]);
}
k++;
v[k] = q;
z[k] = s;
z[k + 1] = +big_number; //std::numeric_limits<Precision>::infinity();
}
k = 0;
for(int q = 0; q < n; q++)
{
while(z[k + 1] < q)
{
k++;
}
d[q] = ((q - v[k]) * (q - v[k])) + f[v[k]];
pos[q] = q > v[k] ? (q - v[k]) : (v[k] - q);
//cout << "n=" << n << " q=" << q << " d[q]=" << d[q] << " v[k]=" << v[k] << " f[v[k]]=" << f[v[k]] << endl;
}
}
void transform_image(BasicImage<Precision>& DT)
{
const ImageRef img_sz(DT.size());
for(int x = 0; x < img_sz.x; x++)
{
for(int y = 0; y < img_sz.y; y++)
{
f[y] = DT[y][x];
}
transform_row(img_sz.y);
for(int y = 0; y < img_sz.y; y++)
{
DT[y][x] = d[y];
}
}
//#if 0
for(int y = 0; y < img_sz.y; y++)
{
for(int x = 0; x < img_sz.x; x++)
{
f[x] = DT[y][x];
}
transform_row(img_sz.x);
for(int x = 0; x < img_sz.x; x++)
{
DT[y][x] = d[x];
}
}
//#endif
}
template <class Functor>
/// Perform distance transform with reverse lookup.
/// @param ADT which edge pixel is closest to this one
void transform_image_with_ADT(BasicImage<Precision>& DT, BasicImage<ImageRef>& ADT, const Functor& func)
{
const ImageRef img_sz(DT.size());
const double maxdist = img_sz.x * img_sz.y;
for(int x = 0; x < img_sz.x; x++)
{
for(int y = 0; y < img_sz.y; y++)
{
f[y] = DT[y][x];
}
transform_row(img_sz.y);
for(int y = 0; y < img_sz.y; y++)
{
DT[y][x] = d[y];
}
}
for(int y = 0; y < img_sz.y; y++)
{
for(int x = 0; x < img_sz.x; x++)
{
f[x] = DT[y][x];
}
transform_row(img_sz.x);
for(int x = 0; x < img_sz.x; x++)
{
DT[y][x] = d[x];
const double hyp = d[x];
if(hyp >= maxdist)
{
ADT[y][x] = ImageRef(-1, -1);
continue;
}
const double dx = pos[x];
const double dy = sqrt(hyp - dx * dx);
const int ddy = static_cast<int>(dy);
const ImageRef candA(static_cast<int>(x - dx), static_cast<int>(y - ddy));
const ImageRef candB(static_cast<int>(x - dx), static_cast<int>(y + ddy));
const ImageRef candC(static_cast<int>(x + dx), static_cast<int>(y - ddy));
const ImageRef candD(static_cast<int>(x + dx), static_cast<int>(y + ddy));
/** cerr << "hyp=" << hyp << " dx="<< dx << " dy=" << dy << " ddy=" << ddy
<< " A=" << candA << " B=" << candB << " C=" << candC << " D=" << candD << endl;*/
if(DT.in_image(candA) && func(candA))
{
ADT[y][x] = candA;
}
else if(DT.in_image(candB) && func(candB))
{
ADT[y][x] = candB;
}
else if(DT.in_image(candC) && func(candC))
{
ADT[y][x] = candC;
}
else if(DT.in_image(candD) && func(candD))
{
ADT[y][x] = candD;
}
else
{
//ADT[y][x] = ImageRef(-1,-1);
/**cerr << hyp << " " << ImageRef(x, y);*/
throw Exceptions::Vision::BadInput("DistanceTransformEuclidean: no points set");
}
}
}
}
void resize(const ImageRef& new_size)
{
if(new_size != sz)
{
sz = new_size;
int m(std::max(new_size.x, new_size.y));
d.resize(m);
v.resize(m);
f.resize(m);
pos.resize(m);
z.resize(m + 1);
}
}
public:
template <class T>
void transform_ADT(const BasicImage<T>& feature, BasicImage<Precision>& DT, BasicImage<ImageRef>& ADT)
{
if(feature.size() != DT.size())
throw Exceptions::Vision::IncompatibleImageSizes(__FUNCTION__);
if(feature.size() != ADT.size())
throw Exceptions::Vision::IncompatibleImageSizes(__FUNCTION__);
resize(feature.size());
apply_functor(DT, NotZero<T>(feature));
transform_image_with_ADT(DT, ADT, NotZero<T>(feature));
}
void transform(BasicImage<Precision>& out)
{
resize(out.size());
transform_image(out);
}
template <class C>
struct NotZero
{
NotZero(const BasicImage<C>& im_)
: im(im_)
{
}
const BasicImage<C>& im;
bool operator()(const ImageRef& i) const
{
return im[i] != 0;
}
};
template <class Out, class Functor>
void apply_functor(BasicImage<Out>& out, const Functor& f)
{
for(int y = 0; y < out.size().y; y++)
for(int x = 0; x < out.size().x; x++)
if(f(ImageRef(x, y)))
out[y][x] = 0;
else
out[y][x] = big_number;
}
private:
ImageRef sz;
int big_number;
std::vector<Precision> d;
std::vector<int> v;
std::vector<Precision> z;
std::vector<Precision> f;
std::vector<Precision> pos;
};
///Compute squared Euclidean distance transform using the Felzenszwalb & Huttenlocher algorithm.
///Example in examples/distance_transform.cc
///@ingroup gVision
///@param in input image: thresholded so anything > 0 is on the object
///@param out output image is euclidean distance of input image.
///@throws Exceptions::Vision::BadInput Throws if the input contains no points.
template <class T, class Q>
void euclidean_distance_transform_sq(const BasicImage<T>& in, BasicImage<Q>& out)
{
if(in.size() != out.size())
throw Exceptions::Vision::IncompatibleImageSizes(__FUNCTION__);
DistanceTransformEuclidean<Q> dt;
dt.apply_functor(out, typename DistanceTransformEuclidean<Q>::template NotZero<T>(in));
dt.transform(out);
}
///Compute squared Euclidean distance transform using the Felzenszwalb & Huttenlocher algorithm.
///Example in examples/distance_transform.cc
///@ingroup gVision
///@param in input image: thresholded so anything > 0 is on the object
///@param out output image is euclidean distance of input image.
///@param lookup_DT For each output pixel, this is the location of the closest input pixel.
///@throws Exceptions::Vision::BadInput Throws if the input contains no points.
template <class T, class Q>
void euclidean_distance_transform_sq(const BasicImage<T>& in, BasicImage<Q>& out, BasicImage<ImageRef>& lookup_DT)
{
if(in.size() != out.size())
throw Exceptions::Vision::IncompatibleImageSizes(__FUNCTION__);
DistanceTransformEuclidean<Q> dt;
dt.transform_ADT(in, out, lookup_DT);
}
///Compute Euclidean distance transform using the Felzenszwalb & Huttenlocher algorithm.
///Example in examples/distance_transform.cc
///@ingroup gVision
///@param in input image: thresholded so anything > 0 is on the object
///@param out output image is euclidean distance of input image.
///@throws Exceptions::Vision::BadInput Throws if the input contains no points.
template <class T, class Q>
void euclidean_distance_transform(const BasicImage<T>& in, BasicImage<Q>& out)
{
euclidean_distance_transform_sq(in, out);
for(int y = 0; y < out.size().y; y++)
for(int x = 0; x < out.size().x; x++)
out[y][x] = sqrt(out[y][x]);
}
///Compute Euclidean distance transform using the Felzenszwalb & Huttenlocher algorithm.
///Example in examples/distance_transform.cc
///@ingroup gVision
///@param in input image: thresholded so anything > 0 is on the object
///@param out output image is euclidean distance of input image.
///@param lookup_DT For each output pixel, this is the location of the closest input pixel.
///@throws Exceptions::Vision::BadInput Throws if the input contains no points.
template <class T, class Q>
void euclidean_distance_transform(const BasicImage<T>& in, BasicImage<Q>& out, BasicImage<ImageRef>& lookup_DT)
{
euclidean_distance_transform_sq(in, out, lookup_DT);
for(int y = 0; y < out.size().y; y++)
for(int x = 0; x < out.size().x; x++)
out[y][x] = sqrt(out[y][x]);
}
#ifndef DOXYGEN_IGNORE_INTERNAL
namespace Internal
{
template <class C>
class DoDistanceTransform
{
};
template <class T>
struct ImagePromise<DoDistanceTransform<T>>
{
ImagePromise(const BasicImage<T>& in_)
: in(in_)
{
}
const BasicImage<T>& in;
template <class C>
void execute(Image<C>& im)
{
im.resize(in.size());
euclidean_distance_transform(in, im);
}
};
};
template <class T>
Internal::ImagePromise<Internal::DoDistanceTransform<T>> euclidean_distance_transform(const BasicImage<T>& in)
{
using namespace Internal;
return ImagePromise<DoDistanceTransform<T>>(in);
}
#else
///Compute Euclidean distance transform using the Felzenszwalb & Huttenlocher algorithm.
///@ingroup gVision
///@param in input image: thresholded so anything > 0 is on the object
///@returns output image is euclidean distance of input image.
///@throws Exceptions::Vision::BadInput Throws if the input contains no points.
template <class T>
Image euclidean_distance_transform(const BasicImage<T>& in);
#endif
///@example distance_transform.cc
///Example of how to use CVD::euclidean_distance_transform_sq
};
#endif