DGtal 1.3.0
Loading...
Searching...
No Matches
Tutorial "Image -> Region -> Distance Transformation"
Author(s) of this documentation:
David Coeurjolly

See the source code: imageSetDT.cpp

Introduction

In this example, we illustrate a very simple shape analysis pipeline:

  • Image loading from file
  • Distance Transformation with the Euclidean metric of a region of interest.

For the sake of simplicity, we just consider a straightforward segmentation step using a simple threshold on gray level values.

Let's start with the definition of basic types (an standard image type from the Z2i shortcut and a grayscale map for exports).

Aim: This class template may be used to (linearly) convert scalar values in a given range into gray l...
ImageContainerBySTLVector< Domain, Value > Image
Note
In this example, the image value type is unsigned char.

Reading an image from a file

Here, we just construct an image using the image reader for the PGM image format.

std::string filename = examplesPath + "samples/contourS.pgm";
DGtal::trace.info() << "Imported image: "<<image<<std::endl;
std::ostream & info()
Trace trace
Definition: Common.h:154
static ImageContainer importPGM(const std::string &aFilename, const Functor &aFunctor=Functor(), bool topbotomOrder=true)

The trace stream displays the following message on the std:cerr output:

Imported image: [Image - STLVector] size=15725 valuetype=1bytes lower=[PointVector] {0, 0} upper=[PointVector] {184, 84}
Vector lower(const Vector &z, unsigned int k)
Vector upper(const Vector &z, unsigned int k)
Note
In case of IO error (file not found, wrong format,...) an DGtal::IOException is raised.

Board exports

In the DGtal structure, an Image is an mapping Points<->Values for a set of points in a digital Domain. In the following example, we use the Board2D mechanism to export the image domain to an SVG file, and the image itself to an EPS file.

aBoard << image.domain();
aBoard.saveSVG("imageDomainTuto.svg");
aBoard.clear();
Display2DFactory::drawImage<Gray>(aBoard, image, (unsigned char)0, (unsigned char)255);
aBoard.saveEPS("imageDomainTuto2.eps");
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
void clear(const DGtal::Color &color=DGtal::Color::None)
Definition: Board.cpp:152
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:805
void saveSVG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:1012
Zoom on the domain output
Input image
Note
Supported 2D export format: EPS, SVG, FIG, TIKZ (+ PNG and PDF if libcairo is installed).

Distance transformation

We illustrate now the Euclidean distance transformation (DT) computation on an image region.

The Euclidean DT consists in labelling each point of the object with the Euclidean distance to the closest background point.

In this example, the object (a.k.a. foreground) is implicitly defined as the set of pixels with value in the interval ]0,135].

Resulting digital set after the segmentation step.
typedef functors::IntervalForegroundPredicate<Image> Binarizer;
Binarizer b(image,1, 135);
DTL2 dt(&image.domain(),&b, &Z2i::l2Metric );
Aim: Implementation of the linear in time distance transformation for separable metrics.
static const L2Metric l2Metric
Definition: StdDefs.h:123

We can now export the result using a Hue shade color map. Since Image containers are consistent with some STL concepts, we can use the std::max_element function to extract the maximal value form the DT map.

DTL2::Value maxDT = (*boost::first_max_element(dt.constRange().begin(),
dt.constRange().end(), std::less<DTL2::Value>() ));
aBoard.clear();
Display2DFactory::drawImage<HueTwice>(aBoard, dt, (DTL2::Value)0,
(DTL2::Value)maxDT);
aBoard.saveEPS("imageDomainTuto3.eps");
Aim: This class template may be used to (linearly) convert scalar values in a given range into a colo...
Euclidean DT result

Required includes

#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/base/BasicFunctors.h"
#include "DGtal/kernel/BasicPointPredicates.h"
#include "DGtal/kernel/sets/DigitalSetInserter.h"
#include "DGtal/images/ImageContainerBySTLVector.h"
#include "DGtal/images/ImageHelper.h"
#include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
#include "DGtal/images/IntervalForegroundPredicate.h"
#include "DGtal/io/boards/Board2D.h"
#include "DGtal/io/readers/PGMReader.h"
#include "DGtal/io/colormaps/HueShadeColorMap.h"
#include "DGtal/io/colormaps/GrayscaleColorMap.h"
#include "ConfigExamples.h"