1 /*M///////////////////////////////////////////////////////////////////////////////////////
\r
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
\r
5 // By downloading, copying, installing or using the software you agree to this license.
\r
6 // If you do not agree to this license, do not download, install,
\r
7 // copy or use the software.
\r
10 // Intel License Agreement
\r
11 // For Open Source Computer Vision Library
\r
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
\r
14 // Third party copyrights are property of their respective owners.
\r
16 // Redistribution and use in source and binary forms, with or without modification,
\r
17 // are permitted provided that the following conditions are met:
\r
19 // * Redistribution's of source code must retain the above copyright notice,
\r
20 // this list of conditions and the following disclaimer.
\r
22 // * Redistribution's in binary form must reproduce the above copyright notice,
\r
23 // this list of conditions and the following disclaimer in the documentation
\r
24 // and/or other materials provided with the distribution.
\r
26 // * The name of Intel Corporation may not be used to endorse or promote products
\r
27 // derived from this software without specific prior written permission.
\r
29 // This software is provided by the copyright holders and contributors "as is" and
\r
30 // any express or implied warranties, including, but not limited to, the implied
\r
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
\r
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
\r
33 // indirect, incidental, special, exemplary, or consequential damages
\r
34 // (including, but not limited to, procurement of substitute goods or services;
\r
35 // loss of use, data, or profits; or business interruption) however caused
\r
36 // and on any theory of liability, whether in contract, strict liability,
\r
37 // or tort (including negligence or otherwise) arising in any way out of
\r
38 // the use of this software, even if advised of the possibility of such damage.
\r
43 #define ICV_DIST_SHIFT 16
\r
44 #define ICV_INIT_DIST0 (INT_MAX >> 2)
\r
47 icvInitTopBottom( int* temp, int tempstep, CvSize size, int border )
\r
50 for( i = 0; i < border; i++ )
\r
52 int* ttop = (int*)(temp + i*tempstep);
\r
53 int* tbottom = (int*)(temp + (size.height + border*2 - i - 1)*tempstep);
\r
55 for( j = 0; j < size.width + border*2; j++ )
\r
57 ttop[j] = ICV_INIT_DIST0;
\r
58 tbottom[j] = ICV_INIT_DIST0;
\r
66 static CvStatus CV_STDCALL
\r
67 icvDistanceTransform_3x3_C1R( const uchar* src, int srcstep, int* temp,
\r
68 int step, float* dist, int dststep, CvSize size, const float* metrics )
\r
70 const int BORDER = 1;
\r
72 const int HV_DIST = CV_FLT_TO_FIX( metrics[0], ICV_DIST_SHIFT );
\r
73 const int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], ICV_DIST_SHIFT );
\r
74 const float scale = 1.f/(1 << ICV_DIST_SHIFT);
\r
76 srcstep /= sizeof(src[0]);
\r
77 step /= sizeof(temp[0]);
\r
78 dststep /= sizeof(dist[0]);
\r
80 icvInitTopBottom( temp, step, size, BORDER );
\r
83 for( i = 0; i < size.height; i++ )
\r
85 const uchar* s = src + i*srcstep;
\r
86 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
\r
88 for( j = 0; j < BORDER; j++ )
\r
89 tmp[-j-1] = tmp[size.width + j] = ICV_INIT_DIST0;
\r
91 for( j = 0; j < size.width; j++ )
\r
97 int t0 = tmp[j-step-1] + DIAG_DIST;
\r
98 int t = tmp[j-step] + HV_DIST;
\r
99 if( t0 > t ) t0 = t;
\r
100 t = tmp[j-step+1] + DIAG_DIST;
\r
101 if( t0 > t ) t0 = t;
\r
102 t = tmp[j-1] + HV_DIST;
\r
103 if( t0 > t ) t0 = t;
\r
110 for( i = size.height - 1; i >= 0; i-- )
\r
112 float* d = (float*)(dist + i*dststep);
\r
113 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
\r
115 for( j = size.width - 1; j >= 0; j-- )
\r
120 int t = tmp[j+step+1] + DIAG_DIST;
\r
121 if( t0 > t ) t0 = t;
\r
122 t = tmp[j+step] + HV_DIST;
\r
123 if( t0 > t ) t0 = t;
\r
124 t = tmp[j+step-1] + DIAG_DIST;
\r
125 if( t0 > t ) t0 = t;
\r
126 t = tmp[j+1] + HV_DIST;
\r
127 if( t0 > t ) t0 = t;
\r
130 d[j] = (float)(t0 * scale);
\r
138 static CvStatus CV_STDCALL
\r
139 icvDistanceTransform_5x5_C1R( const uchar* src, int srcstep, int* temp,
\r
140 int step, float* dist, int dststep, CvSize size, const float* metrics )
\r
142 const int BORDER = 2;
\r
144 const int HV_DIST = CV_FLT_TO_FIX( metrics[0], ICV_DIST_SHIFT );
\r
145 const int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], ICV_DIST_SHIFT );
\r
146 const int LONG_DIST = CV_FLT_TO_FIX( metrics[2], ICV_DIST_SHIFT );
\r
147 const float scale = 1.f/(1 << ICV_DIST_SHIFT);
\r
149 srcstep /= sizeof(src[0]);
\r
150 step /= sizeof(temp[0]);
\r
151 dststep /= sizeof(dist[0]);
\r
153 icvInitTopBottom( temp, step, size, BORDER );
\r
156 for( i = 0; i < size.height; i++ )
\r
158 const uchar* s = src + i*srcstep;
\r
159 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
\r
161 for( j = 0; j < BORDER; j++ )
\r
162 tmp[-j-1] = tmp[size.width + j] = ICV_INIT_DIST0;
\r
164 for( j = 0; j < size.width; j++ )
\r
170 int t0 = tmp[j-step*2-1] + LONG_DIST;
\r
171 int t = tmp[j-step*2+1] + LONG_DIST;
\r
172 if( t0 > t ) t0 = t;
\r
173 t = tmp[j-step-2] + LONG_DIST;
\r
174 if( t0 > t ) t0 = t;
\r
175 t = tmp[j-step-1] + DIAG_DIST;
\r
176 if( t0 > t ) t0 = t;
\r
177 t = tmp[j-step] + HV_DIST;
\r
178 if( t0 > t ) t0 = t;
\r
179 t = tmp[j-step+1] + DIAG_DIST;
\r
180 if( t0 > t ) t0 = t;
\r
181 t = tmp[j-step+2] + LONG_DIST;
\r
182 if( t0 > t ) t0 = t;
\r
183 t = tmp[j-1] + HV_DIST;
\r
184 if( t0 > t ) t0 = t;
\r
191 for( i = size.height - 1; i >= 0; i-- )
\r
193 float* d = (float*)(dist + i*dststep);
\r
194 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
\r
196 for( j = size.width - 1; j >= 0; j-- )
\r
201 int t = tmp[j+step*2+1] + LONG_DIST;
\r
202 if( t0 > t ) t0 = t;
\r
203 t = tmp[j+step*2-1] + LONG_DIST;
\r
204 if( t0 > t ) t0 = t;
\r
205 t = tmp[j+step+2] + LONG_DIST;
\r
206 if( t0 > t ) t0 = t;
\r
207 t = tmp[j+step+1] + DIAG_DIST;
\r
208 if( t0 > t ) t0 = t;
\r
209 t = tmp[j+step] + HV_DIST;
\r
210 if( t0 > t ) t0 = t;
\r
211 t = tmp[j+step-1] + DIAG_DIST;
\r
212 if( t0 > t ) t0 = t;
\r
213 t = tmp[j+step-2] + LONG_DIST;
\r
214 if( t0 > t ) t0 = t;
\r
215 t = tmp[j+1] + HV_DIST;
\r
216 if( t0 > t ) t0 = t;
\r
219 d[j] = (float)(t0 * scale);
\r
227 static CvStatus CV_STDCALL
\r
228 icvDistanceTransformEx_5x5_C1R( const uchar* src, int srcstep, int* temp,
\r
229 int step, float* dist, int dststep, int* labels, int lstep,
\r
230 CvSize size, const float* metrics )
\r
232 const int BORDER = 2;
\r
235 const int HV_DIST = CV_FLT_TO_FIX( metrics[0], ICV_DIST_SHIFT );
\r
236 const int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], ICV_DIST_SHIFT );
\r
237 const int LONG_DIST = CV_FLT_TO_FIX( metrics[2], ICV_DIST_SHIFT );
\r
238 const float scale = 1.f/(1 << ICV_DIST_SHIFT);
\r
240 srcstep /= sizeof(src[0]);
\r
241 step /= sizeof(temp[0]);
\r
242 dststep /= sizeof(dist[0]);
\r
243 lstep /= sizeof(labels[0]);
\r
245 icvInitTopBottom( temp, step, size, BORDER );
\r
248 for( i = 0; i < size.height; i++ )
\r
250 const uchar* s = src + i*srcstep;
\r
251 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
\r
252 int* lls = (int*)(labels + i*lstep);
\r
254 for( j = 0; j < BORDER; j++ )
\r
255 tmp[-j-1] = tmp[size.width + j] = ICV_INIT_DIST0;
\r
257 for( j = 0; j < size.width; j++ )
\r
262 //assert( lls[j] != 0 );
\r
266 int t0 = ICV_INIT_DIST0, t;
\r
269 t = tmp[j-step*2-1] + LONG_DIST;
\r
273 l0 = lls[j-lstep*2-1];
\r
275 t = tmp[j-step*2+1] + LONG_DIST;
\r
279 l0 = lls[j-lstep*2+1];
\r
281 t = tmp[j-step-2] + LONG_DIST;
\r
285 l0 = lls[j-lstep-2];
\r
287 t = tmp[j-step-1] + DIAG_DIST;
\r
291 l0 = lls[j-lstep-1];
\r
293 t = tmp[j-step] + HV_DIST;
\r
299 t = tmp[j-step+1] + DIAG_DIST;
\r
303 l0 = lls[j-lstep+1];
\r
305 t = tmp[j-step+2] + LONG_DIST;
\r
309 l0 = lls[j-lstep+2];
\r
311 t = tmp[j-1] + HV_DIST;
\r
325 for( i = size.height - 1; i >= 0; i-- )
\r
327 float* d = (float*)(dist + i*dststep);
\r
328 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
\r
329 int* lls = (int*)(labels + i*lstep);
\r
331 for( j = size.width - 1; j >= 0; j-- )
\r
337 int t = tmp[j+step*2+1] + LONG_DIST;
\r
341 l0 = lls[j+lstep*2+1];
\r
343 t = tmp[j+step*2-1] + LONG_DIST;
\r
347 l0 = lls[j+lstep*2-1];
\r
349 t = tmp[j+step+2] + LONG_DIST;
\r
353 l0 = lls[j+lstep+2];
\r
355 t = tmp[j+step+1] + DIAG_DIST;
\r
359 l0 = lls[j+lstep+1];
\r
361 t = tmp[j+step] + HV_DIST;
\r
367 t = tmp[j+step-1] + DIAG_DIST;
\r
371 l0 = lls[j+lstep-1];
\r
373 t = tmp[j+step-2] + LONG_DIST;
\r
377 l0 = lls[j+lstep-2];
\r
379 t = tmp[j+1] + HV_DIST;
\r
388 d[j] = (float)(t0 * scale);
\r
397 icvGetDistanceTransformMask( int maskType, float *metrics )
\r
400 return CV_NULLPTR_ERR;
\r
415 metrics[0] = 0.955f;
\r
416 metrics[1] = 1.3693f;
\r
434 metrics[2] = 2.1969f;
\r
437 return CV_BADRANGE_ERR;
\r
445 icvTrueDistTrans( const CvMat* src, CvMat* dst )
\r
449 CV_FUNCNAME( "cvDistTransform2" );
\r
455 const float inf = 1e6f;
\r
456 int thread_count = cvGetNumThreads();
\r
457 int pass1_sz, pass2_sz;
\r
459 if( !CV_ARE_SIZES_EQ( src, dst ))
\r
460 CV_ERROR( CV_StsUnmatchedSizes, "" );
\r
462 if( CV_MAT_TYPE(src->type) != CV_8UC1 ||
\r
463 CV_MAT_TYPE(dst->type) != CV_32FC1 )
\r
464 CV_ERROR( CV_StsUnsupportedFormat,
\r
465 "The input image must have 8uC1 type and the output one must have 32fC1 type" );
\r
470 // (see stage 1 below):
\r
471 // sqr_tab: 2*m, sat_tab: 3*m + 1, d: m*thread_count,
\r
472 pass1_sz = src->rows*(5 + thread_count) + 1;
\r
474 // sqr_tab & inv_tab: n each; f & v: n*thread_count each; z: (n+1)*thread_count
\r
475 pass2_sz = src->cols*(2 + thread_count*3) + thread_count;
\r
476 CV_CALL( buffer = cvCreateMat( 1, MAX(pass1_sz, pass2_sz), CV_32FC1 ));
\r
479 dstep = dst->step / sizeof(float);
\r
481 // stage 1: compute 1d distance transform of each column
\r
483 float* sqr_tab = buffer->data.fl;
\r
484 int* sat_tab = (int*)(sqr_tab + m*2);
\r
485 const int shift = m*2;
\r
487 for( i = 0; i < m; i++ )
\r
488 sqr_tab[i] = (float)(i*i);
\r
489 for( i = m; i < m*2; i++ )
\r
491 for( i = 0; i < shift; i++ )
\r
493 for( ; i <= m*3; i++ )
\r
494 sat_tab[i] = i - shift;
\r
497 #pragma omp parallel for num_threads(thread_count)
\r
499 for( i = 0; i < n; i++ )
\r
501 const uchar* sptr = src->data.ptr + i + (m-1)*sstep;
\r
502 float* dptr = dst->data.fl + i;
\r
503 int* d = (int*)(sat_tab + m*3+1+m*cvGetThreadNum());
\r
506 for( j = m-1; j >= 0; j--, sptr -= sstep )
\r
508 dist = (dist + 1) & (sptr[0] == 0 ? 0 : -1);
\r
513 for( j = 0; j < m; j++, dptr += dstep )
\r
515 dist = dist + 1 - sat_tab[dist + 1 - d[j] + shift];
\r
517 dptr[0] = sqr_tab[dist];
\r
522 // stage 2: compute modified distance transform for each row
\r
524 float* inv_tab = buffer->data.fl;
\r
525 float* sqr_tab = inv_tab + n;
\r
527 inv_tab[0] = sqr_tab[0] = 0.f;
\r
528 for( i = 1; i < n; i++ )
\r
530 inv_tab[i] = (float)(0.5/i);
\r
531 sqr_tab[i] = (float)(i*i);
\r
535 #pragma omp parallel for num_threads(thread_count) schedule(dynamic)
\r
537 for( i = 0; i < m; i++ )
\r
539 float* d = (float*)(dst->data.ptr + i*dst->step);
\r
540 float* f = sqr_tab + n + (n*3+1)*cvGetThreadNum();
\r
542 int* v = (int*)(z + n + 1);
\r
550 for( q = 1, k = 0; q < n; q++ )
\r
558 float s = (fq + sqr_tab[q] - d[p] - sqr_tab[p])*inv_tab[q - p];
\r
570 for( q = 0, k = 0; q < n; q++ )
\r
572 while( z[k+1] < q )
\r
575 d[q] = sqr_tab[abs(q - p)] + f[p];
\r
580 cvPow( dst, dst, 0.5 );
\r
584 cvReleaseMat( &buffer );
\r
588 /*********************************** IPP functions *********************************/
\r
590 typedef CvStatus (CV_STDCALL * CvIPPDistTransFunc)( const uchar* src, int srcstep,
\r
591 void* dst, int dststep,
\r
592 CvSize size, const void* metrics );
\r
594 typedef CvStatus (CV_STDCALL * CvIPPDistTransFunc2)( uchar* src, int srcstep,
\r
595 CvSize size, const int* metrics );
\r
597 /***********************************************************************************/
\r
599 typedef CvStatus (CV_STDCALL * CvDistTransFunc)( const uchar* src, int srcstep,
\r
600 int* temp, int tempstep,
\r
601 float* dst, int dststep,
\r
602 CvSize size, const float* metrics );
\r
605 /****************************************************************************************\
\r
606 Non-inplace and Inplace 8u->8u Distance Transform for CityBlock (a.k.a. L1) metric
\r
607 (C) 2006 by Jay Stavinzky.
\r
608 \****************************************************************************************/
\r
610 //BEGIN ATS ADDITION
\r
611 /* 8-bit grayscale distance transform function */
\r
613 icvDistanceATS_L1_8u( const CvMat* src, CvMat* dst )
\r
615 CV_FUNCNAME( "cvDistanceATS" );
\r
619 int width = src->cols, height = src->rows;
\r
625 const uchar *sbase = src->data.ptr;
\r
626 uchar *dbase = dst->data.ptr;
\r
627 int srcstep = src->step;
\r
628 int dststep = dst->step;
\r
630 CV_ASSERT( CV_IS_MASK_ARR( src ) && CV_MAT_TYPE( dst->type ) == CV_8UC1 );
\r
631 CV_ASSERT( CV_ARE_SIZES_EQ( src, dst ));
\r
633 ////////////////////// forward scan ////////////////////////
\r
634 for( x = 0; x < 256; x++ )
\r
635 lut[x] = CV_CAST_8U(x+1);
\r
637 //init first pixel to max (we're going to be skipping it)
\r
638 dbase[0] = (uchar)(sbase[0] == 0 ? 0 : 255);
\r
640 //first row (scan west only, skip first pixel)
\r
641 for( x = 1; x < width; x++ )
\r
642 dbase[x] = (uchar)(sbase[x] == 0 ? 0 : lut[dbase[x-1]]);
\r
644 for( y = 1; y < height; y++ )
\r
649 //for left edge, scan north only
\r
650 a = sbase[0] == 0 ? 0 : lut[dbase[-dststep]];
\r
651 dbase[0] = (uchar)a;
\r
653 for( x = 1; x < width; x++ )
\r
655 a = sbase[x] == 0 ? 0 : lut[MIN(a, dbase[x - dststep])];
\r
656 dbase[x] = (uchar)a;
\r
660 ////////////////////// backward scan ///////////////////////
\r
662 a = dbase[width-1];
\r
664 // do last row east pixel scan here (skip bottom right pixel)
\r
665 for( x = width - 2; x >= 0; x-- )
\r
668 dbase[x] = (uchar)(CV_CALC_MIN_8U(a, dbase[x]));
\r
671 // right edge is the only error case
\r
672 for( y = height - 2; y >= 0; y-- )
\r
677 a = lut[dbase[width-1+dststep]];
\r
678 dbase[width-1] = (uchar)(MIN(a, dbase[width-1]));
\r
680 for( x = width - 2; x >= 0; x-- )
\r
682 int b = dbase[x+dststep];
\r
683 a = lut[MIN(a, b)];
\r
684 dbase[x] = (uchar)(MIN(a, dbase[x]));
\r
693 /* Wrapper function for distance transform group */
\r
695 cvDistTransform( const void* srcarr, void* dstarr,
\r
696 int distType, int maskSize,
\r
701 CvMat* src_copy = 0;
\r
702 CvMemStorage* st = 0;
\r
704 CV_FUNCNAME( "cvDistTransform" );
\r
708 float _mask[5] = {0};
\r
709 CvMat srcstub, *src = (CvMat*)srcarr;
\r
710 CvMat dststub, *dst = (CvMat*)dstarr;
\r
711 CvMat lstub, *labels = (CvMat*)labelsarr;
\r
713 //CvIPPDistTransFunc ipp_func = 0;
\r
714 //CvIPPDistTransFunc2 ipp_inp_func = 0;
\r
716 CV_CALL( src = cvGetMat( src, &srcstub ));
\r
717 CV_CALL( dst = cvGetMat( dst, &dststub ));
\r
719 if( !CV_IS_MASK_ARR( src ) || (CV_MAT_TYPE( dst->type ) != CV_32FC1 &&
\r
720 (CV_MAT_TYPE(dst->type) != CV_8UC1 || distType != CV_DIST_L1 || labels)) )
\r
721 CV_ERROR( CV_StsUnsupportedFormat,
\r
722 "source image must be 8uC1 and the distance map must be 32fC1 "
\r
723 "(or 8uC1 in case of simple L1 distance transform)" );
\r
725 if( !CV_ARE_SIZES_EQ( src, dst ))
\r
726 CV_ERROR( CV_StsUnmatchedSizes, "the source and the destination images must be of the same size" );
\r
728 if( maskSize != CV_DIST_MASK_3 && maskSize != CV_DIST_MASK_5 && maskSize != CV_DIST_MASK_PRECISE )
\r
729 CV_ERROR( CV_StsBadSize, "Mask size should be 3 or 5 or 0 (presize)" );
\r
731 if( distType == CV_DIST_C || distType == CV_DIST_L1 )
\r
732 maskSize = !labels ? CV_DIST_MASK_3 : CV_DIST_MASK_5;
\r
733 else if( distType == CV_DIST_L2 && labels )
\r
734 maskSize = CV_DIST_MASK_5;
\r
736 if( maskSize == CV_DIST_MASK_PRECISE )
\r
738 CV_CALL( icvTrueDistTrans( src, dst ));
\r
744 CV_CALL( labels = cvGetMat( labels, &lstub ));
\r
745 if( CV_MAT_TYPE( labels->type ) != CV_32SC1 )
\r
746 CV_ERROR( CV_StsUnsupportedFormat, "the output array of labels must be 32sC1" );
\r
748 if( !CV_ARE_SIZES_EQ( labels, dst ))
\r
749 CV_ERROR( CV_StsUnmatchedSizes, "the array of labels has a different size" );
\r
751 if( maskSize == CV_DIST_MASK_3 )
\r
752 CV_ERROR( CV_StsNotImplemented,
\r
753 "3x3 mask can not be used for \"labeled\" distance transform. Use 5x5 mask" );
\r
756 if( distType == CV_DIST_C || distType == CV_DIST_L1 || distType == CV_DIST_L2 )
\r
758 icvGetDistanceTransformMask( (distType == CV_DIST_C ? 0 :
\r
759 distType == CV_DIST_L1 ? 1 : 2) + maskSize*10, _mask );
\r
761 else if( distType == CV_DIST_USER )
\r
764 CV_ERROR( CV_StsNullPtr, "" );
\r
766 memcpy( _mask, mask, (maskSize/2 + 1)*sizeof(float));
\r
771 if( CV_MAT_TYPE(dst->type) == CV_32FC1 )
\r
772 ipp_func = (CvIPPDistTransFunc)(maskSize == CV_DIST_MASK_3 ?
\r
773 icvDistanceTransform_3x3_8u32f_C1R_p : icvDistanceTransform_5x5_8u32f_C1R_p);
\r
774 else if( src->data.ptr != dst->data.ptr )
\r
775 ipp_func = (CvIPPDistTransFunc)icvDistanceTransform_3x3_8u_C1R_p;
\r
777 ipp_inp_func = icvDistanceTransform_3x3_8u_C1IR_p;
\r
780 size = cvGetMatSize(src);
\r
782 /*if( (ipp_func || ipp_inp_func) && src->cols >= 4 && src->rows >= 2 )
\r
785 _imask[0] = cvRound(_mask[0]);
\r
786 _imask[1] = cvRound(_mask[1]);
\r
787 _imask[2] = cvRound(_mask[2]);
\r
791 IPPI_CALL( ipp_func( src->data.ptr, src->step,
\r
792 dst->data.fl, dst->step, size,
\r
793 CV_MAT_TYPE(dst->type) == CV_8UC1 ?
\r
794 (void*)_imask : (void*)_mask ));
\r
798 IPPI_CALL( ipp_inp_func( src->data.ptr, src->step, size, _imask ));
\r
801 else*/ if( CV_MAT_TYPE(dst->type) == CV_8UC1 )
\r
803 CV_CALL( icvDistanceATS_L1_8u( src, dst ));
\r
807 int border = maskSize == CV_DIST_MASK_3 ? 1 : 2;
\r
808 CV_CALL( temp = cvCreateMat( size.height + border*2, size.width + border*2, CV_32SC1 ));
\r
812 CvDistTransFunc func = maskSize == CV_DIST_MASK_3 ?
\r
813 icvDistanceTransform_3x3_C1R :
\r
814 icvDistanceTransform_5x5_C1R;
\r
816 func( src->data.ptr, src->step, temp->data.i, temp->step,
\r
817 dst->data.fl, dst->step, size, _mask );
\r
821 CvSeq *contours = 0;
\r
822 CvPoint top_left = {0,0}, bottom_right = {size.width-1,size.height-1};
\r
825 CV_CALL( st = cvCreateMemStorage() );
\r
826 CV_CALL( src_copy = cvCreateMat( size.height, size.width, src->type ));
\r
827 cvCmpS( src, 0, src_copy, CV_CMP_EQ );
\r
828 cvFindContours( src_copy, st, &contours, sizeof(CvContour),
\r
829 CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
\r
831 for( label = 1; contours != 0; contours = contours->h_next, label++ )
\r
833 CvScalar area_color = cvScalarAll(label);
\r
834 cvDrawContours( labels, contours, area_color, area_color, -255, -1, 8 );
\r
837 cvCopy( src, src_copy );
\r
838 cvRectangle( src_copy, top_left, bottom_right, cvScalarAll(255), 1, 8 );
\r
840 icvDistanceTransformEx_5x5_C1R( src_copy->data.ptr, src_copy->step, temp->data.i, temp->step,
\r
841 dst->data.fl, dst->step, labels->data.i, labels->step, size, _mask );
\r
847 cvReleaseMat( &temp );
\r
848 cvReleaseMat( &src_copy );
\r
849 cvReleaseMemStorage( &st );
\r
852 void cv::distanceTransform( const Mat& src, Mat& dst, Mat& labels,
\r
853 int distanceType, int maskSize )
\r
855 dst.create(src.size(), CV_32F);
\r
856 dst.create(src.size(), CV_32S);
\r
857 CvMat _src = src, _dst = dst, _labels = labels;
\r
858 cvDistTransform(&_src, &_dst, distanceType, maskSize, 0, &_labels);
\r
861 void cv::distanceTransform( const Mat& src, Mat& dst,
\r
862 int distanceType, int maskSize )
\r
864 dst.create(src.size(), CV_32F);
\r
865 CvMat _src = src, _dst = dst;
\r
866 cvDistTransform(&_src, &_dst, distanceType, maskSize, 0, 0);
\r