-
Notifications
You must be signed in to change notification settings - Fork 1
/
distance.h
220 lines (180 loc) · 6.58 KB
/
distance.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
/*
Distance_transform computation using the fast sweeping method
=========================================================================
v0.1.0
Licensed under the MIT License
(c) 2020-2023 Vincent Cruz
This header file provides a function to compute the distance transform of an image.
The implementation is based upon the following papers:
"A fast sweeping method for Eikonal equations"
by H. Zhao, Mathematics of computation, 74 (2005),pp. 603–627
https://www.math.uci.edu/~zhao/homepage/research_files/FSM.pdf
"Finding the Skeleton of 2D Shape and Contours: Implementation of Hamilton-Jacobi Skeleton"
by Yuchen He, Sung Ha Kang, Luis Álvarez (2020)
http://www.ipol.im/pub/art/2021/296/
* Building:
Before including this file, add the following line in the file where you want to have the
implementation.
#define DISTANCE_TRANSFORM_IMPLEMENTATION
* Usage:
void distance_transform(const uint8_t *in, float *out, int width, int height);
"in" is the pointer an 8bpp greyscale image.
"output" is the pointer to the a floating point array of at least width*height elements.
It will contain the distance map.
"width" and "height" are its dimension.
All pixels with a value of 0 are considered to be "inside", whereas any non-zero pixel is
considered outside. As a consequence, a pixel is considered to be on the boundary if its
value is 0 and at least one of its 8-neighbour is not zero.
* Note:
This piece of code is not meant to be "production ready".
*/
#ifndef DISTANCE_TRANSFORM_INCLUDE_H
#define DISTANCE_TRANSFORM_INCLUDE_H
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#ifdef __cplusplus
extern "C" {
#endif
void distance_transform(const uint8_t *in, float *output, int width, int height);
#ifdef __cplusplus
}
#endif
#endif /* DISTANCE_TRANSFORM_INCLUDE_H */
#ifdef DISTANCE_TRANSFORM_IMPLEMENTATION
void distance_transform(const uint8_t *in, float *out, int width, int height) {
int i, j;
uint8_t c;
const float max_dist = (float)(width*width + height*height);
float *ptr = out;
float *line;
// Initialize distance map by setting distance at boundary point to 0 and any other points to the maximal possible distance (width^2 + height^2).
// A boundary point is a pixel with a value of 0 and with a least one of its 8-neightbours different from 0.
c = ~in[0] & (in[1] | in[width] | in[width+1]);
++in;
*ptr++ = c ? 0.f : max_dist;
for(i=1; i<(width-1); i++) {
c = ~in[0] & (in[-1] | in[1] | in[width-1] | in[width] | in[width+1]);
++in;
*ptr++ = c ? 0.f : max_dist;
}
c = ~in[0] & (in[-1] | in[width-1] | in[width]);
++in;
*ptr++ = c ? 0.f : max_dist;
for(j=1; j<(height-1); j++) {
c = ~in[0] & (in[-width] | in[-width+1] | in[1] | in[width] | in[width+1]);
++in;
*ptr++ = c ? 0.f : max_dist;
for(i=1; i<(width-1); i++) {
c = ~in[0] & (in[-1-width] | in[-width] | in[-width+1] | in[-1] | in[1] | in[width-1] | in[width] | in[width+1]);
++in;
*ptr++ = c ? 0.f : max_dist;
}
c = ~in[0] & (in[-1-width] | in[-width] | in[-1] | in[width-1] | in[width]);
++in;
*ptr++ = c ? 0.f : max_dist;
}
c = ~in[0] & (in[-width] | in[-width+1] | in[1]);
++in;
*ptr++ = c ? 0.f : max_dist;
for(i=1; i<(width-1); i++) {
c = ~in[0] & (in[-1-width] | in[-width] | in[-width+1] | in[-1] | in[1]);
++in;
*ptr++ = c ? 0.f : max_dist;
}
c = ~in[0] & (in[-1-width] | in[-width] | in[-1]);
++in;
*ptr++ = c ? 0.f : max_dist;
// Update distance in the 4 sweeping directions.
#define update_distance(dx,dy,inc) \
do { \
ptr[0] = fminf(ptr[0], (fabs((dx)-(dy)) >= 1) ? (fminf((dx), (dy)) + 1.f) : (((dx) + (dy) + sqrtf(2.f - ((dx)-(dy))*((dx)-(dy)))) / 2.f)); \
ptr += (inc); \
} while(0)
// y=[0,height[ x=[0,width[
ptr = out;
update_distance(ptr[1], ptr[width], +1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), ptr[width], +1);
}
update_distance(ptr[-1], ptr[width], +1);
line = ptr;
for(j=1; j<(height-1); j++, line+=width) {
ptr = line;
update_distance(ptr[1], fminf(ptr[-width], ptr[width]), +1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), fminf(ptr[-width], ptr[width]), +1);
}
update_distance(ptr[-1], fminf(ptr[-width], ptr[width]), +1);
}
update_distance(ptr[1], ptr[-width], +1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), ptr[-width], +1);
}
update_distance(ptr[-1], ptr[-width], +1);
// y=[0,height[ x=]width,0]
ptr = out+width-1;
update_distance(ptr[-1], ptr[width], -1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), ptr[width], -1);
}
update_distance(ptr[1], ptr[width], -1);
line = out+width;
for(j=1; j<(height-1); j++, line+=width) {
ptr = line+width-1;
update_distance(ptr[-1], fminf(ptr[-width], ptr[width]), -1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), fminf(ptr[-width], ptr[width]), -1);
}
update_distance(ptr[1], fminf(ptr[-width], ptr[width]), -1);
}
ptr = line + width-1;
update_distance(ptr[-1], ptr[-width], -1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), ptr[-width], -1);
}
update_distance(ptr[1], ptr[-width], -1);
// y=]height,0] x=]width,0]
ptr = out+width-1+(height-1)*width;
update_distance(ptr[-1], ptr[-width], -1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), ptr[-width], -1);
}
update_distance(ptr[1], ptr[-width], -1);
for(j=1; j<(height-1); j++) {
update_distance(ptr[-1], fminf(ptr[-width], ptr[width]), -1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), fminf(ptr[-width], ptr[width]), -1);
}
update_distance(ptr[1], fminf(ptr[-width], ptr[width]), -1);
}
update_distance(ptr[-1], ptr[width], -1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), ptr[width], -1);
}
update_distance(ptr[1], ptr[width], -1);
// y=]height,0] x=[0,width[
line = ptr = out+(height-1)*width;
update_distance(ptr[1], ptr[-width], +1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), ptr[-width], +1);
}
update_distance(ptr[-1], ptr[-width], +1);
line -= width;
for(j=1; j<(height-1); j++, line-=width) {
ptr = line;
update_distance(ptr[1], fminf(ptr[-width], ptr[width]), +1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), fminf(ptr[-width], ptr[width]), +1);
}
update_distance(ptr[-1], fminf(ptr[-width], ptr[width]), +1);
}
ptr = line;
update_distance(ptr[1], ptr[width], +1);
for(i=1; i<(width-1); i++) {
update_distance(fminf(ptr[-1], ptr[1]), ptr[width], +1);
}
update_distance(ptr[-1], ptr[width], +1);
#undef update_distance
}
#endif /* DISTANCE_TRANSFORM_IMPLEMENTATION */