DGtal  1.5.beta
distancetransform2D.cpp
Go to the documentation of this file.
1 
45 #include <iostream>
46 #include <iomanip>
47 #include "DGtal/base/Common.h"
48 #include "DGtal/helpers/StdDefs.h"
49 #include "DGtal/io/colormaps/GrayscaleColorMap.h"
50 #include "DGtal/io/colormaps/HueShadeColorMap.h"
51 #include "DGtal/io/colormaps/TickedColorMap.h"
52 #include "DGtal/io/colormaps/GradientColorMap.h"
53 #include "DGtal/io/boards/Board2D.h"
54 #include "DGtal/images/ImageSelector.h"
55 #include "DGtal/images/SimpleThresholdForegroundPredicate.h"
56 #include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
58 
59 using namespace std;
60 using namespace DGtal;
61 
63 
71 template<typename Image>
72 void randomSeeds(Image &image, const unsigned int nb, const int value)
73 {
74  typename Image::Point p, low = image.domain().lowerBound();
75  typename Image::Vector ext;
76 
77  ext = image.extent();
78 
79  for (unsigned int k = 0 ; k < nb; k++)
80  {
81  for (unsigned int dim = 0; dim < Image::dimension; dim++)
82  p[dim] = rand() % (ext[dim]) + low[dim];
83 
84  image.setValue(p, value);
85  }
86 }
87 
88 int main()
89 {
90  trace.beginBlock ( "Example distancetransform2D" );
91 
93  Z2i::Point a ( 0, 0 );
94  Z2i::Point b ( 127, 127);
95 
96  //Input image with unsigned char values
98  Image image ( Z2i::Domain(a, b ));
99 
100  //We fill the image with the 128 value
101  for ( Image::Iterator it = image.begin(), itend = image.end();it != itend; ++it)
102  (*it)=128;
103  //We generate 16 seeds with 0 values.
104  randomSeeds(image,16,0);
106 
107  //Input shape output
108  typedef GrayscaleColorMap<Image::Value> Gray;
109  Board2D board;
111  Display2DFactory::drawImage<Gray>(board, image, (unsigned int)0, (unsigned int)129);
112  board.saveSVG("inputShape.svg");
113 
115  //Point Predicate from random seed image
117  PointPredicate predicate(image,0);
119 
123 
124 
125  DTL2 dtL2(image.domain(), predicate, Z2i::l2Metric);
126  DTL1 dtL1(image.domain(), predicate, Z2i::l1Metric);
128 
129 
130  DTL2::Value maxv2=0;
131  //We compute the maximum DT value on the L2 map
132  for ( DTL2::ConstRange::ConstIterator it = dtL2.constRange().begin(), itend = dtL2.constRange().end();it != itend; ++it)
133  if ( (*it) > maxv2) maxv2 = (*it);
134 
135  DTL1::Value maxv1=0;
136  //We compute the maximum DT value on the L1 map
137  for ( DTL1::ConstRange::ConstIterator it = dtL1.constRange().begin(), itend = dtL1.constRange().end();it != itend; ++it)
138  if ( (*it) > maxv1) maxv1 = (*it);
139 
141  //Colormap used for the SVG output
142  typedef HueShadeColorMap<DTL2::Value, 2> HueTwice;
144 
145 
146 
147 
148  trace.warning() << dtL2 << " maxValue= "<<maxv2<< endl;
149  board.clear();
150  Display2DFactory::drawImage<HueTwice>(board, dtL2, 0.0, maxv2 + 1);
151  board.saveSVG ( "example-DT-L2.svg" );
152 
153  trace.warning() << dtL1 << " maxValue= "<<maxv1<< endl;
154  board.clear();
155  Display2DFactory::drawImage<HueTwice>(board, dtL1, 0.0, maxv1 + 1);
156  board.saveSVG ( "example-DT-L1.svg" );
157 
158  //Explicit export with ticked colormap
159  //We compute the maximum DT value on the L2 map
160  board.clear();
161  TickedColorMap<double, GradientColorMap<double> > ticked(0.0,maxv2, Color::White);
162  ticked.addRegularTicks(5, 0.5);
163  ticked.finalize();
164  ticked.colormap()->addColor( Color::Red );
165  ticked.colormap()->addColor( Color::Black );
166  for ( DTL2::Domain::ConstIterator it = dtL2.domain().begin(), itend = dtL2.domain().end();it != itend; ++it)
167  {
168  board<< CustomStyle((*it).className(),new CustomColors(ticked(dtL2(*it)),ticked(dtL2(*it))));
169  board << *it;
170  }
171  board.saveSVG("example-DT-L2-ticked.svg");
172 
173  trace.endBlock();
174  return 0;
175 }
176 // //
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Aim: Implementation of the linear in time distance transformation for separable metrics.
Aim: This class template may be used to (linearly) convert scalar values in a given range into gray l...
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:70
Aim: This class adapts any colormap to add "ticks" in the colormap colors.
ColorMap * colormap() const
void addRegularTicks(const unsigned int nbTicks, const Value thickness)
void beginBlock(const std::string &keyword="")
std::ostream & warning()
double endBlock()
Aim: Define a simple Foreground predicate thresholding image values given a single thresold....
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:151
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1011
void setUnit(Unit unit)
Definition: Board.cpp:239
void randomSeeds(Image &image, const unsigned int nb, const int value)
int main()
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:279
Image image(domain)
ImageContainerBySTLVector< Domain, Value > Image
unsigned int dim(const Vector &z)