DGtal  0.9.3beta
distancetransform2D.cpp
1 
34 #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/colormaps/TickedColorMap.h"
42 #include "DGtal/io/colormaps/GradientColorMap.h"
43 #include "DGtal/io/boards/Board2D.h"
44 #include "DGtal/images/ImageSelector.h"
45 #include "DGtal/images/SimpleThresholdForegroundPredicate.h"
46 #include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
48 
49 using namespace std;
50 using namespace DGtal;
51 
53 
61 template<typename Image>
62 void randomSeeds(Image &image, const unsigned int nb, const int value)
63 {
64  typename Image::Point p, low = image.domain().lowerBound();
65  typename Image::Vector ext;
66 
67  ext = image.extent();
68 
69  for (unsigned int k = 0 ; k < nb; k++)
70  {
71  for (unsigned int dim = 0; dim < Image::dimension; dim++)
72  p[dim] = rand() % (ext[dim]) + low[dim];
73 
74  image.setValue(p, value);
75  }
76 }
77 
78 int main()
79 {
80  trace.beginBlock ( "Example distancetransform2D" );
81 
83  Z2i::Point a ( 0, 0 );
84  Z2i::Point b ( 127, 127);
85 
86  //Input image with unsigned char values
88  Image image ( Z2i::Domain(a, b ));
89 
90  //We fill the image with the 128 value
91  for ( Image::Iterator it = image.begin(), itend = image.end();it != itend; ++it)
92  (*it)=128;
93  //We generate 16 seeds with 0 values.
94  randomSeeds(image,16,0);
96 
97  //Input shape output
99  Board2D board;
101  Display2DFactory::drawImage<Gray>(board, image, (unsigned int)0, (unsigned int)129);
102  board.saveSVG("inputShape.svg");
103 
105  //Point Predicate from random seed image
107  PointPredicate predicate(image,0);
109 
113 
114 
115  DTL2 dtL2(image.domain(), predicate, Z2i::l2Metric);
116  DTL1 dtL1(image.domain(), predicate, Z2i::l1Metric);
118 
119 
120  DTL2::Value maxv2=0;
121  //We compute the maximum DT value on the L2 map
122  for ( DTL2::ConstRange::ConstIterator it = dtL2.constRange().begin(), itend = dtL2.constRange().end();it != itend; ++it)
123  if ( (*it) > maxv2) maxv2 = (*it);
124 
125  DTL1::Value maxv1=0;
126  //We compute the maximum DT value on the L1 map
127  for ( DTL1::ConstRange::ConstIterator it = dtL1.constRange().begin(), itend = dtL1.constRange().end();it != itend; ++it)
128  if ( (*it) > maxv1) maxv1 = (*it);
129 
131  //Colormap used for the SVG output
132  typedef HueShadeColorMap<DTL2::Value, 2> HueTwice;
134 
135 
136 
137 
138  trace.warning() << dtL2 << " maxValue= "<<maxv2<< endl;
139  board.clear();
140  Display2DFactory::drawImage<HueTwice>(board, dtL2, 0.0, maxv2 + 1);
141  board.saveSVG ( "example-DT-L2.svg" );
142 
143  trace.warning() << dtL1 << " maxValue= "<<maxv1<< endl;
144  board.clear();
145  Display2DFactory::drawImage<HueTwice>(board, dtL1, 0.0, maxv1 + 1);
146  board.saveSVG ( "example-DT-L1.svg" );
147 
148  //Explicit export with ticked colormap
149  //We compute the maximum DT value on the L2 map
150  board.clear();
151  TickedColorMap<double, GradientColorMap<double> > ticked(0.0,maxv2, Color::White);
152  ticked.addRegularTicks(5, 0.5);
153  ticked.finalize();
154  ticked.colormap()->addColor( Color::Red );
155  ticked.colormap()->addColor( Color::Black );
156  for ( DTL2::Domain::ConstIterator it = dtL2.domain().begin(), itend = dtL2.domain().end();it != itend; ++it)
157  {
158  board<< CustomStyle((*it).className(),new CustomColors(ticked(dtL2(*it)),ticked(dtL2(*it))));
159  board << *it;
160  }
161  board.saveSVG("example-DT-L2-ticked.svg");
162 
163  trace.endBlock();
164  return 0;
165 }
166 // //
void beginBlock(const std::string &keyword="")
Aim: Implementation of the linear in time distance transformation for separable metrics.
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
Trace trace
Definition: Common.h:137
STL namespace.
double endBlock()
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:278
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
void setValue(const Point &aPoint, const Value &aValue)
Definition: Image.h:247
Aim: This class template may be used to (linearly) convert scalar values in a given range into gray l...
Aim: Define a simple Foreground predicate thresholding image values given a single thresold...
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & warning()
const Domain & domain() const
Definition: Image.h:192
void setUnit(Unit unit)
Definition: Board.cpp:240
Aim: This class adapts any colormap to add "ticks" in the colormap colors.
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70