DGtalTools  0.9.2
tangentBC.cpp
1 
30 #include <cmath>
32 #include <iostream>
33 #include <vector>
34 #include <string>
35 #include <boost/program_options/options_description.hpp>
36 #include <boost/program_options/parsers.hpp>
37 #include <boost/program_options/variables_map.hpp>
38 
39 
40 #include "DGtal/base/Common.h"
41 #include "DGtal/helpers/StdDefs.h"
42 
43 //Grid curve
44 #include "DGtal/geometry/curves/FreemanChain.h"
45 #include "DGtal/geometry/curves/GridCurve.h"
46 
47 //Estimators
48 #include "DGtal/geometry/curves/BinomialConvolver.h"
49 
50 using namespace DGtal;
51 
53 namespace po = boost::program_options;
54 
55 
56 
102 int main( int argc, char** argv )
103 {
104  // parse command line ----------------------------------------------
105  po::options_description general_opt("Allowed options are: ");
106  general_opt.add_options()
107  ("help,h", "display this message")
108  ("input,i", po::value<std::string>(), "input FreemanChain file name")
109  ("GridStep,step", po::value<double>()->default_value(1.0), "Grid step");
110 
111 
112  bool parseOK=true;
113  po::variables_map vm;
114  try{
115  po::store(po::parse_command_line(argc, argv, general_opt), vm);
116  }catch(const std::exception& ex){
117  parseOK=false;
118  trace.info()<< "Error checking program options: "<< ex.what()<< std::endl;
119  }
120 
121  po::notify(vm);
122  if(!parseOK || vm.count("help")||argc<=1 || (!(vm.count("input"))) )
123  {
124  trace.info()<< "Tangent using a binomial convolver " <<std::endl << "Basic usage: "<<std::endl
125  << "\t tangentBC [options] --input <fileName> "<<std::endl
126  << general_opt << "\n"
127  << "NB: the file may contain several freeman chains." << "\n";
128  return 0;
129  }
130 
131 
132  double h = vm["GridStep"].as<double>();
133 
134 
135 
136  if(vm.count("input")){
137  std::string fileName = vm["input"].as<std::string>();
138 
139  typedef Z2i::Space Space;
140  typedef Space::Point Point;
141  typedef PointVector<2, double> RealPoint;
142  typedef Space::Integer Integer;
143  typedef FreemanChain<Integer> FreemanChain;
144  typedef std::vector< Point > Storage;
145  typedef Storage::const_iterator ConstIteratorOnPoints;
146 
147  std::vector< FreemanChain > vectFcs =
148  PointListReader< Point >:: getFreemanChainsFromFile<Integer> (fileName);
149 
150  for(unsigned int i=0; i<vectFcs.size(); i++){
151 
152  bool isClosed = vectFcs.at(i).isClosed();
153  std::cout << "# grid curve " << i << "/" << vectFcs.size() << " "
154  << ( (isClosed)?"closed":"open" ) << std::endl;
155 
156  Storage vectPts;
157  FreemanChain::getContourPoints( vectFcs.at(i), vectPts );
158 
159  // Binomial
160  std::cout << "# Curvature estimation from binomial convolution" << std::endl;
161  typedef BinomialConvolver<ConstIteratorOnPoints, double> MyBinomialConvolver;
162  std::cout << "# mask size = " <<
163  MyBinomialConvolver::suggestedSize( h, vectPts.begin(), vectPts.end() ) << std::endl;
164  typedef
165  TangentFromBinomialConvolverFunctor< MyBinomialConvolver, RealPoint >
166  TangentBCFct;
167  BinomialConvolverEstimator< MyBinomialConvolver, TangentBCFct>
168  BCTangentEstimator;
169 
170  BCTangentEstimator.init( h, vectPts.begin(), vectPts.end(), isClosed );
171 
172  std::vector<RealPoint> tangents( vectPts.size() );
173  BCTangentEstimator.eval( vectPts.begin(), vectPts.end(),
174  tangents.begin() );
175 
176  // Output
177  std::cout << "# id tangent.x tangent.y angle(atan2(y,x))" << std::endl;
178  unsigned int j = 0;
179  for ( ConstIteratorOnPoints
180  it = vectPts.begin(), it_end = vectPts.end();
181  it != it_end; ++it, ++j )
182  {
183  double x = tangents[ j ][ 0 ];
184  double y = tangents[ j ][ 1 ];
185  std::cout << j << std::setprecision( 15 )
186  << " " << x << " " << y
187  << " " << atan2( y, x )
188  << std::endl;
189  }
190 
191  }
192 
193  }
194  return 0;
195 }
196