DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Tutorial "Image -> Region -> Grid curve -> Extracting level set boundary from Image"

Table of Contents

Author:
Bertrand Kerautret

This part of the manual describes a simple way to extract a level set boundary as a FreemanChain contour from an gray level image. The first step is to extract the thresholed Digital set from the image (see. DGtal::SetFromImage) and then to track its boundary (see. DGtal::Surfaces). An example can also be found in the example tutorial-examples/freemanChainFromImage.cpp

DigitalSet from thresholded image

The digitalSet can be obtained after thresholding a given image. As described in the section Image and digital object import/export a image can simply be imported with readers as for instance in PNMReader:

std::string filename = examplesPath + "samples/circleR10modif.pgm";
Image image = DGtal::PNMReader<Image>::importPGM(filename);

From the imported image you can extract a DigitalSet by using thresholds:

Z2i::DigitalSet set2d (image.domain());
SetPredicate<Z2i::DigitalSet> set2dPredicate( set2d );
SetFromImage<Z2i::DigitalSet>::append<Image>(set2d, image, 1, 255);

You can display the thresholded set:

Board2D aBoard;
aBoard << set2d;
aBoard << image.domain();
fcExtraction0.png

A KhalimskySpaceND is also needed to extract the region boundary, you can simply use the domain of the imported image to initialise a KhalimskySpaceND:

ks.init( image.domain().lowerBound(), image.domain().upperBound(), true );

Another important element is to define the SurfelAdjacency used to track the DigitalSet boundary:

SurfelAdjacency<2> sAdj( true );

Contour extraction

Afterwards the set of all 4-connected contours can be extracted by using the function extractAllPointContours4C from DGtal::Surfaces:

std::vector< std::vector< Z2i::Point > > vectContoursBdryPointels;
ks, set2dPredicate, sAdj );

Freemanchain construction and display

From the previous vector containing the contour points, we can construct and display its associated FreemanChain.

for(unsigned int i=0; i<vectContoursBdryPointels.size(); i++){
// Constructing and displaying FreemanChains from contours.
FreemanChain<Z2i::Integer> fc (vectContoursBdryPointels.at(i));

Since the reconstructed freemanchain represents the inter pixels boundary of the digital set we can specify it by using the mode "InterGrid" as follows:

aBoard << SetMode( fc.className(), "InterGrid" );
aBoard<< CustomStyle( fc.className(),
new CustomColors( cmap_grad(i), Color::None ) );
aBoard << fc;

By using the display color defining in the example tutorial-examples/freemanChainFromImage.cpp you may obtain the following result where each contour are represented with a specific color:

fcExtraction.png

Note that if you change the SurfelAdjacency used in the contour extraction:

SurfelAdjacency<2> sAdj( false );

you will obtain the followings contours:

fcExtraction2.png

Extracting and displaying contours from the DGtalTools project (https://github.com/DGtal-team/DGtalTools)

The directory 2dContourTools contains command line program "pgm2freeman" allowing to simply extract level set contours from an pgm image:

./2dContourTools/pgm2freeman --image ../../DGTal/examples/samples/church.pgm --minSize 300 --contourSelect 129 526 300 --thresholdRange 100 5 220 > contours.fc ;

This example will load the pgm source file "church.pgm" and applies successive thresholds [100,105], [105, 110].... [215, 220] and selects all the boundary contours with a minimal size equals to 300 and with a distance between its barycenter and point (129 526) less than 300.

The resulting file "contours.fc" contains a freemanchain on each line and can be displayed with the command line program "displayContours":

./2dContourTools/displayContours --FreemanChain contours.fc --backgroundImage ../../DGtal/examples/samples/church.png --outputEPS levelSetImage.eps --outputPDF levelSetImage.pdf

You will obtain the following result in eps and pdf format (if cairo is installed on your system) (left image). By adding the obption

--alphaBG 0.5

the backgroundImage will be rendered with transparency (right image).

churchLevelSets.png
churchLevelSetsTransp.png

Required includes

You need to include the DGTal basic header files:

#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "ConfigExamples.h"

To import the image and create digital set you need to include the following header files:

#include "DGtal/io/readers/PNMReader.h"
#include "DGtal/images/imagesSetsUtils/SetFromImage.h"

To track the frontier of the digital set and create the digital set you nedd to include:

#include "DGtal/topology/helpers/Surfaces.h"
#include "DGtal/geometry/curves/FreemanChain.h"

For the display with gradient color:

#include "DGtal/io/boards/Board2D.h"
#include "DGtal/io/colormaps/GradientColorMap.h"
#include "DGtal/io/Color.h"