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