DGtal 1.3.0
Loading...
Searching...
No Matches
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"
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:
std::set<DGtal::KhalimskySpaceND< 2, int >::SCell> boundarySCell;
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:
std::set<DGtal::KhalimskySpaceND< 2, int >::SCell> boundarySCellhole;
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:
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");
trace.endBlock();
return 0;
}
// //
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)....
Definition: Board2D.h:71
Structure representing an RGB triple with alpha component.
Definition: Color.h:68
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,...
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:79
void beginBlock(const std::string &keyword="")
double endBlock()
void saveFIG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0, bool includeFIGHeader=true) const
Definition: Board.cpp:907
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:805
Z3i::SCell SCell
DGtal is the top-level namespace which contains all DGtal functions and types.
STL namespace.
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:279
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value.
Aim: The predicate returning true iff the point is in the domain given at construction.
ShapeList & insert(const Shape &shape, int depth)
Definition: ShapeList.cpp:184
int main()
Definition: testBits.cpp:56
KSpace K