Path: blob/master/modules/imgproc/src/distransform.cpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Copyright (C) 2013, OpenCV Foundation, all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// 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 damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/41#include "precomp.hpp"4243namespace cv44{4546static const int DIST_SHIFT = 16;47static const int INIT_DIST0 = INT_MAX;48static const int DIST_MAX = (INT_MAX >> 2);49#define CV_FLT_TO_FIX(x,n) cvRound((x)*(1<<(n)))5051static void52initTopBottom( Mat& temp, int border )53{54Size size = temp.size();55for( int i = 0; i < border; i++ )56{57int* ttop = temp.ptr<int>(i);58int* tbottom = temp.ptr<int>(size.height - i - 1);5960for( int j = 0; j < size.width; j++ )61{62ttop[j] = INIT_DIST0;63tbottom[j] = INIT_DIST0;64}65}66}676869static void70distanceTransform_3x3( const Mat& _src, Mat& _temp, Mat& _dist, const float* metrics )71{72const int BORDER = 1;73int i, j;74const unsigned int HV_DIST = CV_FLT_TO_FIX( metrics[0], DIST_SHIFT );75const unsigned int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], DIST_SHIFT );76const float scale = 1.f/(1 << DIST_SHIFT);7778const uchar* src = _src.ptr();79int* temp = _temp.ptr<int>();80float* dist = _dist.ptr<float>();81int srcstep = (int)(_src.step/sizeof(src[0]));82int step = (int)(_temp.step/sizeof(temp[0]));83int dststep = (int)(_dist.step/sizeof(dist[0]));84Size size = _src.size();8586initTopBottom( _temp, BORDER );8788// forward pass89for( i = 0; i < size.height; i++ )90{91const uchar* s = src + i*srcstep;92unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;9394for( j = 0; j < BORDER; j++ )95tmp[-j-1] = tmp[size.width + j] = INIT_DIST0;9697for( j = 0; j < size.width; j++ )98{99if( !s[j] )100tmp[j] = 0;101else102{103unsigned int t0 = tmp[j-step-1] + DIAG_DIST;104unsigned int t = tmp[j-step] + HV_DIST;105if( t0 > t ) t0 = t;106t = tmp[j-step+1] + DIAG_DIST;107if( t0 > t ) t0 = t;108t = tmp[j-1] + HV_DIST;109if( t0 > t ) t0 = t;110tmp[j] = t0;111}112}113}114115// backward pass116for( i = size.height - 1; i >= 0; i-- )117{118float* d = (float*)(dist + i*dststep);119unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;120121for( j = size.width - 1; j >= 0; j-- )122{123unsigned int t0 = tmp[j];124if( t0 > HV_DIST )125{126unsigned int t = tmp[j+step+1] + DIAG_DIST;127if( t0 > t ) t0 = t;128t = tmp[j+step] + HV_DIST;129if( t0 > t ) t0 = t;130t = tmp[j+step-1] + DIAG_DIST;131if( t0 > t ) t0 = t;132t = tmp[j+1] + HV_DIST;133if( t0 > t ) t0 = t;134tmp[j] = t0;135}136t0 = (t0 > DIST_MAX) ? DIST_MAX : t0;137d[j] = (float)(t0 * scale);138}139}140}141142143static void144distanceTransform_5x5( const Mat& _src, Mat& _temp, Mat& _dist, const float* metrics )145{146const int BORDER = 2;147int i, j;148const unsigned int HV_DIST = CV_FLT_TO_FIX( metrics[0], DIST_SHIFT );149const unsigned int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], DIST_SHIFT );150const unsigned int LONG_DIST = CV_FLT_TO_FIX( metrics[2], DIST_SHIFT );151const float scale = 1.f/(1 << DIST_SHIFT);152153const uchar* src = _src.ptr();154int* temp = _temp.ptr<int>();155float* dist = _dist.ptr<float>();156int srcstep = (int)(_src.step/sizeof(src[0]));157int step = (int)(_temp.step/sizeof(temp[0]));158int dststep = (int)(_dist.step/sizeof(dist[0]));159Size size = _src.size();160161initTopBottom( _temp, BORDER );162163// forward pass164for( i = 0; i < size.height; i++ )165{166const uchar* s = src + i*srcstep;167unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;168169for( j = 0; j < BORDER; j++ )170tmp[-j-1] = tmp[size.width + j] = INIT_DIST0;171172for( j = 0; j < size.width; j++ )173{174if( !s[j] )175tmp[j] = 0;176else177{178unsigned int t0 = tmp[j-step*2-1] + LONG_DIST;179unsigned int t = tmp[j-step*2+1] + LONG_DIST;180if( t0 > t ) t0 = t;181t = tmp[j-step-2] + LONG_DIST;182if( t0 > t ) t0 = t;183t = tmp[j-step-1] + DIAG_DIST;184if( t0 > t ) t0 = t;185t = tmp[j-step] + HV_DIST;186if( t0 > t ) t0 = t;187t = tmp[j-step+1] + DIAG_DIST;188if( t0 > t ) t0 = t;189t = tmp[j-step+2] + LONG_DIST;190if( t0 > t ) t0 = t;191t = tmp[j-1] + HV_DIST;192if( t0 > t ) t0 = t;193tmp[j] = t0;194}195}196}197198// backward pass199for( i = size.height - 1; i >= 0; i-- )200{201float* d = (float*)(dist + i*dststep);202unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;203204for( j = size.width - 1; j >= 0; j-- )205{206unsigned int t0 = tmp[j];207if( t0 > HV_DIST )208{209unsigned int t = tmp[j+step*2+1] + LONG_DIST;210if( t0 > t ) t0 = t;211t = tmp[j+step*2-1] + LONG_DIST;212if( t0 > t ) t0 = t;213t = tmp[j+step+2] + LONG_DIST;214if( t0 > t ) t0 = t;215t = tmp[j+step+1] + DIAG_DIST;216if( t0 > t ) t0 = t;217t = tmp[j+step] + HV_DIST;218if( t0 > t ) t0 = t;219t = tmp[j+step-1] + DIAG_DIST;220if( t0 > t ) t0 = t;221t = tmp[j+step-2] + LONG_DIST;222if( t0 > t ) t0 = t;223t = tmp[j+1] + HV_DIST;224if( t0 > t ) t0 = t;225tmp[j] = t0;226}227t0 = (t0 > DIST_MAX) ? DIST_MAX : t0;228d[j] = (float)(t0 * scale);229}230}231}232233234static void235distanceTransformEx_5x5( const Mat& _src, Mat& _temp, Mat& _dist, Mat& _labels, const float* metrics )236{237const int BORDER = 2;238239int i, j;240const unsigned int HV_DIST = CV_FLT_TO_FIX( metrics[0], DIST_SHIFT );241const unsigned int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], DIST_SHIFT );242const unsigned int LONG_DIST = CV_FLT_TO_FIX( metrics[2], DIST_SHIFT );243const float scale = 1.f/(1 << DIST_SHIFT);244245const uchar* src = _src.ptr();246int* temp = _temp.ptr<int>();247float* dist = _dist.ptr<float>();248int* labels = _labels.ptr<int>();249int srcstep = (int)(_src.step/sizeof(src[0]));250int step = (int)(_temp.step/sizeof(temp[0]));251int dststep = (int)(_dist.step/sizeof(dist[0]));252int lstep = (int)(_labels.step/sizeof(labels[0]));253Size size = _src.size();254255initTopBottom( _temp, BORDER );256257// forward pass258for( i = 0; i < size.height; i++ )259{260const uchar* s = src + i*srcstep;261unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;262int* lls = (int*)(labels + i*lstep);263264for( j = 0; j < BORDER; j++ )265tmp[-j-1] = tmp[size.width + j] = INIT_DIST0;266267for( j = 0; j < size.width; j++ )268{269if( !s[j] )270{271tmp[j] = 0;272//assert( lls[j] != 0 );273}274else275{276unsigned int t0 = INIT_DIST0, t;277int l0 = 0;278279t = tmp[j-step*2-1] + LONG_DIST;280if( t0 > t )281{282t0 = t;283l0 = lls[j-lstep*2-1];284}285t = tmp[j-step*2+1] + LONG_DIST;286if( t0 > t )287{288t0 = t;289l0 = lls[j-lstep*2+1];290}291t = tmp[j-step-2] + LONG_DIST;292if( t0 > t )293{294t0 = t;295l0 = lls[j-lstep-2];296}297t = tmp[j-step-1] + DIAG_DIST;298if( t0 > t )299{300t0 = t;301l0 = lls[j-lstep-1];302}303t = tmp[j-step] + HV_DIST;304if( t0 > t )305{306t0 = t;307l0 = lls[j-lstep];308}309t = tmp[j-step+1] + DIAG_DIST;310if( t0 > t )311{312t0 = t;313l0 = lls[j-lstep+1];314}315t = tmp[j-step+2] + LONG_DIST;316if( t0 > t )317{318t0 = t;319l0 = lls[j-lstep+2];320}321t = tmp[j-1] + HV_DIST;322if( t0 > t )323{324t0 = t;325l0 = lls[j-1];326}327328tmp[j] = t0;329lls[j] = l0;330}331}332}333334// backward pass335for( i = size.height - 1; i >= 0; i-- )336{337float* d = (float*)(dist + i*dststep);338unsigned int* tmp = (unsigned int*)(temp + (i+BORDER)*step) + BORDER;339int* lls = (int*)(labels + i*lstep);340341for( j = size.width - 1; j >= 0; j-- )342{343unsigned int t0 = tmp[j];344int l0 = lls[j];345if( t0 > HV_DIST )346{347unsigned int t = tmp[j+step*2+1] + LONG_DIST;348if( t0 > t )349{350t0 = t;351l0 = lls[j+lstep*2+1];352}353t = tmp[j+step*2-1] + LONG_DIST;354if( t0 > t )355{356t0 = t;357l0 = lls[j+lstep*2-1];358}359t = tmp[j+step+2] + LONG_DIST;360if( t0 > t )361{362t0 = t;363l0 = lls[j+lstep+2];364}365t = tmp[j+step+1] + DIAG_DIST;366if( t0 > t )367{368t0 = t;369l0 = lls[j+lstep+1];370}371t = tmp[j+step] + HV_DIST;372if( t0 > t )373{374t0 = t;375l0 = lls[j+lstep];376}377t = tmp[j+step-1] + DIAG_DIST;378if( t0 > t )379{380t0 = t;381l0 = lls[j+lstep-1];382}383t = tmp[j+step-2] + LONG_DIST;384if( t0 > t )385{386t0 = t;387l0 = lls[j+lstep-2];388}389t = tmp[j+1] + HV_DIST;390if( t0 > t )391{392t0 = t;393l0 = lls[j+1];394}395tmp[j] = t0;396lls[j] = l0;397}398t0 = (t0 > DIST_MAX) ? DIST_MAX : t0;399d[j] = (float)(t0 * scale);400}401}402}403404405static void getDistanceTransformMask( int maskType, float *metrics )406{407CV_Assert( metrics != 0 );408409switch (maskType)410{411case 30:412metrics[0] = 1.0f;413metrics[1] = 1.0f;414break;415416case 31:417metrics[0] = 1.0f;418metrics[1] = 2.0f;419break;420421case 32:422metrics[0] = 0.955f;423metrics[1] = 1.3693f;424break;425426case 50:427metrics[0] = 1.0f;428metrics[1] = 1.0f;429metrics[2] = 2.0f;430break;431432case 51:433metrics[0] = 1.0f;434metrics[1] = 2.0f;435metrics[2] = 3.0f;436break;437438case 52:439metrics[0] = 1.0f;440metrics[1] = 1.4f;441metrics[2] = 2.1969f;442break;443default:444CV_Error(CV_StsBadArg, "Unknown metric type");445}446}447448struct DTColumnInvoker : ParallelLoopBody449{450DTColumnInvoker( const Mat* _src, Mat* _dst, const int* _sat_tab, const float* _sqr_tab)451{452src = _src;453dst = _dst;454sat_tab = _sat_tab + src->rows*2 + 1;455sqr_tab = _sqr_tab;456}457458void operator()(const Range& range) const CV_OVERRIDE459{460int i, i1 = range.start, i2 = range.end;461int m = src->rows;462size_t sstep = src->step, dstep = dst->step/sizeof(float);463AutoBuffer<int> _d(m);464int* d = _d.data();465466for( i = i1; i < i2; i++ )467{468const uchar* sptr = src->ptr(m-1) + i;469float* dptr = dst->ptr<float>() + i;470int j, dist = m-1;471472for( j = m-1; j >= 0; j--, sptr -= sstep )473{474dist = (dist + 1) & (sptr[0] == 0 ? 0 : -1);475d[j] = dist;476}477478dist = m-1;479for( j = 0; j < m; j++, dptr += dstep )480{481dist = dist + 1 - sat_tab[dist - d[j]];482d[j] = dist;483dptr[0] = sqr_tab[dist];484}485}486}487488const Mat* src;489Mat* dst;490const int* sat_tab;491const float* sqr_tab;492};493494struct DTRowInvoker : ParallelLoopBody495{496DTRowInvoker( Mat* _dst, const float* _sqr_tab, const float* _inv_tab )497{498dst = _dst;499sqr_tab = _sqr_tab;500inv_tab = _inv_tab;501}502503void operator()(const Range& range) const CV_OVERRIDE504{505const float inf = 1e15f;506int i, i1 = range.start, i2 = range.end;507int n = dst->cols;508AutoBuffer<uchar> _buf((n+2)*2*sizeof(float) + (n+2)*sizeof(int));509float* f = (float*)_buf.data();510float* z = f + n;511int* v = alignPtr((int*)(z + n + 1), sizeof(int));512513for( i = i1; i < i2; i++ )514{515float* d = dst->ptr<float>(i);516int p, q, k;517518v[0] = 0;519z[0] = -inf;520z[1] = inf;521f[0] = d[0];522523for( q = 1, k = 0; q < n; q++ )524{525float fq = d[q];526f[q] = fq;527528for(;;k--)529{530p = v[k];531float s = (fq + sqr_tab[q] - d[p] - sqr_tab[p])*inv_tab[q - p];532if( s > z[k] )533{534k++;535v[k] = q;536z[k] = s;537z[k+1] = inf;538break;539}540}541}542543for( q = 0, k = 0; q < n; q++ )544{545while( z[k+1] < q )546k++;547p = v[k];548d[q] = std::sqrt(sqr_tab[std::abs(q - p)] + f[p]);549}550}551}552553Mat* dst;554const float* sqr_tab;555const float* inv_tab;556};557558static void559trueDistTrans( const Mat& src, Mat& dst )560{561const float inf = 1e15f;562563CV_Assert( src.size() == dst.size() );564565CV_Assert( src.type() == CV_8UC1 && dst.type() == CV_32FC1 );566int i, m = src.rows, n = src.cols;567568cv::AutoBuffer<uchar> _buf(std::max(m*2*sizeof(float) + (m*3+1)*sizeof(int), n*2*sizeof(float)));569// stage 1: compute 1d distance transform of each column570float* sqr_tab = (float*)_buf.data();571int* sat_tab = cv::alignPtr((int*)(sqr_tab + m*2), sizeof(int));572int shift = m*2;573574for( i = 0; i < m; i++ )575sqr_tab[i] = (float)(i*i);576for( i = m; i < m*2; i++ )577sqr_tab[i] = inf;578for( i = 0; i < shift; i++ )579sat_tab[i] = 0;580for( ; i <= m*3; i++ )581sat_tab[i] = i - shift;582583cv::parallel_for_(cv::Range(0, n), cv::DTColumnInvoker(&src, &dst, sat_tab, sqr_tab), src.total()/(double)(1<<16));584585// stage 2: compute modified distance transform for each row586float* inv_tab = sqr_tab + n;587588inv_tab[0] = sqr_tab[0] = 0.f;589for( i = 1; i < n; i++ )590{591inv_tab[i] = (float)(0.5/i);592sqr_tab[i] = (float)(i*i);593}594595cv::parallel_for_(cv::Range(0, m), cv::DTRowInvoker(&dst, sqr_tab, inv_tab));596}597598599/****************************************************************************************\600Non-inplace and Inplace 8u->8u Distance Transform for CityBlock (a.k.a. L1) metric601(C) 2006 by Jay Stavinzky.602\****************************************************************************************/603604//BEGIN ATS ADDITION605// 8-bit grayscale distance transform function606static void607distanceATS_L1_8u( const Mat& src, Mat& dst )608{609int width = src.cols, height = src.rows;610611int a;612uchar lut[256];613int x, y;614615const uchar *sbase = src.ptr();616uchar *dbase = dst.ptr();617int srcstep = (int)src.step;618int dststep = (int)dst.step;619620CV_Assert( src.type() == CV_8UC1 && dst.type() == CV_8UC1 );621CV_Assert( src.size() == dst.size() );622623////////////////////// forward scan ////////////////////////624for( x = 0; x < 256; x++ )625lut[x] = cv::saturate_cast<uchar>(x+1);626627//init first pixel to max (we're going to be skipping it)628dbase[0] = (uchar)(sbase[0] == 0 ? 0 : 255);629630//first row (scan west only, skip first pixel)631for( x = 1; x < width; x++ )632dbase[x] = (uchar)(sbase[x] == 0 ? 0 : lut[dbase[x-1]]);633634for( y = 1; y < height; y++ )635{636sbase += srcstep;637dbase += dststep;638639//for left edge, scan north only640a = sbase[0] == 0 ? 0 : lut[dbase[-dststep]];641dbase[0] = (uchar)a;642643for( x = 1; x < width; x++ )644{645a = sbase[x] == 0 ? 0 : lut[MIN(a, dbase[x - dststep])];646dbase[x] = (uchar)a;647}648}649650////////////////////// backward scan ///////////////////////651652a = dbase[width-1];653654// do last row east pixel scan here (skip bottom right pixel)655for( x = width - 2; x >= 0; x-- )656{657a = lut[a];658dbase[x] = (uchar)(CV_CALC_MIN_8U(a, dbase[x]));659}660661// right edge is the only error case662for( y = height - 2; y >= 0; y-- )663{664dbase -= dststep;665666// do right edge667a = lut[dbase[width-1+dststep]];668a = dbase[width-1] = (uchar)(MIN(a, dbase[width-1]));669670for( x = width - 2; x >= 0; x-- )671{672int b = dbase[x+dststep];673a = lut[MIN(a, b)];674a = MIN(a, dbase[x]);675dbase[x] = (uchar)(a);676}677}678}679//END ATS ADDITION680681}682683namespace cv684{685static void distanceTransform_L1_8U(InputArray _src, OutputArray _dst)686{687CV_INSTRUMENT_REGION();688689Mat src = _src.getMat();690691CV_Assert( src.type() == CV_8UC1);692693_dst.create( src.size(), CV_8UC1);694Mat dst = _dst.getMat();695696#ifdef HAVE_IPP697CV_IPP_CHECK()698{699IppiSize roi = { src.cols, src.rows };700Ipp32s pMetrics[2] = { 1, 2 }; //L1, 3x3 mask701if (CV_INSTRUMENT_FUN_IPP(ippiDistanceTransform_3x3_8u_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<uchar>(), (int)dst.step, roi, pMetrics) >= 0)702{703CV_IMPL_ADD(CV_IMPL_IPP);704return;705}706setIppErrorStatus();707}708#endif709710distanceATS_L1_8u(src, dst);711}712}713714// Wrapper function for distance transform group715void cv::distanceTransform( InputArray _src, OutputArray _dst, OutputArray _labels,716int distType, int maskSize, int labelType )717{718CV_INSTRUMENT_REGION();719720Mat src = _src.getMat(), labels;721bool need_labels = _labels.needed();722723CV_Assert( src.type() == CV_8UC1);724725_dst.create( src.size(), CV_32F);726Mat dst = _dst.getMat();727728if( need_labels )729{730CV_Assert( labelType == DIST_LABEL_PIXEL || labelType == DIST_LABEL_CCOMP );731732_labels.create(src.size(), CV_32S);733labels = _labels.getMat();734maskSize = CV_DIST_MASK_5;735}736737float _mask[5] = {0};738739if( maskSize != CV_DIST_MASK_3 && maskSize != CV_DIST_MASK_5 && maskSize != CV_DIST_MASK_PRECISE )740CV_Error( CV_StsBadSize, "Mask size should be 3 or 5 or 0 (precise)" );741742if( distType == CV_DIST_C || distType == CV_DIST_L1 )743maskSize = !need_labels ? CV_DIST_MASK_3 : CV_DIST_MASK_5;744else if( distType == CV_DIST_L2 && need_labels )745maskSize = CV_DIST_MASK_5;746747if( maskSize == CV_DIST_MASK_PRECISE )748{749750#ifdef HAVE_IPP751CV_IPP_CHECK()752{753#if IPP_DISABLE_PERF_TRUE_DIST_MT754if(cv::getNumThreads()<=1 || (src.total()<(int)(1<<14)))755#endif756{757IppStatus status;758IppiSize roi = { src.cols, src.rows };759Ipp8u *pBuffer;760int bufSize=0;761762status = ippiTrueDistanceTransformGetBufferSize_8u32f_C1R(roi, &bufSize);763if (status>=0)764{765pBuffer = (Ipp8u *)CV_IPP_MALLOC( bufSize );766status = CV_INSTRUMENT_FUN_IPP(ippiTrueDistanceTransform_8u32f_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<float>(), (int)dst.step, roi, pBuffer);767ippFree( pBuffer );768if (status>=0)769{770CV_IMPL_ADD(CV_IMPL_IPP);771return;772}773setIppErrorStatus();774}775}776}777#endif778779trueDistTrans( src, dst );780return;781}782783CV_Assert( distType == CV_DIST_C || distType == CV_DIST_L1 || distType == CV_DIST_L2 );784785getDistanceTransformMask( (distType == CV_DIST_C ? 0 :786distType == CV_DIST_L1 ? 1 : 2) + maskSize*10, _mask );787788Size size = src.size();789790int border = maskSize == CV_DIST_MASK_3 ? 1 : 2;791Mat temp( size.height + border*2, size.width + border*2, CV_32SC1 );792793if( !need_labels )794{795if( maskSize == CV_DIST_MASK_3 )796{797#if defined (HAVE_IPP) && (IPP_VERSION_X100 >= 700)798CV_IPP_CHECK()799{800IppiSize roi = { src.cols, src.rows };801if (CV_INSTRUMENT_FUN_IPP(ippiDistanceTransform_3x3_8u32f_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<float>(), (int)dst.step, roi, _mask) >= 0)802{803CV_IMPL_ADD(CV_IMPL_IPP);804return;805}806setIppErrorStatus();807}808#endif809810distanceTransform_3x3(src, temp, dst, _mask);811}812else813{814#if defined (HAVE_IPP) && (IPP_VERSION_X100 >= 700)815CV_IPP_CHECK()816{817IppiSize roi = { src.cols, src.rows };818if (CV_INSTRUMENT_FUN_IPP(ippiDistanceTransform_5x5_8u32f_C1R, src.ptr<uchar>(), (int)src.step, dst.ptr<float>(), (int)dst.step, roi, _mask) >= 0)819{820CV_IMPL_ADD(CV_IMPL_IPP);821return;822}823setIppErrorStatus();824}825#endif826827distanceTransform_5x5(src, temp, dst, _mask);828}829}830else831{832labels.setTo(Scalar::all(0));833834if( labelType == CV_DIST_LABEL_CCOMP )835{836Mat zpix = src == 0;837connectedComponents(zpix, labels, 8, CV_32S, CCL_WU);838}839else840{841int k = 1;842for( int i = 0; i < src.rows; i++ )843{844const uchar* srcptr = src.ptr(i);845int* labelptr = labels.ptr<int>(i);846847for( int j = 0; j < src.cols; j++ )848if( srcptr[j] == 0 )849labelptr[j] = k++;850}851}852853distanceTransformEx_5x5( src, temp, dst, labels, _mask );854}855}856857void cv::distanceTransform( InputArray _src, OutputArray _dst,858int distanceType, int maskSize, int dstType)859{860CV_INSTRUMENT_REGION();861862if (distanceType == CV_DIST_L1 && dstType==CV_8U)863distanceTransform_L1_8U(_src, _dst);864else865distanceTransform(_src, _dst, noArray(), distanceType, maskSize, DIST_LABEL_PIXEL);866867}868869CV_IMPL void870cvDistTransform( const void* srcarr, void* dstarr,871int distType, int maskSize,872const float * /*mask*/,873void* labelsarr, int labelType )874{875cv::Mat src = cv::cvarrToMat(srcarr);876const cv::Mat dst = cv::cvarrToMat(dstarr);877const cv::Mat labels = cv::cvarrToMat(labelsarr);878879cv::distanceTransform(src, dst, labelsarr ? cv::_OutputArray(labels) : cv::_OutputArray(),880distType, maskSize, labelType);881882}883884885/* End of file. */886887888