DGtal  1.4.beta
distancetransform2D.cpp File Reference
#include <iostream>
#include <iomanip>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/io/colormaps/GrayscaleColorMap.h"
#include "DGtal/io/colormaps/HueShadeColorMap.h"
#include "DGtal/io/colormaps/TickedColorMap.h"
#include "DGtal/io/colormaps/GradientColorMap.h"
#include "DGtal/io/boards/Board2D.h"
#include "DGtal/images/ImageSelector.h"
#include "DGtal/images/SimpleThresholdForegroundPredicate.h"
#include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
Include dependency graph for distancetransform2D.cpp:

Go to the source code of this file.

Functions

template<typename Image >
void randomSeeds (Image &image, const unsigned int nb, const int value)
 
int main ()
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
David Coeurjolly (david.nosp@m..coe.nosp@m.urjol.nosp@m.ly@l.nosp@m.iris..nosp@m.cnrs.nosp@m..fr ) Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2010/11/28

An example file named distancetransform2D. The aim of this example is to demonstrate the distance transformation package of the DGtal library. Using a simple random seed example, we compute the distance transformation (linear in time algorithm) using the L1, Linfinity and L2 metrics.

This file is part of the DGtal library.

Definition in file distancetransform2D.cpp.

Function Documentation

◆ main()

int main ( void  )

[DTDef]

[DTDef]

[DTPredicate]

[DTPredicate]

[DTCompute]

[DTCompute]

[DTColormaps]

[DTColormaps]

Definition at line 88 of file distancetransform2D.cpp.

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 }
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.
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)
MyDigitalSurface::ConstIterator ConstIterator
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

References DGtal::TickedColorMap< TValue, TColorMap >::addRegularTicks(), DGtal::Trace::beginBlock(), LibBoard::Board::clear(), DGtal::TickedColorMap< TValue, TColorMap >::colormap(), DGtal::Trace::endBlock(), DGtal::TickedColorMap< TValue, TColorMap >::finalize(), image(), randomSeeds(), LibBoard::Board::saveSVG(), LibBoard::Board::setUnit(), DGtal::trace, LibBoard::Board::UCentimeter, and DGtal::Trace::warning().

◆ randomSeeds()

template<typename Image >
void randomSeeds ( Image image,
const unsigned int  nb,
const int  value 
)

Set to a given value a random set of nb points.

Parameters
imagethe image
nbthe number of random points to insert
valuethe value to add at each random point

Definition at line 72 of file distancetransform2D.cpp.

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 }
unsigned int dim(const Vector &z)

References dim().

Referenced by main().