33 #include "DGtal/base/Common.h" 
   35 #include "DGtal/kernel/SpaceND.h" 
   36 #include "DGtal/kernel/domains/HyperRectDomain.h" 
   37 #include "DGtal/images/ImageSelector.h" 
   38 #include "DGtal/geometry/volumes/distance/DistanceTransformation.h" 
   39 #include "DGtal/io/colormaps/HueShadeColorMap.h" 
   40 #include "DGtal/io/colormaps/GrayscaleColorMap.h" 
   41 #include "DGtal/shapes/Shapes.h" 
   42 #include "DGtal/helpers/StdDefs.h" 
   43 #include "DGtal/shapes/ShapeFactory.h" 
   44 #include "DGtal/io/boards/Board2D.h" 
   45 #include "DGtal/kernel/sets/SetPredicate.h" 
   46 #include "DGtal/images/imagesSetsUtils/SimpleThresholdForegroundPredicate.h" 
   50 using namespace DGtal;
 
   56 template<
typename Image>
 
   57 void randomSeeds(
Image &input, 
const unsigned int nb, 
const int value)
 
   62   for (
Dimension i = 0; i < Image::Domain::dimension; i++)
 
   63       ext[i] = up[i] - low[i] + 1;
 
   66   for (
unsigned int k = 0 ; k < nb; k++)
 
   68       for (
unsigned int dim = 0; dim < Image::dimension; dim++)
 
   70           p[dim] = rand() % (ext[dim]) +  low[dim];
 
   82 bool testDistanceTransformation()
 
   84   unsigned int nbok = 0;
 
   90   typedef TSpace::Point 
Point;
 
   97   Image image ( Domain(a, b ));
 
   99   for ( 
unsigned k = 0; k < 49; k++ )
 
  101       a[0] = ( k / 7 ) + 5;
 
  102       a[1] = ( k % 7 ) + 5;
 
  103       image.setValue ( a, 128 );
 
  108   Predicate aPredicate(image,0);
 
  113   dt.checkTypesValidity ( );
 
  117   Display2DFactory::drawImage<Gray>(board, image, (
unsigned int)0, (
unsigned int)255);
 
  118   board.saveSVG ( 
"image-preDT.svg" );
 
  120   std::copy ( image.begin(),
 
  122               std::ostream_iterator<unsigned int> ( std::cout, 
" " ) );
 
  126   ImageLong result = dt.compute (  );
 
  130   ImageLong::ConstIterator it = result.begin();
 
  131   ImageLong::ConstIterator itend = result.end();
 
  132   for (; it != itend; ++it)
 
  134       std::cout << (*it) << 
" ";
 
  136   std::cout << std::endl;
 
  140   board.saveSVG ( 
"image-postDT.svg" );
 
  153 bool testDistanceTransformationNeg()
 
  155   unsigned int nbok = 0;
 
  161   typedef TSpace::Point Point;
 
  165   Point a ( -10, -10 );
 
  168   Image image ( Domain( a, b ));
 
  170   for(
int y=-10; y<=10;y++) 
 
  171     for(
int x=-10; x<=10;x++)
 
  173         if ((
abs(x)<7) && (
abs(y)<5))
 
  174           image.setValue(Point(x,y),1);
 
  176           image.setValue(Point(x,y),0);
 
  180   Predicate aPredicate(image,0);
 
  186   dt.checkTypesValidity (  );
 
  190   Display2DFactory::drawImage<Gray>(board, image, (
unsigned int)0, (
unsigned int)1);
 
  191   board.saveSVG ( 
"image-preDT-neg.svg" );
 
  194   for(
int y=-10; y<=10;y++) 
 
  196       for(
int x=-10; x<=10;x++)
 
  198           std::cout<<image(Point(x,y))<<
