DGtalTools  1.5.beta
curvatureMCMS.cpp
1 
32 #include <iostream>
33 
34 #include "DGtal/base/Common.h"
35 
36 #include "DGtal/shapes/ShapeFactory.h"
37 #include "DGtal/shapes/Shapes.h"
38 #include "DGtal/helpers/StdDefs.h"
39 #include "DGtal/topology/helpers/Surfaces.h"
40 
41 //Grid curve
42 #include "DGtal/geometry/curves/FreemanChain.h"
43 #include "DGtal/geometry/curves/GridCurve.h"
44 
45 //Estimators
46 #include "DGtal/geometry/curves/ArithmeticalDSSComputer.h"
47 #include "DGtal/geometry/curves/estimation/MostCenteredMaximalSegmentEstimator.h"
48 #include "DGtal/geometry/curves/estimation/SegmentComputerEstimators.h"
49 
50 #include "CLI11.hpp"
51 
52 #include <vector>
53 #include <string>
54 #include <iomanip>
55 
56 
57 using namespace DGtal;
58 using namespace std;
59 
60 
110 /*
111  Curvature estimation from the length of the most centered maximal segments
112  @param h grid step
113  @param itb begin iterator on points
114  @param ite end iterator on points
115  @param ito output iterator on curvature
116  */
117 template<typename I, typename O>
118 void estimationFromLength( double h, const I& itb, const I& ite, const O& ito )
119 {
120  typedef ArithmeticalDSSComputer<I,int,4> SegmentComputer;
121  SegmentComputer sc;
123  SCEstimator f;
125  Estimator CurvatureEstimator(sc, f);
126 
127  CurvatureEstimator.eval( itb, ite, ito, h );
128 }
129 
130 /*
131  Curvature estimation from the length and width
132  of the most centered maximal segments
133  @param h grid step
134  @param itb begin iterator on points
135  @param ite end iterator on points
136  @param ito output iterator on curvature
137  */
138 template<typename I, typename O>
139 void estimationFromLengthAndWidth( double h, const I& itb, const I& ite, const O& ito )
140 {
141  typedef ArithmeticalDSSComputer<I,int,4> SegmentComputer;
142  SegmentComputer sc;
143  typedef CurvatureFromDSSEstimator<SegmentComputer> SCEstimator;
144  SCEstimator f;
146  Estimator CurvatureEstimator(sc, f);
147 
148  CurvatureEstimator.eval( itb, ite, ito, h );
149 }
150 
151 
153 
154 int main( int argc, char** argv )
155 {
156  // parse command line CLI ----------------------------------------------
157  CLI::App app;
158  string fileName;
159  double h {1.0};
160 
161  app.description("Estimates curvature using length of most centered segment computers.\n Typical use example:\n \t curvatureMCMS [options] --input <fileName>\n");
162  app.add_option("-i,--input, 1",fileName,"Input FreemanChain file name")->required()->check(CLI::ExistingFile);
163  app.add_option("--GridStep", h, "Grid step",true);
164 
165  app.get_formatter()->column_width(40);
166  CLI11_PARSE(app, argc, argv);
167  // END parse command line using CLI ----------------------------------------------
168 
169  typedef Z2i::Space Space;
170  typedef Space::Point Point;
171  typedef Space::Integer Integer;
172  typedef Z2i::KSpace KSpace;
174 
175  vector< FreemanChain > vectFcs = PointListReader< Point >::getFreemanChainsFromFile<Integer> (fileName);
176 
177  for(unsigned int i=0; i<vectFcs.size(); i++){
178 
179  // Freeman chain
180  FreemanChain fc = vectFcs.at(i);
181  // Create GridCurve
182  GridCurve<> gridcurve;
183  gridcurve.initFromPointsRange( fc.begin(), fc.end() );
184 
185  cout << "# grid curve " << i+1 << "/"
186  << gridcurve.size() << " "
187  << ( (gridcurve.isClosed())?"closed":"open" ) << endl;
188 
189  // Create range of incident points
190  typedef GridCurve<KSpace>::PointsRange Range;
191  Range r = gridcurve.getPointsRange();//building range
192 
193  // Estimation
194  cout << "# Curvature estimation from maximal segments" << endl;
195  std::vector<double> estimations1;
196  std::vector<double> estimations2;
197  if (gridcurve.isOpen())
198  {
199  cout << "# open grid curve" << endl;
200  estimationFromLength( h, r.begin(), r.end(), back_inserter(estimations1) );
201  estimationFromLengthAndWidth( h, r.begin(), r.end(), back_inserter(estimations2) );
202  }
203  else
204  {
205  cout << "# closed grid curve" << endl;
206  estimationFromLength( h, r.c(), r.c(), back_inserter(estimations1) );
207  estimationFromLengthAndWidth( h, r.c(), r.c(), back_inserter(estimations2) );
208  }
209 
210  // Output
211  cout << "# id curvatureFromLength curvatureFromLengthAndWidth" << endl;
212  unsigned int j = 0;
213  for ( Range::ConstIterator it = r.begin(), itEnd = r.end();
214  it != itEnd; ++it, ++j ) {
215 cout << j << setprecision( 15 )
216  << " " << estimations1[ j ]
217  << " " << estimations2[ j ] << endl;
218  }
219 
220  }
221 
222 
223  return 0;
224 }
225 
int main(int argc, char **argv)
ConstIterator begin() const
ConstIterator end() const
PointsRange getPointsRange() const
bool initFromPointsRange(const TIterator &itb, const TIterator &ite)
bool isClosed() const
bool isOpen() const
Storage::size_type size() const
typename Self::Point Point
DGtal::int32_t Integer
SpaceND< 2, Integer > Space