1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
43 #define ICV_DIST_SHIFT 16
44 #define ICV_INIT_DIST0 (INT_MAX >> 2)
47 icvInitTopBottom( int* temp, int tempstep, CvSize size, int border )
50 for( i = 0; i < border; i++ )
52 int* ttop = (int*)(temp + i*tempstep);
53 int* tbottom = (int*)(temp + (size.height + border*2 - i - 1)*tempstep);
55 for( j = 0; j < size.width + border*2; j++ )
57 ttop[j] = ICV_INIT_DIST0;
58 tbottom[j] = ICV_INIT_DIST0;
66 static CvStatus CV_STDCALL
67 icvDistanceTransform_3x3_C1R( const uchar* src, int srcstep, int* temp,
68 int step, float* dist, int dststep, CvSize size, const float* metrics )
72 const int HV_DIST = CV_FLT_TO_FIX( metrics[0], ICV_DIST_SHIFT );
73 const int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], ICV_DIST_SHIFT );
74 const float scale = 1.f/(1 << ICV_DIST_SHIFT);
76 srcstep /= sizeof(src[0]);
77 step /= sizeof(temp[0]);
78 dststep /= sizeof(dist[0]);
80 icvInitTopBottom( temp, step, size, BORDER );
83 for( i = 0; i < size.height; i++ )
85 const uchar* s = src + i*srcstep;
86 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
88 for( j = 0; j < BORDER; j++ )
89 tmp[-j-1] = tmp[size.width + j] = ICV_INIT_DIST0;
91 for( j = 0; j < size.width; j++ )
97 int t0 = tmp[j-step-1] + DIAG_DIST;
98 int t = tmp[j-step] + HV_DIST;
100 t = tmp[j-step+1] + DIAG_DIST;
102 t = tmp[j-1] + HV_DIST;
110 for( i = size.height - 1; i >= 0; i-- )
112 float* d = (float*)(dist + i*dststep);
113 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
115 for( j = size.width - 1; j >= 0; j-- )
120 int t = tmp[j+step+1] + DIAG_DIST;
122 t = tmp[j+step] + HV_DIST;
124 t = tmp[j+step-1] + DIAG_DIST;
126 t = tmp[j+1] + HV_DIST;
130 d[j] = (float)(t0 * scale);
138 static CvStatus CV_STDCALL
139 icvDistanceTransform_5x5_C1R( const uchar* src, int srcstep, int* temp,
140 int step, float* dist, int dststep, CvSize size, const float* metrics )
142 const int BORDER = 2;
144 const int HV_DIST = CV_FLT_TO_FIX( metrics[0], ICV_DIST_SHIFT );
145 const int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], ICV_DIST_SHIFT );
146 const int LONG_DIST = CV_FLT_TO_FIX( metrics[2], ICV_DIST_SHIFT );
147 const float scale = 1.f/(1 << ICV_DIST_SHIFT);
149 srcstep /= sizeof(src[0]);
150 step /= sizeof(temp[0]);
151 dststep /= sizeof(dist[0]);
153 icvInitTopBottom( temp, step, size, BORDER );
156 for( i = 0; i < size.height; i++ )
158 const uchar* s = src + i*srcstep;
159 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
161 for( j = 0; j < BORDER; j++ )
162 tmp[-j-1] = tmp[size.width + j] = ICV_INIT_DIST0;
164 for( j = 0; j < size.width; j++ )
170 int t0 = tmp[j-step*2-1] + LONG_DIST;
171 int t = tmp[j-step*2+1] + LONG_DIST;
173 t = tmp[j-step-2] + LONG_DIST;
175 t = tmp[j-step-1] + DIAG_DIST;
177 t = tmp[j-step] + HV_DIST;
179 t = tmp[j-step+1] + DIAG_DIST;
181 t = tmp[j-step+2] + LONG_DIST;
183 t = tmp[j-1] + HV_DIST;
191 for( i = size.height - 1; i >= 0; i-- )
193 float* d = (float*)(dist + i*dststep);
194 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
196 for( j = size.width - 1; j >= 0; j-- )
201 int t = tmp[j+step*2+1] + LONG_DIST;
203 t = tmp[j+step*2-1] + LONG_DIST;
205 t = tmp[j+step+2] + LONG_DIST;
207 t = tmp[j+step+1] + DIAG_DIST;
209 t = tmp[j+step] + HV_DIST;
211 t = tmp[j+step-1] + DIAG_DIST;
213 t = tmp[j+step-2] + LONG_DIST;
215 t = tmp[j+1] + HV_DIST;
219 d[j] = (float)(t0 * scale);
227 static CvStatus CV_STDCALL
228 icvDistanceTransformEx_5x5_C1R( const uchar* src, int srcstep, int* temp,
229 int step, float* dist, int dststep, int* labels, int lstep,
230 CvSize size, const float* metrics )
232 const int BORDER = 2;
235 const int HV_DIST = CV_FLT_TO_FIX( metrics[0], ICV_DIST_SHIFT );
236 const int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], ICV_DIST_SHIFT );
237 const int LONG_DIST = CV_FLT_TO_FIX( metrics[2], ICV_DIST_SHIFT );
238 const float scale = 1.f/(1 << ICV_DIST_SHIFT);
240 srcstep /= sizeof(src[0]);
241 step /= sizeof(temp[0]);
242 dststep /= sizeof(dist[0]);
243 lstep /= sizeof(labels[0]);
245 icvInitTopBottom( temp, step, size, BORDER );
248 for( i = 0; i < size.height; i++ )
250 const uchar* s = src + i*srcstep;
251 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
252 int* lls = (int*)(labels + i*lstep);
254 for( j = 0; j < BORDER; j++ )
255 tmp[-j-1] = tmp[size.width + j] = ICV_INIT_DIST0;
257 for( j = 0; j < size.width; j++ )
262 //assert( lls[j] != 0 );
266 int t0 = ICV_INIT_DIST0, t;
269 t = tmp[j-step*2-1] + LONG_DIST;
273 l0 = lls[j-lstep*2-1];
275 t = tmp[j-step*2+1] + LONG_DIST;
279 l0 = lls[j-lstep*2+1];
281 t = tmp[j-step-2] + LONG_DIST;
287 t = tmp[j-step-1] + DIAG_DIST;
293 t = tmp[j-step] + HV_DIST;
299 t = tmp[j-step+1] + DIAG_DIST;
305 t = tmp[j-step+2] + LONG_DIST;
311 t = tmp[j-1] + HV_DIST;
325 for( i = size.height - 1; i >= 0; i-- )
327 float* d = (float*)(dist + i*dststep);
328 int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
329 int* lls = (int*)(labels + i*lstep);
331 for( j = size.width - 1; j >= 0; j-- )
337 int t = tmp[j+step*2+1] + LONG_DIST;
341 l0 = lls[j+lstep*2+1];
343 t = tmp[j+step*2-1] + LONG_DIST;
347 l0 = lls[j+lstep*2-1];
349 t = tmp[j+step+2] + LONG_DIST;
355 t = tmp[j+step+1] + DIAG_DIST;
361 t = tmp[j+step] + HV_DIST;
367 t = tmp[j+step-1] + DIAG_DIST;
373 t = tmp[j+step-2] + LONG_DIST;
379 t = tmp[j+1] + HV_DIST;
388 d[j] = (float)(t0 * scale);
397 icvGetDistanceTransformMask( int maskType, float *metrics )
400 return CV_NULLPTR_ERR;
416 metrics[1] = 1.3693f;
434 metrics[2] = 2.1969f;
437 return CV_BADRANGE_ERR;
445 icvTrueDistTrans( const CvMat* src, CvMat* dst )
449 CV_FUNCNAME( "cvDistTransform2" );
455 const float inf = 1e6f;
456 int thread_count = cvGetNumThreads();
457 int pass1_sz, pass2_sz;
459 if( !CV_ARE_SIZES_EQ( src, dst ))
460 CV_ERROR( CV_StsUnmatchedSizes, "" );
462 if( CV_MAT_TYPE(src->type) != CV_8UC1 ||
463 CV_MAT_TYPE(dst->type) != CV_32FC1 )
464 CV_ERROR( CV_StsUnsupportedFormat,
465 "The input image must have 8uC1 type and the output one must have 32fC1 type" );
470 // (see stage 1 below):
471 // sqr_tab: 2*m, sat_tab: 3*m + 1, d: m*thread_count,
472 pass1_sz = src->rows*(5 + thread_count) + 1;
474 // sqr_tab & inv_tab: n each; f & v: n*thread_count each; z: (n+1)*thread_count
475 pass2_sz = src->cols*(2 + thread_count*3) + thread_count;
476 CV_CALL( buffer = cvCreateMat( 1, MAX(pass1_sz, pass2_sz), CV_32FC1 ));
479 dstep = dst->step / sizeof(float);
481 // stage 1: compute 1d distance transform of each column
483 float* sqr_tab = buffer->data.fl;
484 int* sat_tab = (int*)(sqr_tab + m*2);
485 const int shift = m*2;
487 for( i = 0; i < m; i++ )
488 sqr_tab[i] = (float)(i*i);
489 for( i = m; i < m*2; i++ )
491 for( i = 0; i < shift; i++ )
493 for( ; i <= m*3; i++ )
494 sat_tab[i] = i - shift;
497 #pragma omp parallel for num_threads(thread_count)
499 for( i = 0; i < n; i++ )
501 const uchar* sptr = src->data.ptr + i + (m-1)*sstep;
502 float* dptr = dst->data.fl + i;
503 int* d = (int*)(sat_tab + m*3+1+m*cvGetThreadNum());
506 for( j = m-1; j >= 0; j--, sptr -= sstep )
508 dist = (dist + 1) & (sptr[0] == 0 ? 0 : -1);
513 for( j = 0; j < m; j++, dptr += dstep )
515 dist = dist + 1 - sat_tab[dist + 1 - d[j] + shift];
517 dptr[0] = sqr_tab[dist];
522 // stage 2: compute modified distance transform for each row
524 float* inv_tab = buffer->data.fl;
525 float* sqr_tab = inv_tab + n;
527 inv_tab[0] = sqr_tab[0] = 0.f;
528 for( i = 1; i < n; i++ )
530 inv_tab[i] = (float)(0.5/i);
531 sqr_tab[i] = (float)(i*i);
535 #pragma omp parallel for num_threads(thread_count), schedule(dynamic)
537 for( i = 0; i < m; i++ )
539 float* d = (float*)(dst->data.ptr + i*dst->step);
540 float* f = sqr_tab + n + (n*3+1)*cvGetThreadNum();
542 int* v = (int*)(z + n + 1);
550 for( q = 1, k = 0; q < n; q++ )
558 float s = (fq + sqr_tab[q] - d[p] - sqr_tab[p])*inv_tab[q - p];
570 for( q = 0, k = 0; q < n; q++ )
575 d[q] = sqr_tab[abs(q - p)] + f[p];
580 cvPow( dst, dst, 0.5 );
584 cvReleaseMat( &buffer );
588 /*********************************** IPP functions *********************************/
590 icvDistanceTransform_3x3_8u32f_C1R_t icvDistanceTransform_3x3_8u32f_C1R_p = 0;
591 icvDistanceTransform_5x5_8u32f_C1R_t icvDistanceTransform_5x5_8u32f_C1R_p = 0;
592 icvDistanceTransform_3x3_8u_C1IR_t icvDistanceTransform_3x3_8u_C1IR_p = 0;
593 icvDistanceTransform_3x3_8u_C1R_t icvDistanceTransform_3x3_8u_C1R_p = 0;
595 typedef CvStatus (CV_STDCALL * CvIPPDistTransFunc)( const uchar* src, int srcstep,
596 void* dst, int dststep,
597 CvSize size, const void* metrics );
599 typedef CvStatus (CV_STDCALL * CvIPPDistTransFunc2)( uchar* src, int srcstep,
600 CvSize size, const int* metrics );
602 /***********************************************************************************/
604 typedef CvStatus (CV_STDCALL * CvDistTransFunc)( const uchar* src, int srcstep,
605 int* temp, int tempstep,
606 float* dst, int dststep,
607 CvSize size, const float* metrics );
610 /****************************************************************************************\
611 User-contributed code:
613 Non-inplace and Inplace 8u->8u Distance Transform for CityBlock (a.k.a. L1) metric
614 (C) 2006 by Jay Stavinzky.
615 \****************************************************************************************/
618 /* 8-bit grayscale distance transform function */
620 icvDistanceATS_L1_8u( const CvMat* src, CvMat* dst )
622 CV_FUNCNAME( "cvDistanceATS" );
626 int width = src->cols, height = src->rows;
632 const uchar *sbase = src->data.ptr;
633 uchar *dbase = dst->data.ptr;
634 int srcstep = src->step;
635 int dststep = dst->step;
637 CV_ASSERT( CV_IS_MASK_ARR( src ) && CV_MAT_TYPE( dst->type ) == CV_8UC1 );
638 CV_ASSERT( CV_ARE_SIZES_EQ( src, dst ));
640 ////////////////////// forward scan ////////////////////////
641 for( x = 0; x < 256; x++ )
642 lut[x] = CV_CAST_8U(x+1);
644 //init first pixel to max (we're going to be skipping it)
645 dbase[0] = (uchar)(sbase[0] == 0 ? 0 : 255);
647 //first row (scan west only, skip first pixel)
648 for( x = 1; x < width; x++ )
649 dbase[x] = (uchar)(sbase[x] == 0 ? 0 : lut[dbase[x-1]]);
651 for( y = 1; y < height; y++ )
656 //for left edge, scan north only
657 a = sbase[0] == 0 ? 0 : lut[dbase[-dststep]];
660 for( x = 1; x < width; x++ )
662 a = sbase[x] == 0 ? 0 : lut[CV_CALC_MIN_8U(a, dbase[x - dststep])];
667 ////////////////////// backward scan ///////////////////////
671 // do last row east pixel scan here (skip bottom right pixel)
672 for( x = width - 2; x >= 0; x-- )
675 dbase[x] = (uchar)(CV_CALC_MIN_8U(a, dbase[x]));
678 // right edge is the only error case
679 for( y = height - 2; y >= 0; y-- )
684 a = lut[dbase[width-1+dststep]];
685 dbase[width-1] = (uchar)(CV_CALC_MIN_8U(a, dbase[width-1]));
687 for( x = width - 2; x >= 0; x-- )
689 int b = dbase[x+dststep];
690 a = lut[CV_CALC_MIN_8U(a, b)];
691 dbase[x] = (uchar)(CV_CALC_MIN_8U(a, dbase[x]));
700 /* Wrapper function for distance transform group */
702 cvDistTransform( const void* srcarr, void* dstarr,
703 int distType, int maskSize,
709 CvMemStorage* st = 0;
711 CV_FUNCNAME( "cvDistTransform" );
715 float _mask[5] = {0};
717 CvMat srcstub, *src = (CvMat*)srcarr;
718 CvMat dststub, *dst = (CvMat*)dstarr;
719 CvMat lstub, *labels = (CvMat*)labelsarr;
721 CvIPPDistTransFunc ipp_func = 0;
722 CvIPPDistTransFunc2 ipp_inp_func = 0;
724 CV_CALL( src = cvGetMat( src, &srcstub ));
725 CV_CALL( dst = cvGetMat( dst, &dststub ));
727 if( !CV_IS_MASK_ARR( src ) || CV_MAT_TYPE( dst->type ) != CV_32FC1 &&
728 (CV_MAT_TYPE(dst->type) != CV_8UC1 || distType != CV_DIST_L1 || labels) )
729 CV_ERROR( CV_StsUnsupportedFormat,
730 "source image must be 8uC1 and the distance map must be 32fC1 "
731 "(or 8uC1 in case of simple L1 distance transform)" );
733 if( !CV_ARE_SIZES_EQ( src, dst ))
734 CV_ERROR( CV_StsUnmatchedSizes, "the source and the destination images must be of the same size" );
736 if( maskSize != CV_DIST_MASK_3 && maskSize != CV_DIST_MASK_5 && maskSize != CV_DIST_MASK_PRECISE )
737 CV_ERROR( CV_StsBadSize, "Mask size should be 3 or 5 or 0 (presize)" );
739 if( distType == CV_DIST_C || distType == CV_DIST_L1 )
740 maskSize = !labels ? CV_DIST_MASK_3 : CV_DIST_MASK_5;
741 else if( distType == CV_DIST_L2 && labels )
742 maskSize = CV_DIST_MASK_5;
744 if( maskSize == CV_DIST_MASK_PRECISE )
746 CV_CALL( icvTrueDistTrans( src, dst ));
752 CV_CALL( labels = cvGetMat( labels, &lstub ));
753 if( CV_MAT_TYPE( labels->type ) != CV_32SC1 )
754 CV_ERROR( CV_StsUnsupportedFormat, "the output array of labels must be 32sC1" );
756 if( !CV_ARE_SIZES_EQ( labels, dst ))
757 CV_ERROR( CV_StsUnmatchedSizes, "the array of labels has a different size" );
759 if( maskSize == CV_DIST_MASK_3 )
760 CV_ERROR( CV_StsNotImplemented,
761 "3x3 mask can not be used for \"labeled\" distance transform. Use 5x5 mask" );
764 if( distType == CV_DIST_C || distType == CV_DIST_L1 || distType == CV_DIST_L2 )
766 icvGetDistanceTransformMask( (distType == CV_DIST_C ? 0 :
767 distType == CV_DIST_L1 ? 1 : 2) + maskSize*10, _mask );
769 else if( distType == CV_DIST_USER )
772 CV_ERROR( CV_StsNullPtr, "" );
774 memcpy( _mask, mask, (maskSize/2 + 1)*sizeof(float));
779 if( CV_MAT_TYPE(dst->type) == CV_32FC1 )
780 ipp_func = (CvIPPDistTransFunc)(maskSize == CV_DIST_MASK_3 ?
781 icvDistanceTransform_3x3_8u32f_C1R_p : icvDistanceTransform_5x5_8u32f_C1R_p);
782 else if( src->data.ptr != dst->data.ptr )
783 ipp_func = (CvIPPDistTransFunc)icvDistanceTransform_3x3_8u_C1R_p;
785 ipp_inp_func = icvDistanceTransform_3x3_8u_C1IR_p;
788 size = cvGetMatSize(src);
790 if( (ipp_func || ipp_inp_func) && src->cols >= 4 && src->rows >= 2 )
792 _imask[0] = cvRound(_mask[0]);
793 _imask[1] = cvRound(_mask[1]);
794 _imask[2] = cvRound(_mask[2]);
798 IPPI_CALL( ipp_func( src->data.ptr, src->step,
799 dst->data.fl, dst->step, size,
800 CV_MAT_TYPE(dst->type) == CV_8UC1 ?
801 (void*)_imask : (void*)_mask ));
805 IPPI_CALL( ipp_inp_func( src->data.ptr, src->step, size, _imask ));
808 else if( CV_MAT_TYPE(dst->type) == CV_8UC1 )
810 CV_CALL( icvDistanceATS_L1_8u( src, dst ));
814 int border = maskSize == CV_DIST_MASK_3 ? 1 : 2;
815 CV_CALL( temp = cvCreateMat( size.height + border*2, size.width + border*2, CV_32SC1 ));
819 CvDistTransFunc func = maskSize == CV_DIST_MASK_3 ?
820 icvDistanceTransform_3x3_C1R :
821 icvDistanceTransform_5x5_C1R;
823 func( src->data.ptr, src->step, temp->data.i, temp->step,
824 dst->data.fl, dst->step, size, _mask );
829 CvPoint top_left = {0,0}, bottom_right = {size.width-1,size.height-1};
832 CV_CALL( st = cvCreateMemStorage() );
833 CV_CALL( src_copy = cvCreateMat( size.height, size.width, src->type ));
834 cvCmpS( src, 0, src_copy, CV_CMP_EQ );
835 cvFindContours( src_copy, st, &contours, sizeof(CvContour),
836 CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
838 for( label = 1; contours != 0; contours = contours->h_next, label++ )
840 CvScalar area_color = cvScalarAll(label);
841 cvDrawContours( labels, contours, area_color, area_color, -255, -1, 8 );
844 cvCopy( src, src_copy );
845 cvRectangle( src_copy, top_left, bottom_right, cvScalarAll(255), 1, 8 );
847 icvDistanceTransformEx_5x5_C1R( src_copy->data.ptr, src_copy->step, temp->data.i, temp->step,
848 dst->data.fl, dst->step, labels->data.i, labels->step, size, _mask );
854 cvReleaseMat( &temp );
855 cvReleaseMat( &src_copy );
856 cvReleaseMemStorage( &st );