DGtal  1.3.beta
geometry/volumes/distance/distancetransform2D.cpp

Example of distance transformation in dimension 2.

See also
nD Volumetric Analysis using Separable Processes
Distance transform with L2 norm.
#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"
using namespace std;
using namespace DGtal;
template<typename Image>
void randomSeeds(Image &image, const unsigned int nb, const int value)
{
typename Image::Point p, low = image.domain().lowerBound();
typename Image::Vector ext;
ext = image.extent();
for (unsigned int k = 0 ; k < nb; k++)
{
for (unsigned int dim = 0; dim < Image::dimension; dim++)
p[dim] = rand() % (ext[dim]) + low[dim];
image.setValue(p, value);
}
}
int main()
{
trace.beginBlock ( "Example distancetransform2D" );
Z2i::Point a ( 0, 0 );
Z2i::Point b ( 127, 127);
//Input image with unsigned char values
Image image ( Z2i::Domain(a, b ));
//We fill the image with the 128 value
for ( Image::Iterator it = image.begin(), itend = image.end();it != itend; ++it)
(*it)=128;
//We generate 16 seeds with 0 values.
randomSeeds(image,16,0);
//Input shape output
Board2D board;
Display2DFactory::drawImage<Gray>(board, image, (unsigned int)0, (unsigned int)129);
board.saveSVG("inputShape.svg");
//Point Predicate from random seed image
PointPredicate predicate(image,0);
DTL2 dtL2(image.domain(), predicate, Z2i::l2Metric);
DTL1 dtL1(image.domain(), predicate, Z2i::l1Metric);
DTL2::Value maxv2=0;
//We compute the maximum DT value on the L2 map
for ( DTL2::ConstRange::ConstIterator it = dtL2.constRange().begin(), itend = dtL2.constRange().end();it != itend; ++it)
if ( (*it) > maxv2) maxv2 = (*it);
DTL1::Value maxv1=0;
//We compute the maximum DT value on the L1 map
for ( DTL1::ConstRange::ConstIterator it = dtL1.constRange().begin(), itend = dtL1.constRange().end();it != itend; ++it)
if ( (*it) > maxv1) maxv1 = (*it);
//Colormap used for the SVG output
trace.warning() << dtL2 << " maxValue= "<<maxv2<< endl;
board.clear();
Display2DFactory::drawImage<HueTwice>(board, dtL2, 0.0, maxv2 + 1);
board.saveSVG ( "example-DT-L2.svg" );
trace.warning() << dtL1 << " maxValue= "<<maxv1<< endl;
board.clear();
Display2DFactory::drawImage<HueTwice>(board, dtL1, 0.0, maxv1 + 1);
board.saveSVG ( "example-DT-L1.svg" );
//Explicit export with ticked colormap
//We compute the maximum DT value on the L2 map
board.clear();
TickedColorMap<double, GradientColorMap<double> > ticked(0.0,maxv2, Color::White);
ticked.addRegularTicks(5, 0.5);
ticked.finalize();
ticked.colormap()->addColor( Color::Red );
ticked.colormap()->addColor( Color::Black );
for ( DTL2::Domain::ConstIterator it = dtL2.domain().begin(), itend = dtL2.domain().end();it != itend; ++it)
{
board<< CustomStyle((*it).className(),new CustomColors(ticked(dtL2(*it)),ticked(dtL2(*it))));
board << *it;
}
board.saveSVG("example-DT-L2-ticked.svg");
return 0;
}
// //
LibBoard::Board::setUnit
void setUnit(Unit unit)
Definition: Board.cpp:240
DGtal::GrayscaleColorMap
Aim: This class template may be used to (linearly) convert scalar values in a given range into gray l...
Definition: GrayscaleColorMap.h:93
ConstIterator
MyDigitalSurface::ConstIterator ConstIterator
Definition: greedy-plane-segmentation-ex2.cpp:93
image
Image image(domain)
DGtal::HyperRectDomain< Space >
DGtal::Trace::endBlock
double endBlock()
DGtal::ImageContainerBySTLVector
Definition: ImageContainerBySTLVector.h:126
LibBoard::Board::UCentimeter
@ UCentimeter
Definition: Board.h:43
DGtal::TickedColorMap
Aim: This class adapts any colormap to add "ticks" in the colormap colors.
Definition: TickedColorMap.h:78
DGtal::HueShadeColorMap
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Definition: HueShadeColorMap.h:90
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
dim
unsigned int dim(const Vector &z)
Definition: viewDualSurface.cpp:174
LibBoard::Board::clear
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
DGtal::CustomStyle
Definition: Board2D.h:217
Image
ImageContainerBySTLVector< Domain, Value > Image
Definition: testSimpleRandomAccessRangeFromPoint.cpp:45
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::CustomColors
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:278
DGtal::ImageContainerBySTLVector< Domain, Value >::Iterator
std::vector< Value >::iterator Iterator
Definition: ImageContainerBySTLVector.h:264
LibBoard::Board::saveSVG
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
randomSeeds
void randomSeeds(Image &input, const unsigned int nb, const int value)
Definition: testDistanceTransformation.cpp:60
main
int main(int argc, char **argv)
Definition: testArithmeticDSS-benchmark.cpp:147
DGtal::Board2D
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:70
DGtal::Image
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
DGtal::PointVector< dim, Integer >
DGtal::DistanceTransformation
Aim: Implementation of the linear in time distance transformation for separable metrics.
Definition: DistanceTransformation.h:98
DGtal::Trace::warning
std::ostream & warning()
Value
double Value
Definition: testSimpleRandomAccessRangeFromPoint.cpp:38
DGtal::functors::SimpleThresholdForegroundPredicate
Aim: Define a simple Foreground predicate thresholding image values given a single thresold....
Definition: SimpleThresholdForegroundPredicate.h:65