DGtal  0.9.3beta
toricdomainvolumetric.cpp
1 
32 #include <iostream>
34 #include <iomanip>
35 #include "DGtal/base/Common.h"
36 #include "DGtal/helpers/StdDefs.h"
37 #include "DGtal/io/colormaps/GrayscaleColorMap.h"
38 #include "DGtal/io/colormaps/HueShadeColorMap.h"
39 #include "DGtal/io/colormaps/TickedColorMap.h"
40 #include "DGtal/io/colormaps/GradientColorMap.h"
41 #include "DGtal/io/boards/Board2D.h"
42 #include "DGtal/images/ImageSelector.h"
43 #include "DGtal/images/SimpleThresholdForegroundPredicate.h"
44 #include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
46 
47 
49 int main()
50 {
51 
53  using namespace std;
54  using namespace DGtal;
55  using namespace Z2i;
56 
57  Point a ( 0, 0 );
58  Point b ( 32, 16);
59 
60  //Input image with unsigned char values
62  Image image ( Domain(a, b ));
63 
64  //We fill the image with the 128 value
65  for ( Image::Iterator it = image.begin(), itend = image.end();it != itend; ++it)
66  (*it)=128;
67 
68  //We add 3 seeds with 0 values.
69  image.setValue(Point(16,2), 0);
70  image.setValue(Point(2,11), 0);
71  image.setValue(Point(30,15), 0);
73 
74  trace.beginBlock ( "Example toricdomainvolumetric" );
75  //Input shape output
77  Board2D board;
79  Display2DFactory::drawImage<Gray>(board, image, (unsigned int)0, (unsigned int)129);
80  board.saveSVG("toric-inputShape.svg");
81 
83  //Point Predicate from random seed image
85  PointPredicate predicate(image,0);
87 
91 
92  //Regular 2D domain
93  DTL2 dtL2(image.domain(), predicate, l2Metric);
94  //Full toric 2D domain
95  DTL2Toric dtL2Toric(image.domain(), predicate, l2Metric, {{true, true}} );
97 
101 
102  // 2D domain that is periodic along the first dimension.
103  DTL2ToricX dtL2ToricX( image.domain(), predicate, l2Metric, {{true, false}} );
104  // 2D domain that is periodic along the second dimension.
105  DTL2ToricY dtL2ToricY( image.domain(), predicate, l2Metric, {{false, true}} );
107 
108  //We compute the maximum DT value on the L2 map
109  const DTL2::Value maxv2 = * (std::max_element(dtL2.constRange().begin(), dtL2.constRange().end()));
110  const DTL2Toric::Value maxvtoric = * (std::max_element(dtL2Toric.constRange().begin(), dtL2Toric.constRange().end()));
111  const DTL2ToricX::Value maxvtoricX = * (std::max_element(dtL2ToricX.constRange().begin(), dtL2ToricX.constRange().end()));
112  const DTL2ToricY::Value maxvtoricY = * (std::max_element(dtL2ToricY.constRange().begin(), dtL2ToricY.constRange().end()));
113 
114  // Color map based on the maximal value for all maps (in order to compare results with similar colors).
115  const auto maxvall = std::max( { maxv2, maxvtoric, maxvtoricX, maxvtoricY } );
116 
118  //Colormap used for the SVG output
119  typedef HueShadeColorMap<DTL2::Value, 1> HueTwice;
121 
122  trace.warning() << "DT maxValue= " << maxv2 << endl;
123  board.clear();
124  Display2DFactory::drawImage<HueTwice>(board, dtL2, 0.0, maxvall + 1);
125  board.saveSVG ( "toric-example-DT-L2.svg" );
126 
127  trace.warning() << "Full toric maxValue= " << maxvtoric << endl;
128  board.clear();
129  Display2DFactory::drawImage<HueTwice>(board, dtL2Toric, 0.0, maxvall + 1);
130  board.saveSVG ( "toric-example-DT-L2-toric.svg" );
131 
132  trace.warning() << "1th dimension periodic maxValue= " << maxvtoricX << endl;
133  board.clear();
134  Display2DFactory::drawImage<HueTwice>(board, dtL2ToricX, 0.0, maxvall + 1);
135  board.saveSVG ( "toric-example-DT-L2-toricX.svg" );
136 
137  trace.warning() << "2nd dimension periodic maxValue= " << maxvtoricY << endl;
138  board.clear();
139  Display2DFactory::drawImage<HueTwice>(board, dtL2ToricY, 0.0, maxvall + 1);
140  board.saveSVG ( "toric-example-DT-L2-toricY.svg" );
141 
142  //Explicit export with ticked colormap
143  //We compute the maximum DT value on the L2 map
144  TickedColorMap<double, GradientColorMap<double> > ticked(0.0,maxv2, Color::White);
145  ticked.addRegularTicks(3, 0.5);
146  ticked.finalize();
147  ticked.colormap()->addColor( Color::Red );
148  ticked.colormap()->addColor( Color::Black );
149  board.clear();
150  for ( auto it = dtL2.domain().begin(), itend = dtL2.domain().end();it != itend; ++it)
151  {
152  board<< CustomStyle((*it).className(),new CustomColors(ticked(dtL2(*it)),ticked(dtL2(*it))));
153  board << *it;
154  }
155  board.saveSVG("toric-example-DT-L2-ticked.svg");
156 
157  board.clear();
158  for ( auto it = dtL2Toric.domain().begin(), itend = dtL2Toric.domain().end();it != itend; ++it)
159  {
160  board<< CustomStyle((*it).className(),new CustomColors(ticked(dtL2Toric(*it)),ticked(dtL2Toric(*it))));
161  board << *it;
162  }
163  board.saveSVG("toric-example-DT-L2-ticked-toric.svg");
164 
165  board.clear();
166  for ( auto it = dtL2ToricX.domain().begin(), itend = dtL2ToricX.domain().end();it != itend; ++it)
167  {
168  board<< CustomStyle((*it).className(),new CustomColors(ticked(dtL2ToricX(*it)),ticked(dtL2ToricX(*it))));
169  board << *it;
170  }
171  board.saveSVG("toric-example-DT-L2-ticked-toricX.svg");
172 
173  board.clear();
174  for ( auto it = dtL2ToricY.domain().begin(), itend = dtL2ToricY.domain().end();it != itend; ++it)
175  {
176  board<< CustomStyle((*it).className(),new CustomColors(ticked(dtL2ToricY(*it)),ticked(dtL2ToricY(*it))));
177  board << *it;
178  }
179  board.saveSVG("toric-example-DT-L2-ticked-toricY.svg");
180 
181  //Voronoi vector output
182  board.clear();
183  board << dtL2.domain();
184  for ( auto it = dtL2.domain().begin(), itend = dtL2.domain().end();it != itend; ++it)
185  if ( dtL2.getVoronoiVector(*it) != *it )
186  Display2DFactory::draw(board,dtL2.getVoronoiVector(*it) - (*it), (*it));
187  board.saveSVG("toric-example-Voro-L2.svg");
188 
189  board.clear();
190  board << dtL2Toric.domain();
191  for ( auto it = dtL2Toric.domain().begin(), itend = dtL2Toric.domain().end();it != itend; ++it)
192  if ( dtL2Toric.getVoronoiVector(*it) != *it )
193  Display2DFactory::draw(board, dtL2Toric.getVoronoiVector(*it) - (*it), (*it));
194  board.saveSVG("toric-example-Voro-L2-toric.svg");
195 
196  board.clear();
197  board << dtL2Toric.domain();
198  for ( auto it = dtL2Toric.domain().begin(), itend = dtL2Toric.domain().end();it != itend; ++it)
199  if ( dtL2Toric.getVoronoiVector(*it) != *it )
200  Display2DFactory::draw(board, dtL2Toric.projectPoint(dtL2Toric.getVoronoiVector(*it)) - (*it), (*it));
201  board.saveSVG("toric-example-Voro-L2-toric-projected.svg");
202 
203  board.clear();
204  board << dtL2ToricX.domain();
205  for ( auto it = dtL2ToricX.domain().begin(), itend = dtL2ToricX.domain().end();it != itend; ++it)
206  if ( dtL2ToricX.getVoronoiVector(*it) != *it )
207  Display2DFactory::draw(board, dtL2ToricX.getVoronoiVector(*it) - (*it), (*it));
208  board.saveSVG("toric-example-Voro-L2-toricX.svg");
209 
210  board.clear();
211  board << dtL2ToricY.domain();
212  for ( auto it = dtL2ToricY.domain().begin(), itend = dtL2ToricY.domain().end();it != itend; ++it)
213  if ( dtL2ToricY.getVoronoiVector(*it) != *it )
214  Display2DFactory::draw(board, dtL2ToricY.getVoronoiVector(*it) - (*it), (*it));
215  board.saveSVG("toric-example-Voro-L2-toricY.svg");
216 
217  trace.endBlock();
218  return 0;
219 }
220 // //
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...
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()
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