DGtal 1.3.0
Loading...
Searching...
No Matches
Tutorial "Image -> Region -> Grid curve -> Extracting level set boundary from Image"
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 PGMReader:

std::string filename = examplesPath + "samples/circleR10modif.pgm";
static ImageContainer importPGM(const std::string &aFilename, const Functor &aFunctor=Functor(), bool topbotomOrder=true)
ImageContainerBySTLVector< Domain, Value > Image

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

Z2i::DigitalSet set2d (image.domain());
SetFromImage<Z2i::DigitalSet>::append<Image>(set2d, image, 1, 255);
DigitalSetSelector< Domain, BIG_DS+HIGH_BEL_DS >::Type DigitalSet
Definition: StdDefs.h:100

You can display the thresholded set:

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


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 );
KhalimskySpaceND< 2, Integer > KSpace
Definition: StdDefs.h:77

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, set2d, sAdj );
static void extractAllPointContours4C(std::vector< std::vector< Point > > &aVectPointContour2D, const KSpace &aKSpace, const PointPredicate &pp, const SurfelAdjacency< 2 > &aSAdj)

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;
static const Color None
Definition: Color.h:412

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:

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

SurfelAdjacency<2> sAdj( false );

you will obtain the followings contours:

Extracting and displaying contours from the DGtalTools project

Project available in the DGtal website (http://libdgtal.org/tools/)

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

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

This example will load the pgm source file "church.pgm" and applies successive thresholds [100,105], [100, 110].... [100, 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":

./visualisation/displayContours --FreemanChain contours.fc --backgroundImage ../../DGtal/examples/samples/church.png --outputFile levelSetImage.eps
DGtal is the top-level namespace which contains all DGtal functions and types.

You will obtain the following result in eps (you can also use the pdf format if cairo is installed on your system).

By adding the obption

--alphaBG 0.5

the backgroundImage will be rendered with transparency.

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/PGMReader.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"
using namespace DGtal;