DGtalTools  0.9.4
curvatureMCMS.cpp
1 
31 #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 
51 #include <boost/program_options/options_description.hpp>
52 #include <boost/program_options/parsers.hpp>
53 #include <boost/program_options/variables_map.hpp>
54 
55 #include <vector>
56 #include <string>
57 #include <iomanip>
58 
59 
60 using namespace DGtal;
61 using namespace std;
62 
63 
109 /*
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.init( h, itb, ite );
128  CurvatureEstimator.eval( itb, ite, ito );
129 }
130 
131 /*
132  Curvature estimation from the length and width
133  of the most centered maximal segments
134  @param h grid step
135  @param itb begin iterator on points
136  @param ite end iterator on points
137  @param ito output iterator on curvature
138  */
139 template<typename I, typename O>
140 void estimationFromLengthAndWidth( double h, const I& itb, const I& ite, const O& ito )
141 {
142  typedef ArithmeticalDSSComputer<I,int,4> SegmentComputer;
143  SegmentComputer sc;
144  typedef CurvatureFromDSSEstimator<SegmentComputer> SCEstimator;
145  SCEstimator f;
147  Estimator CurvatureEstimator(sc, f);
148 
149  CurvatureEstimator.init( h, itb, ite );
150  CurvatureEstimator.eval( itb, ite, ito );
151 }
152 
153 
155 namespace po = boost::program_options;
156 
157 int main( int argc, char** argv )
158 {
159  // parse command line ----------------------------------------------
160  po::options_description general_opt("Allowed options are");
161  general_opt.add_options()
162  ("help,h", "display this message")
163  ("input,i", po::value<std::string>(), "input FreemanChain file name")
164  ("GridStep,step", po::value<double>()->default_value(1.0), "Grid step");
165 
166 
167  bool parseOK=true;
168  po::variables_map vm;
169  try{
170  po::store(po::parse_command_line(argc, argv, general_opt), vm);
171  }catch(const std::exception& ex){
172  parseOK=false;
173  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
174  }
175 
176  po::notify(vm);
177  if(!parseOK || vm.count("help")||argc<=1 || (!(vm.count("input"))) )
178  {
179  trace.info() << "Estimates curvature using length of most centered segment computers. " << std::endl;
180  trace.info() << "Basic usage: " << std::endl
181  << "\t curvatureMCMS [options] --input <fileName> "<< std::endl
182  << general_opt << "\n";
183  return 0;
184  }
185 
186 
187  double h = vm["GridStep"].as<double>();
188 
189  if(vm.count("input")){
190  string fileName = vm["input"].as<string>();
191 
192  typedef Z2i::Space Space;
193  typedef Space::Point Point;
194  typedef Space::Integer Integer;
195  typedef Z2i::KSpace KSpace;
197 
198  vector< FreemanChain > vectFcs = PointListReader< Point >::getFreemanChainsFromFile<Integer> (fileName);
199 
200  for(unsigned int i=0; i<vectFcs.size(); i++){
201 
202  // Freeman chain
203  FreemanChain fc = vectFcs.at(i);
204  // Create GridCurve
205  GridCurve<> gridcurve;
206  gridcurve.initFromPointsRange( fc.begin(), fc.end() );
207 
208  cout << "# grid curve " << i+1 << "/"
209  << gridcurve.size() << " "
210  << ( (gridcurve.isClosed())?"closed":"open" ) << endl;
211 
212  // Create range of incident points
213  typedef GridCurve<KSpace>::PointsRange Range;
214  Range r = gridcurve.getPointsRange();//building range
215 
216  // Estimation
217  cout << "# Curvature estimation from maximal segments" << endl;
218  std::vector<double> estimations1;
219  std::vector<double> estimations2;
220  if (gridcurve.isOpen())
221  {
222  cout << "# open grid curve" << endl;
223  estimationFromLength( h, r.begin(), r.end(), back_inserter(estimations1) );
224  estimationFromLengthAndWidth( h, r.begin(), r.end(), back_inserter(estimations2) );
225  }
226  else
227  {
228  cout << "# closed grid curve" << endl;
229  estimationFromLength( h, r.c(), r.c(), back_inserter(estimations1) );
230  estimationFromLengthAndWidth( h, r.c(), r.c(), back_inserter(estimations2) );
231  }
232 
233  // Output
234  cout << "# id curvatureFromLength curvatureFromLengthAndWidth" << endl;
235  unsigned int j = 0;
236  for ( Range::ConstIterator it = r.begin(), itEnd = r.end();
237  it != itEnd; ++it, ++j ) {
238  cout << j << setprecision( 15 )
239  << " " << estimations1[ j ]
240  << " " << estimations2[ j ] << endl;
241  }
242 
243  }
244 
245  }
246 
247  return 0;
248 }
249 
DGtal::int32_t Integer
PointsRange getPointsRange() const
SpaceND< 2, Integer > Space
STL namespace.
ConstIterator begin() const
bool isClosed() const
bool initFromPointsRange(const TIterator &itb, const TIterator &ite)
bool isOpen() const
ConstIterator end() const
Storage::size_type size() const
Trace trace(traceWriterTerm)
std::ostream & info()
typename Self::Point Point