DGtal  0.9.2
ctopo-fillContours.cpp
1 
30 #include <iostream>
32 #include "ConfigExamples.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/base/Common.h"
35 #include "DGtal/topology/helpers/Surfaces.h"
36 #include "DGtal/topology/SurfelSetPredicate.h"
37 #include "DGtal/geometry/curves/FreemanChain.h"
38 #include "DGtal/io/boards/Board2D.h"
39 
40 
42 
43 using namespace std;
44 using namespace DGtal;
45 
47 
48 int main( int /*argc*/, char** /*argv*/ )
49 {
50  trace.beginBlock ( "Example ctopo-fillContours" );
52  K.init(Z2i::Point(0, 10), Z2i::Point(20, 30), false);
53 
54  // We choose a direct and indirect oriented contour.
56  FreemanChain<int> fc1 ("001001001001001111101111011222222223222222322233333330301033333003", 6, 14);
57  FreemanChain<int> fc2 ("1111000033332222", 6, 20);
59 
60  Board2D aBoard;
61  Board2D aBoard2;
62  aBoard << K.lowerBound() << K.upperBound() ;
63  aBoard2 << K.lowerBound() << K.upperBound() ;
64 
65  //From the FreemanChain we can get a vector of SCell wrapped in a SurfelSetPredicate with sign defined from the FreemanChain orientation:
67 
68  typedef KhalimskySpaceND<2, int>::SCell SCell;
69  std::set<DGtal::KhalimskySpaceND< 2, int >::SCell> boundarySCell;
70  FreemanChain<int>::getInterPixelLinels(K, fc1, boundarySCell, false);
72 
73  aBoard << CustomStyle((*boundarySCell.begin()).className(), new CustomColors(DGtal::Color::Red, DGtal::Color::Red) );
74  for( std::set<DGtal::KhalimskySpaceND< 2, int >::SCell>::const_iterator it= boundarySCell.begin();
75  it!= boundarySCell.end(); it++){
76  aBoard << *it;
77  }
78 
79  // We can also add other freeman chains with indirect orientation to construct a hole in interior of the shape:
81  std::set<DGtal::KhalimskySpaceND< 2, int >::SCell> boundarySCellhole;
82  FreemanChain<int>::getInterPixelLinels(K, fc2, boundarySCellhole, false);
84 
85 
86  aBoard << CustomStyle((*boundarySCell.begin()).className(), new CustomColors(DGtal::Color::Blue, DGtal::Color::Blue) );
87  aBoard2 << CustomStyle((*boundarySCell.begin()).className(), new CustomColors(DGtal::Color::Blue, DGtal::Color::Blue) );
88 
89 
90 
91  for( std::set<DGtal::KhalimskySpaceND< 2, int >::SCell>::const_iterator it= boundarySCellhole.begin(); it!= boundarySCellhole.end(); it++){
92  aBoard << *it;
93  aBoard2 << *it;
94  boundarySCell.insert(*it);
95  }
96 
97 
98  // Now we can compute the unsigned cell associated to interior pixels:
99 
101  typedef ImageContainerBySTLMap< Z2i::Domain, bool> BoolImage2D;
102  BoolImage2D::Domain imageDomain( Z2i::Point(0,10), Z2i::Point(20,30) );
103  BoolImage2D interiorCellImage( imageDomain );
104  Surfaces<DGtal::KhalimskySpaceND< 2, int > >::uFillInterior(K, functors::SurfelSetPredicate<std::set<SCell>,SCell>(boundarySCell),
105  interiorCellImage, 1, false);
107 
108  aBoard << CustomStyle(K.uSpel(Z2i::Point(0,0)).className(), new CustomColors(DGtal::Color::None, Color(200, 200, 200)) );
109  for(BoolImage2D::Domain::ConstIterator it = interiorCellImage.domain().begin();
110  it!=interiorCellImage.domain().end(); it++){
111  if(interiorCellImage(*it)){
112  aBoard << K.uSpel(*it);
113  }
114  }
115 
116 
117  // We can also compute the unsigned cell associated to interior and exterior pixels:
119  BoolImage2D interiorCellHoleImage( imageDomain );
120  BoolImage2D exteriorCellHoleImage( imageDomain );
121 
122 
123  Surfaces<DGtal::KhalimskySpaceND< 2, int > >::uFillInterior(K, functors::SurfelSetPredicate<std::set<SCell>, SCell>(boundarySCellhole),
124  interiorCellHoleImage, 1, true);
125  Surfaces<DGtal::KhalimskySpaceND< 2, int > >::uFillExterior(K, functors::SurfelSetPredicate<std::set<SCell>, SCell>(boundarySCellhole),
126  exteriorCellHoleImage, 1, false);
128 
129  aBoard2 << CustomStyle(K.uSpel(Z2i::Point(0,0)).className(),
130  new CustomColors(DGtal::Color::None, Color(200, 200, 200)) );
131  for(BoolImage2D::Domain::ConstIterator it = interiorCellHoleImage.domain().begin();
132  it!=interiorCellHoleImage.domain().end(); it++){
133  if(interiorCellHoleImage(*it)){
134  aBoard2 << K.uSpel(*it);
135  }
136  }
137  aBoard2 << CustomStyle(K.uSpel(Z2i::Point(0,0)).className(),
138  new CustomColors(DGtal::Color::None, Color(100, 100, 100)) );
139  for(BoolImage2D::Domain::ConstIterator it = exteriorCellHoleImage.domain().begin();
140  it!=exteriorCellHoleImage.domain().end(); it++){
141  if(exteriorCellHoleImage(*it)){
142  aBoard2 << K.uSpel(*it);
143  }
144  }
145 
146  aBoard.saveEPS("example_ctopo-fillContours.eps");
147  aBoard.saveFIG("example_ctopo-fillContours.fig");
148 
149  aBoard2.saveEPS("example_ctopo-fillContours2.eps");
150  aBoard2.saveFIG("example_ctopo-fillContours2.fig");
151  trace.endBlock();
152  return 0;
153 }
154 // //
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:130
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:78
STL namespace.
double endBlock()
Custom style class redefining the pen color and the fill color. You may use Board2D::Color::None for ...
Definition: Board2D.h:278
std::string className() const
void saveFIG(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0, bool includeFIGHeader=true) const
Definition: Board.cpp:907
static const Color None
Definition: Color.h:387
const Point & lowerBound() const
static const Color Blue
Definition: Color.h:394
bool init(const Point &lower, const Point &upper, bool isClosed)
const Point & upperBound() const
DGtal is the top-level namespace which contains all DGtal functions and types.
Represents a signed cell in a cellular grid space by its Khalimsky coordinates and a boolean value...
void saveEPS(const char *filename, PageSize size=Board::BoundingBox, double margin=10.0) const
Definition: Board.cpp:805
Structure representing an RGB triple with alpha component.
Definition: Color.h:66
Aim: The predicate returning true iff the point is in the domain given at construction.
Cell uSpel(Point p) const
static const Color Red
Definition: Color.h:391
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex...
Aim: This class specializes a 'Board' class so as to display DGtal objects more naturally (with <<)...
Definition: Board2D.h:70