DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
distancetransform2D.cpp
1 
34 
35 #include <iostream>
36 #include <iomanip>
37 #include "DGtal/base/Common.h"
38 #include "DGtal/helpers/StdDefs.h"
39 #include "DGtal/io/colormaps/GrayscaleColorMap.h"
40 #include "DGtal/io/colormaps/HueShadeColorMap.h"
41 #include "DGtal/io/boards/Board2D.h"
42 #include "DGtal/images/ImageSelector.h"
43 #include "DGtal/images/imagesSetsUtils/SimpleThresholdForegroundPredicate.h"
44 #include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
46 
47 using namespace std;
48 using namespace DGtal;
49 
51 
59 template<typename Image>
60 void randomSeeds(Image &image, const unsigned int nb, const int value)
61 {
62  typename Image::Point p, low = image.domain().lowerBound();
63  typename Image::Vector ext;
64 
65  ext = image.extent();
66 
67  for (unsigned int k = 0 ; k < nb; k++)
68  {
69  for (unsigned int dim = 0; dim < Image::dimension; dim++)
70  p[dim] = rand() % (ext[dim]) + low[dim];
71 
72  image.setValue(p, value);
73  }
74 }
75 
76 int main()
77 {
78  trace.beginBlock ( "Example distancetransform2D" );
79 
81  Z2i::Point a ( 0, 0 );
82  Z2i::Point b ( 127, 127);
83 
84  //Input image with unsigned char values
86  Image image ( Z2i::Domain(a, b ));
87 
88  //We fill the image with the 128 value
89  for ( Image::Iterator it = image.begin(), itend = image.end();it != itend; ++it)
90  (*it)=128;
91  //We generate 16 seeds with 0 values.
92  randomSeeds(image,16,0);
94 
96  //Colormap used for the SVG output
97  typedef HueShadeColorMap<long int, 2> HueTwice;
100 
101 
102  //Input shape output
103  Board2D board;
105  Display2DFactory::drawImage<Gray>(board, image, (unsigned int)0, (unsigned int)129);
106  board.saveSVG("inputShape.svg");
107 
109  //Point Predicate from random seed image
110  typedef SimpleThresholdForegroundPredicate<Image> PointPredicate;
111  PointPredicate predicate(image,0);
113 
118 
119 
120  DTL2 dtL2( image.domain(), predicate );
121  DTLInf dtLinf(image.domain(), predicate );
122  DTL1 dtL1(image.domain(), predicate );
123 
124  DTL2::OutputImage resultL2 = dtL2.compute ( );
125  DTLInf::OutputImage resultLinf = dtLinf.compute ( );
126  DTL1::OutputImage resultL1 = dtL1.compute ( );
128 
129 
130  DGtal::int64_t maxv=0;
131  //We compute the maximum DT value on the Linf map
132  for ( DTLInf::OutputImage::ConstIterator it = resultLinf.begin(), itend = resultLinf.end();it != itend; ++it)
133  if ( (*it) > maxv) maxv = (DGtal::int64_t)(*it);
134 
135  DGtal::int64_t maxv2=0;
136  //We compute the maximum DT value on the L2 map
137  for ( DTL2::OutputImage::ConstIterator it = resultL2.begin(), itend = resultL2.end();it != itend; ++it)
138  if ( (*it) > maxv2) maxv2 = (DGtal::int64_t)(*it);
139  DGtal::int64_t maxv1=0;
140 
141  //We compute the maximum DT value on the L1 map
142  for ( DTL1::OutputImage::ConstIterator it = resultL1.begin(), itend = resultL1.end();it != itend; ++it)
143  if ( (*it) > maxv1) maxv1 = (DGtal::int64_t)(*it);
144 
145 
146  trace.warning() << resultL2 << " maxValue= "<<maxv2<< endl;
147  board.clear();
148  Display2DFactory::drawImage<HueTwice>(board, resultL2, (DGtal::int64_t)0, maxv2 + 1);
149  board.saveSVG ( "example-DT-L2.svg" );
150 
151  trace.warning() << resultL1 << " maxValue= "<<maxv1<< endl;
152  board.clear();
153  Display2DFactory::drawImage<HueTwice>(board, resultL1, (DGtal::int64_t)0, maxv1 + 1);
154  board.saveSVG ( "example-DT-L1.svg" );
155 
156  trace.warning() << resultLinf << " maxValue= "<<maxv<< endl;
157  board.clear();
158  Display2DFactory::drawImage<HueTwice>(board, resultLinf, (DGtal::int64_t)0, maxv + 1);
159  board.saveSVG ( "example-DT-Linf.svg" );
160 
161 
162  //We compute the maximum DT value on the L2 map
163  for ( unsigned int j=0;j<33;j++)
164  {
165  for(unsigned int i=0; i<33; i++)
166  trace.info()<< resultL2(Z2i::Point(i,j)) << " ";
167  trace.info()<<std::endl;
168  }
169 
170  trace.endBlock();
171  return 0;
172 }
173 // //