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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "precomp.hpp"
#include "_lsvm_distancetransform.h"
/*
// Computation the point of intersection functions
// (parabolas on the variable y)
// a(y - q1) + b(q1 - y)(q1 - y) + f[q1]
// a(y - q2) + b(q2 - y)(q2 - y) + f[q2]
//
//
// API
// int GetPointOfIntersection(const float *f,
const float a, const float b,
int q1, int q2, float *point);
// INPUT
// f - function on the regular grid
// a - coefficient of the function
// b - coefficient of the function
// q1 - parameter of the function
// q2 - parameter of the function
// OUTPUT
// point - point of intersection
// RESULT
// Error status
*/
int GetPointOfIntersection(const float *f,
const float a, const float b,
int q1, int q2, float *point)
{
if (q1 == q2)
{
return DISTANCE_TRANSFORM_EQUAL_POINTS;
} /* if (q1 == q2) */
(*point) = ( (f[q2] - a * q2 + b *q2 * q2) -
(f[q1] - a * q1 + b * q1 * q1) ) / (2 * b * (q2 - q1));
return DISTANCE_TRANSFORM_OK;
}
/*
// Decision of one dimensional problem generalized distance transform
// on the regular grid at all points
// min (a(y' - y) + b(y' - y)(y' - y) + f(y')) (on y')
//
// API
// int DistanceTransformOneDimensionalProblem(const float *f, const int n,
const float a, const float b,
float *distanceTransform,
int *points);
// INPUT
// f - function on the regular grid
// n - grid dimension
// a - coefficient of optimizable function
// b - coefficient of optimizable function
// OUTPUT
// distanceTransform - values of generalized distance transform
// points - arguments that corresponds to the optimal value of function
// RESULT
// Error status
*/
int DistanceTransformOneDimensionalProblem(const float *f, const int n,
const float a, const float b,
float *distanceTransform,
int *points)
{
int i, k;
int tmp;
int diff;
float pointIntersection;
int *v;
float *z;
k = 0;
// Allocation memory (must be free in this function)
v = (int *)malloc (sizeof(int) * n);
z = (float *)malloc (sizeof(float) * (n + 1));
v[0] = 0;
z[0] = (float)F_MIN; // left border of envelope
z[1] = (float)F_MAX; // right border of envelope
for (i = 1; i < n; i++)
{
tmp = GetPointOfIntersection(f, a, b, v[k], i, &pointIntersection);
if (tmp != DISTANCE_TRANSFORM_OK)
{
free(v);
free(z);
return DISTANCE_TRANSFORM_GET_INTERSECTION_ERROR;
} /* if (tmp != DISTANCE_TRANSFORM_OK) */
if (pointIntersection <= z[k])
{
// Envelope doesn't contain current parabola
do
{
k--;
tmp = GetPointOfIntersection(f, a, b, v[k], i, &pointIntersection);
if (tmp != DISTANCE_TRANSFORM_OK)
{
free(v);
free(z);
return DISTANCE_TRANSFORM_GET_INTERSECTION_ERROR;
} /* if (tmp != DISTANCE_TRANSFORM_OK) */
}while (pointIntersection <= z[k]);
// Addition parabola to the envelope
k++;
v[k] = i;
z[k] = pointIntersection;
z[k + 1] = (float)F_MAX;
}
else
{
// Addition parabola to the envelope
k++;
v[k] = i;
z[k] = pointIntersection;
z[k + 1] = (float)F_MAX;
} /* if (pointIntersection <= z[k]) */
}
// Computation values of generalized distance transform at all grid points
k = 0;
for (i = 0; i < n; i++)
{
while (z[k + 1] < i)
{
k++;
}
points[i] = v[k];
diff = i - v[k];
distanceTransform[i] = a * diff + b * diff * diff + f[v[k]];
}
// Release allocated memory
free(v);
free(z);
return DISTANCE_TRANSFORM_OK;
}
/*
// Computation next cycle element
//
// API
// int GetNextCycleElement(int k, int n, int q);
// INPUT
// k - index of the previous cycle element
// n - number of matrix rows
// q - parameter that equal
(number_of_rows * number_of_columns - 1)
// OUTPUT
// None
// RESULT
// Next cycle element
*/
int GetNextCycleElement(int k, int n, int q)
{
return ((k * n) % q);
}
/*
// Transpose cycle elements
//
// API
// void TransposeCycleElements(float *a, int *cycle, int cycle_len)
// INPUT
// a - initial matrix
// cycle - indeces array of cycle
// cycle_len - number of elements in the cycle
// OUTPUT
// a - matrix with transposed elements
// RESULT
// Error status
*/
void TransposeCycleElements(float *a, int *cycle, int cycle_len)
{
int i;
float buf;
for (i = cycle_len - 1; i > 0 ; i--)
{
buf = a[ cycle[i] ];
a[ cycle[i] ] = a[ cycle[i - 1] ];
a[ cycle[i - 1] ] = buf;
}
}
/*
// Transpose cycle elements
//
// API
// void TransposeCycleElements(int *a, int *cycle, int cycle_len)
// INPUT
// a - initial matrix
// cycle - indeces array of cycle
// cycle_len - number of elements in the cycle
// OUTPUT
// a - matrix with transposed elements
// RESULT
// Error status
*/
static void TransposeCycleElements_int(int *a, int *cycle, int cycle_len)
{
int i;
int buf;
for (i = cycle_len - 1; i > 0 ; i--)
{
buf = a[ cycle[i] ];
a[ cycle[i] ] = a[ cycle[i - 1] ];
a[ cycle[i - 1] ] = buf;
}
}
/*
// Getting transposed matrix
//
// API
// void Transpose(float *a, int n, int m);
// INPUT
// a - initial matrix
// n - number of rows
// m - number of columns
// OUTPUT
// a - transposed matrix
// RESULT
// None
*/
void Transpose(float *a, int n, int m)
{
int *cycle;
int i, k, q, cycle_len;
int max_cycle_len;
max_cycle_len = n * m;
// Allocation memory (must be free in this function)
cycle = (int *)malloc(sizeof(int) * max_cycle_len);
cycle_len = 0;
q = n * m - 1;
for (i = 1; i < q; i++)
{
k = GetNextCycleElement(i, n, q);
cycle[cycle_len] = i;
cycle_len++;
while (k > i)
{
cycle[cycle_len] = k;
cycle_len++;
k = GetNextCycleElement(k, n, q);
}
if (k == i)
{
TransposeCycleElements(a, cycle, cycle_len);
} /* if (k == i) */
cycle_len = 0;
}
// Release allocated memory
free(cycle);
}
/*
// Getting transposed matrix
//
// API
// void Transpose_int(int *a, int n, int m);
// INPUT
// a - initial matrix
// n - number of rows
// m - number of columns
// OUTPUT
// a - transposed matrix
// RESULT
// None
*/
static void Transpose_int(int *a, int n, int m)
{
int *cycle;
int i, k, q, cycle_len;
int max_cycle_len;
max_cycle_len = n * m;
// Allocation memory (must be free in this function)
cycle = (int *)malloc(sizeof(int) * max_cycle_len);
cycle_len = 0;
q = n * m - 1;
for (i = 1; i < q; i++)
{
k = GetNextCycleElement(i, n, q);
cycle[cycle_len] = i;
cycle_len++;
while (k > i)
{
cycle[cycle_len] = k;
cycle_len++;
k = GetNextCycleElement(k, n, q);
}
if (k == i)
{
TransposeCycleElements_int(a, cycle, cycle_len);
} /* if (k == i) */
cycle_len = 0;
}
// Release allocated memory
free(cycle);
}
/*
// Decision of two dimensional problem generalized distance transform
// on the regular grid at all points
// min{d2(y' - y) + d4(y' - y)(y' - y) +
min(d1(x' - x) + d3(x' - x)(x' - x) + f(x',y'))} (on x', y')
//
// API
// int DistanceTransformTwoDimensionalProblem(const float *f,
const int n, const int m,
const float coeff[4],
float *distanceTransform,
int *pointsX, int *pointsY);
// INPUT
// f - function on the regular grid
// n - number of rows
// m - number of columns
// coeff - coefficients of optimizable function
coeff[0] = d1, coeff[1] = d2,
coeff[2] = d3, coeff[3] = d4
// OUTPUT
// distanceTransform - values of generalized distance transform
// pointsX - arguments x' that correspond to the optimal value
// pointsY - arguments y' that correspond to the optimal value
// RESULT
// Error status
*/
int DistanceTransformTwoDimensionalProblem(const float *f,
const int n, const int m,
const float coeff[4],
float *distanceTransform,
int *pointsX, int *pointsY)
{
int i, j, tmp;
int resOneDimProblem;
int size = n * m;
std::vector<float> internalDistTrans(size);
std::vector<int> internalPointsX(size);
for (i = 0; i < n; i++)
{
resOneDimProblem = DistanceTransformOneDimensionalProblem(
f + i * m, m,
coeff[0], coeff[2],
&internalDistTrans[i * m],
&internalPointsX[i * m]);
if (resOneDimProblem != DISTANCE_TRANSFORM_OK)
return DISTANCE_TRANSFORM_ERROR;
}
Transpose(&internalDistTrans[0], n, m);
for (j = 0; j < m; j++)
{
resOneDimProblem = DistanceTransformOneDimensionalProblem(
&internalDistTrans[j * n], n,
coeff[1], coeff[3],
distanceTransform + j * n,
pointsY + j * n);
if (resOneDimProblem != DISTANCE_TRANSFORM_OK)
return DISTANCE_TRANSFORM_ERROR;
}
Transpose(distanceTransform, m, n);
Transpose_int(pointsY, m, n);
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
tmp = pointsY[i * m + j];
pointsX[i * m + j] = internalPointsX[tmp * m + j];
}
}
return DISTANCE_TRANSFORM_OK;
}