DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
imageGridCurveEstimator.cpp
1 
31 
32 #include <iostream>
33 #include <fstream>
34 #include <algorithm>
36 
38 #include "DGtal/base/Common.h"
39 #include "DGtal/helpers/StdDefs.h"
40 #include "ConfigExamples.h"
42 
44 #include "DGtal/base/BasicFunctors.h"
45 #include "DGtal/kernel/BasicPointPredicates.h"
46 #include "DGtal/io/readers/PNMReader.h"
47 #include "DGtal/images/ImageContainerBySTLVector.h"
49 
51 #include "DGtal/topology/helpers/Surfaces.h"
53 
55 #include "DGtal/geometry/curves/estimation/DSSLengthEstimator.h"
57 
58 //display
59 #include "DGtal/io/boards/Board2D.h"
60 
61 //segmentation
62 #include "DGtal/geometry/curves/GreedySegmentation.h"
63 
65 
66 int main()
67 {
68  //image import
70  std::string filename = examplesPath + "samples/contourS.pgm";
71  Image image = DGtal::PNMReader<Image>::importPGM(filename);
72 
74  //predicate from the image
75  typedef IntervalThresholder<Image::Value> Binarizer;
76  Binarizer b(1, 135);
77  PointFunctorPredicate<Image,Binarizer> predicate(image, b);
79 
81  Z2i::KSpace ks; //Khalimsky space
82  ks.init( image.domain().lowerBound(), image.domain().upperBound(), true );
83  SurfelAdjacency<2> sAdj( true ); //adjacency
85 
87  //extraction of all the contours
88  std::vector< std::vector< Z2i::SCell > > contours;
90  ::extractAll2DSCellContours( contours, ks, sAdj, predicate );
92 
93  if (contours.size() > 0)
94  {
95 
97  //init grid curve from the first retrieved contour
98  Z2i::Curve c;
99  c.initFromSCellsVector( contours.at(1) );
101 
103  //range of points
105  Range r = c.getPointsRange();
107 
109  //length estimation based on a DSS segmentation
111  DSSlength.init(1, r.begin(), r.end(), c.isClosed());
112  double length = DSSlength.eval();
113  trace.info() << "Length: " << length << endl;
115 
116  //DSS segmentation display
117  typedef Z2i::Curve::PointsRange::ConstCirculator ConstCirculator;
118  typedef ArithmeticalDSS<ConstCirculator,int,4> SegmentComputer;
119  typedef GreedySegmentation<SegmentComputer> Segmentation;
120 
121  Segmentation theSegmentation( r.c(), r.c(), SegmentComputer() );
122  Segmentation::SegmentComputerIterator i = theSegmentation.begin();
123  Segmentation::SegmentComputerIterator end = theSegmentation.end();
124 
125  DGtal::Board2D aBoard;
126  aBoard << SetMode("PointVector", "Grid");
127  for ( ; i != end; ++i) {
128  aBoard << SetMode(i->className(), "Points") << *i;
129  aBoard << SetMode(i->className(), "BoundingBox") << *i;
130  }
131  aBoard.saveEPS("DisplayDSSSegmentationTuto3.eps");
132 
133  } else trace.info() << "no contour" << endl;
134 
135  return 0;
136 
137 }
138