DGtalTools  0.9.4
curvatureScaleSpaceBCC.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 
44 //Estimators
45 #include "DGtal/geometry/curves/BinomialConvolver.h"
46 
47 
48 // Generation of resulting image
49 #include "DGtal/images/ImageContainerBySTLVector.h"
50 #include "DGtal/io/writers/GenericWriter.h"
51 #include "DGtal/io/colormaps/GradientColorMap.h"
52 #include "DGtal/io/writers/PPMWriter.h"
53 
54 
55 #include <boost/program_options/options_description.hpp>
56 #include <boost/program_options/parsers.hpp>
57 #include <boost/program_options/variables_map.hpp>
58 
59 #include <vector>
60 #include <string>
61 #include <iomanip>
62 
63 
64 using namespace DGtal;
65 
66 
67 
107 void
108 computeCurvatureBCC(double h, const FreemanChain<Z2i::Integer> &fc, std::vector<double> &resCurvature,
109  bool isClosed){
110  std::vector<Z2i::Point> vectPoints;
112 
113  typedef BinomialConvolver<std::vector<Z2i::Point>::const_iterator, double> MyBinomialConvolver;
116  BCCurvatureEstimator.init( h, vectPoints.begin(), vectPoints.end(), isClosed );
117  resCurvature.clear();
118  resCurvature.resize(vectPoints.size());
119  BCCurvatureEstimator.eval( vectPoints.begin(), vectPoints.end(), resCurvature.begin() );
120 }
121 
122 
123 
125 namespace po = boost::program_options;
126 
127 int main( int argc, char** argv )
128 {
129 
130  // parse command line ----------------------------------------------
131  po::options_description general_opt("Allowed options are: ");
132  general_opt.add_options()
133  ("help,h", "display this message")
134  ("input,i", po::value<std::string>(), "Input FreemanChain file name")
135  ("gridStepInit", po::value<double>()->default_value(1.0), "Grid step initial")
136  ("gridStepIncrement", po::value<double>()->default_value(1.0), "Grid step increment ")
137  ("output,o ", po::value<std::string>(), "set the output name ")
138  ("gridStepFinal", po::value<double>()->default_value(1.0), "Grid step final")
139  ("curvatureCutOff,c", po::value<double>()->default_value(10.0), "set the curvature limits to better display");
140 
141 
143 
144 
145  bool parseOK=true;
146  po::variables_map vm;
147  try{
148  po::store(po::parse_command_line(argc, argv, general_opt), vm);
149  }catch(const std::exception& ex){
150  parseOK=false;
151  trace.info()<< "Error checking program options: "<< ex.what()<< std::endl;
152  }
153  po::notify(vm);
154  if(!parseOK || vm.count("help")||argc<=1 || (!(vm.count("input"))) )
155  {
156  trace.info()<< "Generate the Curvature Scale Space image using a binomial convolver based estimator." <<std::endl
157  << "The x axis is associated to the contour point and the y axis to the scale. The colors represent the curvature values included between the cutoff values (set to 10 by default)."
158  <<std::endl << "Basic usage: "<<std::endl
159  << "\t curvatureScaleSpaceBCC --input <filename> --output <filename> "<<std::endl
160  << general_opt << "\n"
161  << "Example: "<<std::endl
162  << "\t curvatureScaleSpaceBCC -i ${DGtal}/examples/samples/contourS.fc --gridStepInit 0.001 --gridStepIncrement 0.0005 --gridStepFinal 0.05 -o cssResu.ppm"<< std::endl;
163 
164  return 0;
165  }
166  double h_initial = vm["gridStepInit"].as<double>();
167  double h_increment = vm["gridStepIncrement"].as<double>();
168  double h_final = vm["gridStepFinal"].as<double>();
169  double curvatureCutOff = vm["curvatureCutOff"].as<double>();
170 
171 
172  if(vm.count("input")){
173  std::string fileName = vm["input"].as<std::string>();
174  std::vector< DGtal::FreemanChain<Z2i::Integer> > vectFcs = PointListReader< Z2i::Point >:: getFreemanChainsFromFile<Z2i::Integer> (fileName);
175  bool isClosed = vectFcs.at(0).isClosed();
176 
177 
178  // Preparing resulting image:
179  unsigned int height = (int)((h_final-h_initial)/h_increment);
180  // We add one point if the freemnchain is open.
181  Z2i::Domain domain (Z2i::Point(0,0), Z2i::Point(vectFcs.at(0).size()+(isClosed? 0: 1), height-1));
182  Image2D cssImage(domain);
183  HueShadeColorMap<double> gradCurvature (-curvatureCutOff, curvatureCutOff);
184 
185  trace.progressBar(0, height);
186  double h= h_initial;
187  for(double l= 0; l < height; l++ ){
188  // Binomial estimator
189  trace.progressBar(l, height);
190  std::vector<double> curvaturesBCC;
191  computeCurvatureBCC(h, vectFcs.at(0), curvaturesBCC, isClosed);
192  // Output
193  unsigned int j = 0;
194  for ( std::vector<double>::const_iterator it = curvaturesBCC.begin(), it_end = curvaturesBCC.end();
195  it != it_end; ++it, ++j ) {
196  double c = *it;
197  c = c<-curvatureCutOff? -curvatureCutOff: c;
198  c = c>curvatureCutOff? curvatureCutOff: c;
199  cssImage.setValue(Z2i::Point(j, l), c);
200  }
201  h=h+h_increment;
202  }
203  trace.progressBar(height, height);
204  trace.info() <<std::endl;
205  DGtal::GenericWriter<Image2D, 2, double, HueShadeColorMap<double> >::exportFile(vm["output"].as<std::string>(), cssImage, gradCurvature );
206  }
207  return 0;
208 }
209 
void progressBar(const double currentValue, const double maximalValue)
void init(const double h, const ConstIterator &itb, const ConstIterator &ite, const bool isClosed=true)
Trace trace(traceWriterTerm)
std::ostream & info()
Quantity eval(const ConstIterator &it)
static void getContourPoints(const FreemanChain &fc, std::vector< Point > &aVContour)