DGtal  0.9.3beta
Tutorial "Image -> Region -> Distance Transformation"

Table of Contents

Author(s) of this documentation:
David Coeurjolly

See the source code: imageSetDT.cpp


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

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).

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";
Image image = DGtal::PGMReader<Image>::importPGM(filename);
DGtal::trace.info() << "Imported image: "<<image<<std::endl;

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}
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();
Display2DFactory::drawImage<Gray>(aBoard, image, (unsigned char)0, (unsigned char)255);
Zoom on the domain output
Input image
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 );

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>() ));
Display2DFactory::drawImage<HueTwice>(aBoard, dt, (DTL2::Value)0,
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"