DGtalTools  0.9.4
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 #include "DGtal/io/readers/PointListReader.h"
43 
44 //Grid curve
45 #include "DGtal/geometry/curves/FreemanChain.h"
46 #include "DGtal/geometry/curves/GridCurve.h"
47 
48 //Estimators
49 #include "DGtal/geometry/curves/BinomialConvolver.h"
50 
51 using namespace DGtal;
52 
54 namespace po = boost::program_options;
55 
56 
57 
103 int main( int argc, char** argv )
104 {
105  // parse command line ----------------------------------------------
106  po::options_description general_opt("Allowed options are: ");
107  general_opt.add_options()
108  ("help,h", "display this message")
109  ("input,i", po::value<std::string>(), "input file name: FreemanChain (.fc) or a sequence of discrete points (.sdp).")
110  ("GridStep,step", po::value<double>()->default_value(1.0), "Grid step");
111 
112 
113  bool parseOK=true;
114  po::variables_map vm;
115  try{
116  po::store(po::parse_command_line(argc, argv, general_opt), vm);
117  }catch(const std::exception& ex){
118  parseOK=false;
119  trace.info()<< "Error checking program options: "<< ex.what()<< std::endl;
120  }
121 
122  po::notify(vm);
123  if(!parseOK || vm.count("help")||argc<=1 || (!(vm.count("input"))) )
124  {
125  trace.info()<< "Tangent using a binomial convolver " <<std::endl << "Basic usage: "<<std::endl
126  << "\t tangentBC [options] --input <fileName> "<<std::endl
127  << general_opt << "\n"
128  << "NB: the file may contain several freeman chains." << "\n";
129  return 0;
130  }
131 
132 
133  double h = vm["GridStep"].as<double>();
134 
135 
136 
137  if(vm.count("input")){
138  std::string fileName = vm["input"].as<std::string>();
139  std::string extension = fileName.substr( fileName.find_last_of(".") + 1 );
140  bool isSDP = extension == "sdp";
141  typedef Z2i::Space Space;
142  typedef Space::Point Point;
144  typedef Space::Integer Integer;
146  typedef std::vector< Point > Storage;
147  typedef Storage::const_iterator ConstIteratorOnPoints;
148 
149  std::vector< FreemanChain > vectFcs;
150  if(!isSDP)
151  {
152  vectFcs =
153  PointListReader< Point >:: getFreemanChainsFromFile<Integer> (fileName);
154  }
155  for(unsigned int i=0; i<vectFcs.size() || (i==0 && isSDP); i++){
156  Storage vectPts;
157  bool isClosed;
158  if(!isSDP)
159  {
160  isClosed = vectFcs.at(i).isClosed();
161  std::cout << "# grid curve " << i << "/" << vectFcs.size() << " "
162  << ( (isClosed)?"closed":"open" ) << std::endl;
163  FreemanChain::getContourPoints( vectFcs.at(i), vectPts );
164  }
165  else
166  {
168  Z2i::Point pf =vectPts[0];
169  Z2i::Point pl =vectPts[vectPts.size()-1];
170  isClosed = (pf[0]-pl[0])+(pf[1]-pl[1]) <= 1;
171  }
172 
173  // Binomial
174  std::cout << "# Curvature estimation from binomial convolution" << std::endl;
175  typedef BinomialConvolver<ConstIteratorOnPoints, double> MyBinomialConvolver;
176  std::cout << "# mask size = " <<
177  MyBinomialConvolver::suggestedSize( h, vectPts.begin(), vectPts.end() ) << std::endl;
178  typedef
180  TangentBCFct;
182  BCTangentEstimator;
183 
184  BCTangentEstimator.init( h, vectPts.begin(), vectPts.end(), isClosed );
185 
186  std::vector<RealPoint> tangents( vectPts.size() );
187  BCTangentEstimator.eval( vectPts.begin(), vectPts.end(),
188  tangents.begin() );
189 
190  // Output
191  std::cout << "# id tangent.x tangent.y angle(atan2(y,x)) x y" << std::endl;
192  unsigned int j = 0;
193  for ( ConstIteratorOnPoints
194  it = vectPts.begin(), it_end = vectPts.end();
195  it != it_end; ++it, ++j )
196  {
197  double x = tangents[ j ][ 0 ];
198  double y = tangents[ j ][ 1 ];
199  std::cout << j << std::setprecision( 15 )
200  << " " << x << " " << y
201  << " " << atan2( y, x ) << " " << (*it)[0] << " " << (*it)[1]
202  << std::endl;
203  }
204 
205  }
206 
207  }
208  return 0;
209 }
210 
DGtal::int32_t Integer
void init(const double h, const ConstIterator &itb, const ConstIterator &ite, const bool isClosed=true)
SpaceND< 2, Integer > Space
static std::vector< TPoint > getPointsFromFile(const std::string &filename, std::vector< unsigned int > aVectPosition=std::vector< unsigned int >())
Trace trace(traceWriterTerm)
std::ostream & info()
typename Self::Point Point
Quantity eval(const ConstIterator &it)
static void getContourPoints(const FreemanChain &fc, std::vector< Point > &aVContour)