Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/src/distransform.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000, Intel Corporation, all rights reserved.
14
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
#include "precomp.hpp"
43
44
namespace cv
45
{
46
47
static const int DIST_SHIFT = 16;
48
static const int INIT_DIST0 = INT_MAX;
49
static const int DIST_MAX = (INT_MAX >> 2);
50
#define CV_FLT_TO_FIX(x,n) cvRound((x)*(1<<(n)))
51
52
static void
53
initTopBottom( Mat& temp, int border )
54
{
55
Size size = temp.size();
56
for( int i = 0; i < border; i++ )
57
{
58
int* ttop = temp.ptr<int>(i);
59
int* tbottom = temp.ptr<int>(size.height - i - 1);
60
61
for( int j = 0; j < size.width; j++ )
62
{
63
ttop[j] = INIT_DIST0;
64
tbottom[j] = INIT_DIST0;
65
}
66
}
67
}
68
69
70
static void
71
distanceTransform_3x3( const Mat& _src, Mat& _temp, Mat& _dist, const float* metrics )
72
{
73
const int BORDER = 1;
74
int i, j;
75
const unsigned int HV_DIST = CV_FLT_TO_FIX( metrics[0], DIST_SHIFT );
76
const unsigned int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], DIST_SHIFT );
77
const float scale = 1.f/(1 << DIST_SHIFT);
78
79
const uchar* src = _src.ptr();
80
int* temp = _temp.ptr<int>();
81
float* dist = _dist.ptr<float>();
82
int srcstep = (int)(_src.step/sizeof(src[0]));
83
int step = (int)(_temp.step/sizeof(temp[0]));
84
int dststep = (int)(_dist.step/sizeof(dist[0]));
85
Size size = _src.size();
86
87
initTopBottom( _temp, BORDER );
88
89
// forward pass
90
for( i = 0; i < size.height; i++ )
91
{
92
const uchar* s = src + i*srcstep;
93
unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;
94
95
for( j = 0; j < BORDER; j++ )
96
tmp[-j-1] = tmp[size.width + j] = INIT_DIST0;
97
98
for( j = 0; j < size.width; j++ )
99
{
100
if( !s[j] )
101
tmp[j] = 0;
102
else
103
{
104
unsigned int t0 = tmp[j-step-1] + DIAG_DIST;
105
unsigned int t = tmp[j-step] + HV_DIST;
106
if( t0 > t ) t0 = t;
107
t = tmp[j-step+1] + DIAG_DIST;
108
if( t0 > t ) t0 = t;
109
t = tmp[j-1] + HV_DIST;
110
if( t0 > t ) t0 = t;
111
tmp[j] = t0;
112
}
113
}
114
}
115
116
// backward pass
117
for( i = size.height - 1; i >= 0; i-- )
118
{
119
float* d = (float*)(dist + i*dststep);
120
unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;
121
122
for( j = size.width - 1; j >= 0; j-- )
123
{
124
unsigned int t0 = tmp[j];
125
if( t0 > HV_DIST )
126
{
127
unsigned int t = tmp[j+step+1] + DIAG_DIST;
128
if( t0 > t ) t0 = t;
129
t = tmp[j+step] + HV_DIST;
130
if( t0 > t ) t0 = t;
131
t = tmp[j+step-1] + DIAG_DIST;
132
if( t0 > t ) t0 = t;
133
t = tmp[j+1] + HV_DIST;
134
if( t0 > t ) t0 = t;
135
tmp[j] = t0;
136
}
137
t0 = (t0 > DIST_MAX) ? DIST_MAX : t0;
138
d[j] = (float)(t0 * scale);
139
}
140
}
141
}
142
143
144
static void
145
distanceTransform_5x5( const Mat& _src, Mat& _temp, Mat& _dist, const float* metrics )
146
{
147
const int BORDER = 2;
148
int i, j;
149
const unsigned int HV_DIST = CV_FLT_TO_FIX( metrics[0], DIST_SHIFT );
150
const unsigned int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], DIST_SHIFT );
151
const unsigned int LONG_DIST = CV_FLT_TO_FIX( metrics[2], DIST_SHIFT );
152
const float scale = 1.f/(1 << DIST_SHIFT);
153
154
const uchar* src = _src.ptr();
155
int* temp = _temp.ptr<int>();
156
float* dist = _dist.ptr<float>();
157
int srcstep = (int)(_src.step/sizeof(src[0]));
158
int step = (int)(_temp.step/sizeof(temp[0]));
159
int dststep = (int)(_dist.step/sizeof(dist[0]));
160
Size size = _src.size();
161
162
initTopBottom( _temp, BORDER );
163
164
// forward pass
165
for( i = 0; i < size.height; i++ )
166
{
167
const uchar* s = src + i*srcstep;
168
unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;
169
170
for( j = 0; j < BORDER; j++ )
171
tmp[-j-1] = tmp[size.width + j] = INIT_DIST0;
172
173
for( j = 0; j < size.width; j++ )
174
{
175
if( !s[j] )
176
tmp[j] = 0;
177
else
178
{
179
unsigned int t0 = tmp[j-step*2-1] + LONG_DIST;
180
unsigned int t = tmp[j-step*2+1] + LONG_DIST;
181
if( t0 > t ) t0 = t;
182
t = tmp[j-step-2] + LONG_DIST;
183
if( t0 > t ) t0 = t;
184
t = tmp[j-step-1] + DIAG_DIST;
185
if( t0 > t ) t0 = t;
186
t = tmp[j-step] + HV_DIST;
187
if( t0 > t ) t0 = t;
188
t = tmp[j-step+1] + DIAG_DIST;
189
if( t0 > t ) t0 = t;
190
t = tmp[j-step+2] + LONG_DIST;
191
if( t0 > t ) t0 = t;
192
t = tmp[j-1] + HV_DIST;
193
if( t0 > t ) t0 = t;
194
tmp[j] = t0;
195
}
196
}
197
}
198
199
// backward pass
200
for( i = size.height - 1; i >= 0; i-- )
201
{
202
float* d = (float*)(dist + i*dststep);
203
unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;
204
205
for( j = size.width - 1; j >= 0; j-- )
206
{
207
unsigned int t0 = tmp[j];
208
if( t0 > HV_DIST )
209
{
210
unsigned int t = tmp[j+step*2+1] + LONG_DIST;
211
if( t0 > t ) t0 = t;
212
t = tmp[j+step*2-1] + LONG_DIST;
213
if( t0 > t ) t0 = t;
214
t = tmp[j+step+2] + LONG_DIST;
215
if( t0 > t ) t0 = t;
216
t = tmp[j+step+1] + DIAG_DIST;
217
if( t0 > t ) t0 = t;
218
t = tmp[j+step] + HV_DIST;
219
if( t0 > t ) t0 = t;
220
t = tmp[j+step-1] + DIAG_DIST;
221
if( t0 > t ) t0 = t;
222
t = tmp[j+step-2] + LONG_DIST;
223
if( t0 > t ) t0 = t;
224
t = tmp[j+1] + HV_DIST;
225
if( t0 > t ) t0 = t;
226
tmp[j] = t0;
227
}
228
t0 = (t0 > DIST_MAX) ? DIST_MAX : t0;
229
d[j] = (float)(t0 * scale);
230
}
231
}
232
}
233
234
235
static void
236
distanceTransformEx_5x5( const Mat& _src, Mat& _temp, Mat& _dist, Mat& _labels, const float* metrics )
237
{
238
const int BORDER = 2;
239
240
int i, j;
241
const unsigned int HV_DIST = CV_FLT_TO_FIX( metrics[0], DIST_SHIFT );
242
const unsigned int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], DIST_SHIFT );
243
const unsigned int LONG_DIST = CV_FLT_TO_FIX( metrics[2], DIST_SHIFT );
244
const float scale = 1.f/(1 << DIST_SHIFT);
245
246
const uchar* src = _src.ptr();
247
int* temp = _temp.ptr<int>();
248
float* dist = _dist.ptr<float>();
249
int* labels = _labels.ptr<int>();
250
int srcstep = (int)(_src.step/sizeof(src[0]));
251
int step = (int)(_temp.step/sizeof(temp[0]));
252
int dststep = (int)(_dist.step/sizeof(dist[0]));
253
int lstep = (int)(_labels.step/sizeof(labels[0]));
254
Size size = _src.size();
255
256
initTopBottom( _temp, BORDER );
257
258
// forward pass
259
for( i = 0; i < size.height; i++ )
260
{
261
const uchar* s = src + i*srcstep;
262
unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;
263
int* lls = (int*)(labels + i*lstep);
264
265
for( j = 0; j < BORDER; j++ )
266
tmp[-j-1] = tmp[size.width + j] = INIT_DIST0;
267
268
for( j = 0; j < size.width; j++ )
269
{
270
if( !s[j] )
271
{
272
tmp[j] = 0;
273
//assert( lls[j] != 0 );
274
}
275
else
276
{
277
unsigned int t0 = INIT_DIST0, t;
278
int l0 = 0;
279
280
t = tmp[j-step*2-1] + LONG_DIST;
281
if( t0 > t )
282
{
283
t0 = t;
284
l0 = lls[j-lstep*2-1];
285
}
286
t = tmp[j-step*2+1] + LONG_DIST;
287
if( t0 > t )
288
{
289
t0 = t;
290
l0 = lls[j-lstep*2+1];
291
}
292
t = tmp[j-step-2] + LONG_DIST;
293
if( t0 > t )
294
{
295
t0 = t;
296
l0 = lls[j-lstep-2];
297
}
298
t = tmp[j-step-1] + DIAG_DIST;
299
if( t0 > t )
300
{
301
t0 = t;
302
l0 = lls[j-lstep-1];
303
}
304
t = tmp[j-step] + HV_DIST;
305
if( t0 > t )
306
{
307
t0 = t;
308
l0 = lls[j-lstep];
309
}
310
t = tmp[j-step+1] + DIAG_DIST;
311
if( t0 > t )
312
{
313
t0 = t;
314
l0 = lls[j-lstep+1];
315
}
316
t = tmp[j-step+2] + LONG_DIST;
317
if( t0 > t )
318
{
319
t0 = t;
320
l0 = lls[j-lstep+2];
321
}
322
t = tmp[j-1] + HV_DIST;
323
if( t0 > t )
324
{
325
t0 = t;
326
l0 = lls[j-1];
327
}
328
329
tmp[j] = t0;
330
lls[j] = l0;
331
}
332
}
333
}
334
335
// backward pass
336
for( i = size.height - 1; i >= 0; i-- )
337
{
338
float* d = (float*)(dist + i*dststep);
339
unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;
340
int* lls = (int*)(labels + i*lstep);
341
342
for( j = size.width - 1; j >= 0; j-- )
343
{
344
unsigned int t0 = tmp[j];
345
int l0 = lls[j];
346
if( t0 > HV_DIST )
347
{
348
unsigned int t = tmp[j+step*2+1] + LONG_DIST;
349
if( t0 > t )
350
{
351
t0 = t;
352
l0 = lls[j+lstep*2+1];
353
}
354
t = tmp[j+step*2-1] + LONG_DIST;
355
if( t0 > t )
356
{
357
t0 = t;
358
l0 = lls[j+lstep*2-1];
359
}
360
t = tmp[j+step+2] + LONG_DIST;
361
if( t0 > t )
362
{
363
t0 = t;
364
l0 = lls[j+lstep+2];
365
}
366
t = tmp[j+step+1] + DIAG_DIST;
367
if( t0 > t )
368
{
369
t0 = t;
370
l0 = lls[j+lstep+1];
371
}
372
t = tmp[j+step] + HV_DIST;
373
if( t0 > t )
374
{
375
t0 = t;
376
l0 = lls[j+lstep];
377
}
378
t = tmp[j+step-1] + DIAG_DIST;
379
if( t0 > t )
380
{
381
t0 = t;
382
l0 = lls[j+lstep-1];
383
}
384
t = tmp[j+step-2] + LONG_DIST;
385
if( t0 > t )
386
{
387
t0 = t;
388
l0 = lls[j+lstep-2];
389
}
390
t = tmp[j+1] + HV_DIST;
391
if( t0 > t )
392
{
393
t0 = t;
394
l0 = lls[j+1];
395
}
396
tmp[j] = t0;
397
lls[j] = l0;
398
}
399
t0 = (t0 > DIST_MAX) ? DIST_MAX : t0;
400
d[j] = (float)(t0 * scale);
401
}
402
}
403
}
404
405
406
static void getDistanceTransformMask( int maskType, float *metrics )
407
{
408
CV_Assert( metrics != 0 );
409
410
switch (maskType)
411
{
412
case 30:
413
metrics[0] = 1.0f;
414
metrics[1] = 1.0f;
415
break;
416
417
case 31:
418
metrics[0] = 1.0f;
419
metrics[1] = 2.0f;
420
break;
421
422
case 32:
423
metrics[0] = 0.955f;
424
metrics[1] = 1.3693f;
425
break;
426
427
case 50:
428
metrics[0] = 1.0f;
429
metrics[1] = 1.0f;
430
metrics[2] = 2.0f;
431
break;
432
433
case 51:
434
metrics[0] = 1.0f;
435
metrics[1] = 2.0f;
436
metrics[2] = 3.0f;
437
break;
438
439
case 52:
440
metrics[0] = 1.0f;
441
metrics[1] = 1.4f;
442
metrics[2] = 2.1969f;
443
break;
444
default:
445
CV_Error(CV_StsBadArg, "Unknown metric type");
446
}
447
}
448
449
struct DTColumnInvoker : ParallelLoopBody
450
{
451
DTColumnInvoker( const Mat* _src, Mat* _dst, const int* _sat_tab, const float* _sqr_tab)
452
{
453
src = _src;
454
dst = _dst;
455
sat_tab = _sat_tab + src->rows*2 + 1;
456
sqr_tab = _sqr_tab;
457
}
458
459
void operator()(const Range& range) const CV_OVERRIDE
460
{
461
int i, i1 = range.start, i2 = range.end;
462
int m = src->rows;
463
size_t sstep = src->step, dstep = dst->step/sizeof(float);
464
AutoBuffer<int> _d(m);
465
int* d = _d.data();
466
467
for( i = i1; i < i2; i++ )
468
{
469
const uchar* sptr = src->ptr(m-1) + i;
470
float* dptr = dst->ptr<float>() + i;
471
int j, dist = m-1;
472
473
for( j = m-1; j >= 0; j--, sptr -= sstep )
474
{
475
dist = (dist + 1) & (sptr[0] == 0 ? 0 : -1);
476
d[j] = dist;
477
}
478
479
dist = m-1;
480
for( j = 0; j < m; j++, dptr += dstep )
481
{
482
dist = dist + 1 - sat_tab[dist - d[j]];
483
d[j] = dist;
484
dptr[0] = sqr_tab[dist];
485
}
486
}
487
}
488
489
const Mat* src;
490
Mat* dst;
491
const int* sat_tab;
492
const float* sqr_tab;
493
};
494
495
struct DTRowInvoker : ParallelLoopBody
496
{
497
DTRowInvoker( Mat* _dst, const float* _sqr_tab, const float* _inv_tab )
498
{
499
dst = _dst;
500
sqr_tab = _sqr_tab;
501
inv_tab = _inv_tab;
502
}
503
504
void operator()(const Range& range) const CV_OVERRIDE
505
{
506
const float inf = 1e15f;
507
int i, i1 = range.start, i2 = range.end;
508
int n = dst->cols;
509
AutoBuffer<uchar> _buf((n+2)*2*sizeof(float) + (n+2)*sizeof(int));
510
float* f = (float*)_buf.data();
511
float* z = f + n;
512
int* v = alignPtr((int*)(z + n + 1), sizeof(int));
513
514
for( i = i1; i < i2; i++ )
515
{
516
float* d = dst->ptr<float>(i);
517
int p, q, k;
518
519
v[0] = 0;
520
z[0] = -inf;
521
z[1] = inf;
522
f[0] = d[0];
523
524
for( q = 1, k = 0; q < n; q++ )
525
{
526
float fq = d[q];
527
f[q] = fq;
528
529
for(;;k--)
530
{
531
p = v[k];
532
float s = (fq + sqr_tab[q] - d[p] - sqr_tab[p])*inv_tab[q - p];
533
if( s > z[k] )
534
{
535
k++;
536
v[k] = q;
537
z[k] = s;
538
z[k+1] = inf;
539
break;
540
}
541
}
542
}
543
544
for( q = 0, k = 0; q < n; q++ )
545
{
546
while( z[k+1] < q )
547
k++;
548
p = v[k];
549
d[q] = std::sqrt(sqr_tab[std::abs(q - p)] + f[p]);
550
}
551
}
552
}
553
554
Mat* dst;
555
const float* sqr_tab;
556
const float* inv_tab;
557
};
558
559
static void
560
trueDistTrans( const Mat& src, Mat& dst )
561
{
562
const float inf = 1e15f;
563
564
CV_Assert( src.size() == dst.size() );
565
566
CV_Assert( src.type() == CV_8UC1 && dst.type() == CV_32FC1 );
567
int i, m = src.rows, n = src.cols;
568
569
cv::AutoBuffer<uchar> _buf(std::max(m*2*sizeof(float) + (m*3+1)*sizeof(int), n*2*sizeof(float)));
570
// stage 1: compute 1d distance transform of each column
571
float* sqr_tab = (float*)_buf.data();
572
int* sat_tab = cv::alignPtr((int*)(sqr_tab + m*2), sizeof(int));
573
int shift = m*2;
574
575
for( i = 0; i < m; i++ )
576
sqr_tab[i] = (float)(i*i);
577
for( i = m; i < m*2; i++ )
578
sqr_tab[i] = inf;
579
for( i = 0; i < shift; i++ )
580
sat_tab[i] = 0;
581
for( ; i <= m*3; i++ )
582
sat_tab[i] = i - shift;
583
584
cv::parallel_for_(cv::Range(0, n), cv::DTColumnInvoker(&src, &dst, sat_tab, sqr_tab), src.total()/(double)(1<<16));
585
586
// stage 2: compute modified distance transform for each row
587
float* inv_tab = sqr_tab + n;
588
589
inv_tab[0] = sqr_tab[0] = 0.f;
590
for( i = 1; i < n; i++ )
591
{
592
inv_tab[i] = (float)(0.5/i);
593
sqr_tab[i] = (float)(i*i);
594
}
595
596
cv::parallel_for_(cv::Range(0, m), cv::DTRowInvoker(&dst, sqr_tab, inv_tab));
597
}
598
599
600
/****************************************************************************************\
601
Non-inplace and Inplace 8u->8u Distance Transform for CityBlock (a.k.a. L1) metric
602
(C) 2006 by Jay Stavinzky.
603
\****************************************************************************************/
604
605
//BEGIN ATS ADDITION
606
// 8-bit grayscale distance transform function
607
static void
608
distanceATS_L1_8u( const Mat& src, Mat& dst )
609
{
610
int width = src.cols, height = src.rows;
611
612
int a;
613
uchar lut[256];
614
int x, y;
615
616
const uchar *sbase = src.ptr();
617
uchar *dbase = dst.ptr();
618
int srcstep = (int)src.step;
619
int dststep = (int)dst.step;
620
621
CV_Assert( src.type() == CV_8UC1 && dst.type() == CV_8UC1 );
622
CV_Assert( src.size() == dst.size() );
623
624
////////////////////// forward scan ////////////////////////
625
for( x = 0; x < 256; x++ )
626
lut[x] = cv::saturate_cast<uchar>(x+1);
627
628
//init first pixel to max (we're going to be skipping it)
629
dbase[0] = (uchar)(sbase[0] == 0 ? 0 : 255);
630
631
//first row (scan west only, skip first pixel)
632
for( x = 1; x < width; x++ )
633
dbase[x] = (uchar)(sbase[x] == 0 ? 0 : lut[dbase[x-1]]);
634
635
for( y = 1; y < height; y++ )
636
{
637
sbase += srcstep;
638
dbase += dststep;
639
640
//for left edge, scan north only
641
a = sbase[0] == 0 ? 0 : lut[dbase[-dststep]];
642
dbase[0] = (uchar)a;
643
644
for( x = 1; x < width; x++ )
645
{
646
a = sbase[x] == 0 ? 0 : lut[MIN(a, dbase[x - dststep])];
647
dbase[x] = (uchar)a;
648
}
649
}
650
651
////////////////////// backward scan ///////////////////////
652
653
a = dbase[width-1];
654
655
// do last row east pixel scan here (skip bottom right pixel)
656
for( x = width - 2; x >= 0; x-- )
657
{
658
a = lut[a];
659
dbase[x] = (uchar)(CV_CALC_MIN_8U(a, dbase[x]));
660
}
661
662
// right edge is the only error case
663
for( y = height - 2; y >= 0; y-- )
664
{
665
dbase -= dststep;
666
667
// do right edge
668
a = lut[dbase[width-1+dststep]];
669
a = dbase[width-1] = (uchar)(MIN(a, dbase[width-1]));
670
671
for( x = width - 2; x >= 0; x-- )
672
{
673
int b = dbase[x+dststep];
674
a = lut[MIN(a, b)];
675
a = MIN(a, dbase[x]);
676
dbase[x] = (uchar)(a);
677
}
678
}
679
}
680
//END ATS ADDITION
681
682
}
683
684
namespace cv
685
{
686
static void distanceTransform_L1_8U(InputArray _src, OutputArray _dst)
687
{
688
CV_INSTRUMENT_REGION();
689
690
Mat src = _src.getMat();
691
692
CV_Assert( src.type() == CV_8UC1);
693
694
_dst.create( src.size(), CV_8UC1);
695
Mat dst = _dst.getMat();
696
697
#ifdef HAVE_IPP
698
CV_IPP_CHECK()
699
{
700
IppiSize roi = { src.cols, src.rows };
701
Ipp32s pMetrics[2] = { 1, 2 }; //L1, 3x3 mask
702
if (CV_INSTRUMENT_FUN_IPP(ippiDistanceTransform_3x3_8u_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<uchar>(), (int)dst.step, roi, pMetrics) >= 0)
703
{
704
CV_IMPL_ADD(CV_IMPL_IPP);
705
return;
706
}
707
setIppErrorStatus();
708
}
709
#endif
710
711
distanceATS_L1_8u(src, dst);
712
}
713
}
714
715
// Wrapper function for distance transform group
716
void cv::distanceTransform( InputArray _src, OutputArray _dst, OutputArray _labels,
717
int distType, int maskSize, int labelType )
718
{
719
CV_INSTRUMENT_REGION();
720
721
Mat src = _src.getMat(), labels;
722
bool need_labels = _labels.needed();
723
724
CV_Assert( src.type() == CV_8UC1);
725
726
_dst.create( src.size(), CV_32F);
727
Mat dst = _dst.getMat();
728
729
if( need_labels )
730
{
731
CV_Assert( labelType == DIST_LABEL_PIXEL || labelType == DIST_LABEL_CCOMP );
732
733
_labels.create(src.size(), CV_32S);
734
labels = _labels.getMat();
735
maskSize = CV_DIST_MASK_5;
736
}
737
738
float _mask[5] = {0};
739
740
if( maskSize != CV_DIST_MASK_3 && maskSize != CV_DIST_MASK_5 && maskSize != CV_DIST_MASK_PRECISE )
741
CV_Error( CV_StsBadSize, "Mask size should be 3 or 5 or 0 (precise)" );
742
743
if( distType == CV_DIST_C || distType == CV_DIST_L1 )
744
maskSize = !need_labels ? CV_DIST_MASK_3 : CV_DIST_MASK_5;
745
else if( distType == CV_DIST_L2 && need_labels )
746
maskSize = CV_DIST_MASK_5;
747
748
if( maskSize == CV_DIST_MASK_PRECISE )
749
{
750
751
#ifdef HAVE_IPP
752
CV_IPP_CHECK()
753
{
754
#if IPP_DISABLE_PERF_TRUE_DIST_MT
755
if(cv::getNumThreads()<=1 || (src.total()<(int)(1<<14)))
756
#endif
757
{
758
IppStatus status;
759
IppiSize roi = { src.cols, src.rows };
760
Ipp8u *pBuffer;
761
int bufSize=0;
762
763
status = ippiTrueDistanceTransformGetBufferSize_8u32f_C1R(roi, &bufSize);
764
if (status>=0)
765
{
766
pBuffer = (Ipp8u *)CV_IPP_MALLOC( bufSize );
767
status = CV_INSTRUMENT_FUN_IPP(ippiTrueDistanceTransform_8u32f_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<float>(), (int)dst.step, roi, pBuffer);
768
ippFree( pBuffer );
769
if (status>=0)
770
{
771
CV_IMPL_ADD(CV_IMPL_IPP);
772
return;
773
}
774
setIppErrorStatus();
775
}
776
}
777
}
778
#endif
779
780
trueDistTrans( src, dst );
781
return;
782
}
783
784
CV_Assert( distType == CV_DIST_C || distType == CV_DIST_L1 || distType == CV_DIST_L2 );
785
786
getDistanceTransformMask( (distType == CV_DIST_C ? 0 :
787
distType == CV_DIST_L1 ? 1 : 2) + maskSize*10, _mask );
788
789
Size size = src.size();
790
791
int border = maskSize == CV_DIST_MASK_3 ? 1 : 2;
792
Mat temp( size.height + border*2, size.width + border*2, CV_32SC1 );
793
794
if( !need_labels )
795
{
796
if( maskSize == CV_DIST_MASK_3 )
797
{
798
#if defined (HAVE_IPP) && (IPP_VERSION_X100 >= 700)
799
CV_IPP_CHECK()
800
{
801
IppiSize roi = { src.cols, src.rows };
802
if (CV_INSTRUMENT_FUN_IPP(ippiDistanceTransform_3x3_8u32f_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<float>(), (int)dst.step, roi, _mask) >= 0)
803
{
804
CV_IMPL_ADD(CV_IMPL_IPP);
805
return;
806
}
807
setIppErrorStatus();
808
}
809
#endif
810
811
distanceTransform_3x3(src, temp, dst, _mask);
812
}
813
else
814
{
815
#if defined (HAVE_IPP) && (IPP_VERSION_X100 >= 700)
816
CV_IPP_CHECK()
817
{
818
IppiSize roi = { src.cols, src.rows };
819
if (CV_INSTRUMENT_FUN_IPP(ippiDistanceTransform_5x5_8u32f_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<float>(), (int)dst.step, roi, _mask) >= 0)
820
{
821
CV_IMPL_ADD(CV_IMPL_IPP);
822
return;
823
}
824
setIppErrorStatus();
825
}
826
#endif
827
828
distanceTransform_5x5(src, temp, dst, _mask);
829
}
830
}
831
else
832
{
833
labels.setTo(Scalar::all(0));
834
835
if( labelType == CV_DIST_LABEL_CCOMP )
836
{
837
Mat zpix = src == 0;
838
connectedComponents(zpix, labels, 8, CV_32S, CCL_WU);
839
}
840
else
841
{
842
int k = 1;
843
for( int i = 0; i < src.rows; i++ )
844
{
845
const uchar* srcptr = src.ptr(i);
846
int* labelptr = labels.ptr<int>(i);
847
848
for( int j = 0; j < src.cols; j++ )
849
if( srcptr[j] == 0 )
850
labelptr[j] = k++;
851
}
852
}
853
854
distanceTransformEx_5x5( src, temp, dst, labels, _mask );
855
}
856
}
857
858
void cv::distanceTransform( InputArray _src, OutputArray _dst,
859
int distanceType, int maskSize, int dstType)
860
{
861
CV_INSTRUMENT_REGION();
862
863
if (distanceType == CV_DIST_L1 && dstType==CV_8U)
864
distanceTransform_L1_8U(_src, _dst);
865
else
866
distanceTransform(_src, _dst, noArray(), distanceType, maskSize, DIST_LABEL_PIXEL);
867
868
}
869
870
CV_IMPL void
871
cvDistTransform( const void* srcarr, void* dstarr,
872
int distType, int maskSize,
873
const float * /*mask*/,
874
void* labelsarr, int labelType )
875
{
876
cv::Mat src = cv::cvarrToMat(srcarr);
877
const cv::Mat dst = cv::cvarrToMat(dstarr);
878
const cv::Mat labels = cv::cvarrToMat(labelsarr);
879
880
cv::distanceTransform(src, dst, labelsarr ? cv::_OutputArray(labels) : cv::_OutputArray(),
881
distType, maskSize, labelType);
882
883
}
884
885
886
/* End of file. */
887
888