DGtal  1.5.beta
topology/ctopo-fillContours.cpp

A simple example illustrating the filling of the interior/exterior of a given 2D contour. This program outputs these images:

See also
Filling oriented digital contours
#include <iostream>
#include "ConfigExamples.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/base/Common.h"
#include "DGtal/topology/helpers/Surfaces.h"
#include "DGtal/topology/SurfelSetPredicate.h"
#include "DGtal/geometry/curves/FreemanChain.h"
#include "DGtal/io/boards/Board2D.h"
#include "DGtal/images/ImageContainerBySTLMap.h"
using namespace std;
using namespace DGtal;
int main( int /*argc*/, char** /*argv*/ )
{
trace.beginBlock ( "Example ctopo-fillContours" );
K.init(Z2i::Point(0, 10), Z2i::Point(20, 30), false);
// We choose a direct and indirect oriented contour.
FreemanChain<int> fc1 ("001001001001001111101111011222222223222222322233333330301033333003", 6, 14);
FreemanChain<int> fc2 ("1111000033332222", 6, 20);
Board2D aBoard;
Board2D aBoard2;
aBoard << K.lowerBound() << K.upperBound() ;
aBoard2 << K.lowerBound() << K.upperBound() ;
//From the FreemanChain we can get a vector of SCell wrapped in a SurfelSetPredicate with sign defined from the FreemanChain orientation:
FreemanChain<int>::getInterPixelLinels(K, fc1, boundarySCell, false);
aBoard << CustomStyle((*boundarySCell.begin()).className(), new CustomColors(DGtal::Color::Red, DGtal::Color::Red) );
for( std::set<DGtal::KhalimskySpaceND< 2, int >::SCell>::const_iterator it= boundarySCell.begin();
it!= boundarySCell.end(); it++){
aBoard << *it;
}
// We can also add other freeman chains with indirect orientation to construct a hole in interior of the shape:
FreemanChain<int>::getInterPixelLinels(K, fc2, boundarySCellhole, false);
aBoard << CustomStyle((*boundarySCell.begin()).className(), new CustomColors(DGtal::Color::Blue, DGtal::Color::Blue) );
aBoard2 << CustomStyle((*boundarySCell.begin()).className(), new CustomColors(DGtal::Color::Blue, DGtal::Color::Blue) );
for( std::set<DGtal::KhalimskySpaceND< 2, int >::SCell>::const_iterator it= boundarySCellhole.begin(); it!= boundarySCellhole.end(); it++){
aBoard << *it;
aBoard2 << *it;
boundarySCell.insert(*it);
}
// Now we can compute the unsigned cell associated to interior pixels:
typedef ImageContainerBySTLMap< Z2i::Domain, bool> BoolImage2D;
BoolImage2D::Domain imageDomain( Z2i::Point(0,10), Z2i::Point(20,30) );
BoolImage2D interiorCellImage( imageDomain );
Surfaces<DGtal::KhalimskySpaceND< 2, int > >::uFillInterior(K, functors::SurfelSetPredicate<std::set<SCell>,SCell>(boundarySCell),
interiorCellImage, 1, false);
aBoard << CustomStyle(K.lowerCell().className(), new CustomColors(DGtal::Color::None, Color(200, 200, 200)) );
for(BoolImage2D::Domain::ConstIterator it = interiorCellImage.domain().begin();
it!=interiorCellImage.domain().end(); it++){
if(interiorCellImage(*it)){
aBoard << K.uSpel(*it);
}
}
// We can also compute the unsigned cell associated to interior and exterior pixels:
BoolImage2D interiorCellHoleImage( imageDomain );
BoolImage2D exteriorCellHoleImage( imageDomain );
Surfaces<DGtal::KhalimskySpaceND< 2, int > >::uFillInterior(K, functors::SurfelSetPredicate<std::set<SCell>, SCell>(boundarySCellhole),
interiorCellHoleImage, 1, true);
Surfaces<DGtal::KhalimskySpaceND< 2, int > >::uFillExterior(K, functors::SurfelSetPredicate<std::set<SCell>, SCell>(boundarySCellhole),
exteriorCellHoleImage, 1, false);
aBoard2 << CustomStyle(K.lowerCell().className(),
new CustomColors(DGtal::Color::None, Color(200, 200, 200)) );
for(BoolImage2D::Domain::ConstIterator it = interiorCellHoleImage.domain().begin();
it!=interiorCellHoleImage.domain().end(); it++){
if(interiorCellHoleImage(*it)){
aBoard2 << K.uSpel(*it);
}
}
aBoard2 << CustomStyle(K.lowerCell().className(),
new CustomColors(DGtal::Color::None, Color(100, 100, 100)) );
for(BoolImage2D::Domain::ConstIterator it = exteriorCellHoleImage.domain().begin();
it!=exteriorCellHoleImage.domain().end(); it++){
if(exteriorCellHoleImage(*it)){
aBoard2 << K.uSpel(*it);
}
}
aBoard.saveEPS("example_ctopo-fillContours.eps");
aBoard.saveFIG("example_ctopo-fillContours.fig");
aBoard2.saveEPS("example_ctopo-fillContours2.eps");
aBoard2.saveFIG("example_ctopo-fillContours2.fig");
return 0;
}
// //
static const Color None
Definition: Color.h:412
static const Color Red
Definition: Color.h:416
static const Color Blue
Definition: Color.h:419
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
void beginBlock(const std::string &keyword="")
double endBlock()
Z3i::SCell SCell
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
int main(int argc, char **argv)
MyPointD Point
Definition: testClone2.cpp:383
KSpace K
HyperRectDomain< Space > Domain