"  ";
 
  200       std::cout<<std::endl;
 
  204   ImageLong result = dt.compute (  );
 
  207   for(ImageLong::Iterator it = result.begin(), itend = result.end();
 
  212   for(
int y=-10; y<=10;y++) 
 
  214       for(
int x=-10; x<=10;x++)
 
  216           std::cout<<result(Point(x,y))<<
"  ";
 
  218       std::cout<<std::endl;
 
  227   board.saveSVG ( 
"image-postDT-neg.svg" );
 
  240   unsigned int nbok = 0;
 
  246   typedef TSpace::Point Point;
 
  255   Z2i::Domain domain(flower.getLowerBound(), flower.getUpperBound());
 
  267   DistanceTransformation<TSpace, SetPredicate<Z2i::DigitalSet>, 1> dt1(domain,aPredicate);
 
  268   typedef DistanceTransformation<TSpace, SetPredicate<Z2i::DigitalSet>,1>::OutputImage ImageLong1;
 
  271   ImageLong result = dt.compute (  );
 
  272   ImageLong0 result0 = dt0.compute (  );
 
  273   ImageLong1 result1 = dt1.compute (  );
 
  278   for ( ImageLong::Iterator it = result.begin(), itend = result.end();
 
  284   board.saveSVG ( 
"image-DTSet.svg" );
 
  288   for ( ImageLong::Iterator it = result0.begin(), itend = result0.end();
 
  294   board.saveSVG ( 
"image-DTSet-linfty.svg" );
 
  298   for ( ImageLong::Iterator it = result1.begin(), itend = result1.end();
 
  304   board.saveSVG ( 
"image-DTSet-l1.svg" );
 
  314 bool testDistanceTransformationBorder()
 
  316   unsigned int nbok = 0;
 
  319   trace.
beginBlock ( 
"Testing DT computation with Infinity values at the first step" );
 
  322   typedef TSpace::Point Point;
 
  328   Point b ( 128, 128 );
 
  331   Image image ( Domain(a, b ));
 
  333   for ( 
Image::Iterator it = image.begin(), itend = image.end();it != itend; ++it)
 
  337   randomSeeds(image, 19, 0);
 
  340   Predicate aPredicate(image,0);
 
  345   dt.checkTypesValidity (  );
 
  349   Display2DFactory::drawImage<Hue>(board, image, (
unsigned int)0, (
unsigned int)150);
 
  350   board.saveSVG ( 
"image-preDT-border.svg" );
 
  353   ImageLong result = dt.compute (  );
 
  356   for ( ImageLong::Iterator it = result.begin(), itend = result.end();it != itend; ++it)
 
  360   ImageLong::ConstIterator it = result.begin();
 
  361   for (
unsigned int y = 0; y < 33; y++)
 
  363       for (
unsigned int x = 0; x < 33; x++)
 
  365           std::cout << std::setw(4) << (*it) << 
" ";
 
  368       std::cout << std::endl;
 
  376   board.saveSVG ( 
"image-postDT-border.svg" );
 
  391 bool testDistanceTransformation3D()
 
  393   unsigned int nbok = 0;
 
  399   typedef TSpace::Point Point;
 
  404   Point b ( 15, 15, 15 );
 
  406   Image image ( Domain(a, b ));
 
  410   for (Domain::ConstIterator it = dom.begin(),
 
  411          itend = dom.end(); it != itend; ++it)
 
  413       if ( ((*it) - c).norm() < 7)
 
  414         image.setValue ( *it, 128 );
 
  418   Predicate aPredicate(image,0);
 
  424   dt.checkTypesValidity (  );
 
  426   ImageLong result = dt.compute (  );
 
  429   for (
unsigned int y = 0; y < 16; y++)
 
  431       for (
unsigned int x = 0; x < 16; x++)
 
  434           std::cout << result(p) << 
"   ";
 
  436       std::cout << std::endl;
 
  451 bool testTypeValidity()
 
  453   unsigned int nbok = 0;
 
  459   typedef TSpace::Point Point;
 
  465   Image image ( Domain(a, b ));
 
  468   Predicate aPredicate(image,0);
 
  475   dt.checkTypesValidity (  );
 
  480   dt34.checkTypesValidity (  );
 
  487 bool testChessboard()
 
  489   unsigned int nbok = 0;
 
  492   trace.
beginBlock ( 
"Testing DT computation with Infinity values at the first step" );
 
  495   typedef TSpace::Point Point;
 
  501   Point b ( 128, 128 );
 
  504   Image image ( Domain( a, b ));
 
  506   for ( 
Image::Iterator it = image.begin(), itend = image.end();it != itend; ++it)
 
  510   randomSeeds(image, 19, 0);
 
  515   Predicate aPredicate(image,0);
 
  520   DT2 dt2(Domain(a,b), aPredicate);
 
  524   DT dt(Domain(a,b), aPredicate);;
 
  528   DT1 dt1(Domain(a,b), aPredicate);;
 
  530   DT::OutputImage result = dt.compute (  );
 
  531   DT1::OutputImage result1 = dt1.compute (  );
 
  532   DT2::OutputImage result2 = dt2.compute ();
 
  535   for ( DT::OutputImage::Iterator it = result.begin(), itend = result.end();it != itend; ++it)
 
  543   for (
unsigned int y = 0; y < 16; y++)
 
  545       for (
unsigned int x = 0; x < 16; x++)
 
  548           std::cout << std::setw(4) << result(p) << 
" ";
 
  550       std::cout << std::endl;
 
  558   board.saveSVG ( 
"image-DT-linfty.svg" );
 
  565   for ( DT1::OutputImage::Iterator it2 = result1.begin(), 
 
  566           itend = result1.end();
 
  573   trace.
info()<< 
"Exporting to SVG L1"<<endl;
 
  576   board.saveSVG ( 
"image-DT-l1.svg" );
 
  581   for ( DT2::OutputImage::Iterator it = result2.begin(), itend = result2.end();
 
  588   trace.
info()<< 
"Exporting to SVG L2"<<endl;
 
  591   board.saveSVG ( 
"image-DT-l2.svg" );
 
  603 int main ( 
int argc, 
char** argv )
 
  607   for ( 
int i = 0; i < argc; ++i )
 
  611   bool res =  testTypeValidity() && testDistanceTransformation() && testDistanceTransformationNeg() 
 
  613     && testDistanceTransformationBorder() 
 
  614     && testDistanceTransformation3D()
 
  618   trace.
emphase() << ( res ? 
"Passed." : 
"Error." ) << endl;