bool
compareShapeEstimators( const string & name,
      Shape & aShape, 
      double h )
{
  // Types
  typedef typename Space::Point Point;
  typedef typename Space::Vector Vector;
  typedef typename Space::RealPoint RealPoint;
  typedef typename Space::Integer Integer;
  typedef HyperRectDomain<Space> Domain;
  typedef KhalimskySpaceND<Space::dimension,Integer> KSpace;
  typedef typename KSpace::SCell SCell;
  typedef typename GridCurve<KSpace>::PointsRange PointsRange;
  typedef typename GridCurve<KSpace>::ArrowsRange ArrowsRange;
  typedef typename PointsRange::ConstIterator ConstIteratorOnPoints;


  // Digitizer
  GaussDigitizer<Space,Shape> dig;  
  dig.attach( aShape ); // attaches the shape.
  Vector vlow(-1,-1); Vector vup(1,1);
  dig.init( aShape.getLowerBound()+vlow, aShape.getUpperBound()+vup, h ); 
  Domain domain = dig.getDomain();

  // Create cellular space
  KSpace K;
  bool ok = K.init( dig.getLowerBound(), dig.getUpperBound(), true );
  if ( ! ok )
    {
      std::cerr << "[compareShapeEstimators]"
    << " error in creating KSpace." << std::endl;
      return false;
    }
  try {
    // Extracts shape boundary
    SurfelAdjacency<KSpace::dimension> SAdj( true );
    SCell bel = Surfaces<KSpace>::findABel( K, dig, 10000 );
    // Getting the consecutive surfels of the 2D boundary
    std::vector<Point> points;
    Surfaces<KSpace>::track2DBoundaryPoints( points, K, SAdj, dig, bel );
    // Create GridCurve
    GridCurve<KSpace> gridcurve;
    gridcurve.initFromVector( points );
    // Ranges
    PointsRange r = gridcurve.getPointsRange(); 
    std::cout << "# range size = " << r.size() << std::endl;  

    // Estimations
    // True values
    std::cout << "# True values computation" << std::endl;  
    typedef ParametricShapeTangentFunctor< Shape > TangentFunctor;
    typedef ParametricShapeCurvatureFunctor< Shape > CurvatureFunctor;
  
    TrueLocalEstimatorOnPoints< ConstIteratorOnPoints, Shape, TangentFunctor >  
      trueTangentEstimator;
    TrueLocalEstimatorOnPoints< ConstIteratorOnPoints, Shape, CurvatureFunctor >  
      trueCurvatureEstimator;
  
    trueTangentEstimator.init( h, r.begin(), r.end(), &aShape, gridcurve.isClosed());
    std::vector<RealPoint> trueTangents = 
      estimateQuantity( trueTangentEstimator, r.begin(), r.end() );
    trueCurvatureEstimator.init( h, r.begin(), r.end(), &aShape, gridcurve.isClosed());
    std::vector<double> trueCurvatures = 
      estimateQuantity( trueCurvatureEstimator, r.begin(), r.end() );
  
    // Maximal Segments
    std::cout << "# Maximal DSS tangent estimation" << std::endl;  
    typedef ArithmeticalDSS<ConstIteratorOnPoints,Integer,4> SegmentComputer;
    typedef TangentFromDSSFunctor<SegmentComputer> SCFunctor;
    SegmentComputer sc;
    SCFunctor f; 
    MostCenteredMaximalSegmentEstimator<SegmentComputer,SCFunctor> MSTangentEstimator(sc, f); 
   
    Clock c;
    
    c.startClock();
    MSTangentEstimator.init( h, r.begin(), r.end(), gridcurve.isClosed() );
    std::vector<typename SCFunctor::Value> MSTangents = 
      estimateQuantity( MSTangentEstimator, r.begin(), r.end() );
    double TMST = c.stopClock();


    // Binomial
    std::cout << "# Tangent and curvature estimation from binomial convolution" << std::endl;
    typedef BinomialConvolver<ConstIteratorOnPoints, double> MyBinomialConvolver;
    std::cout << "# mask size = " << 
      MyBinomialConvolver::suggestedSize( h, r.begin(), r.end() ) << std::endl;
    typedef TangentFromBinomialConvolverFunctor< MyBinomialConvolver, RealPoint >
      TangentBCFct;
    typedef CurvatureFromBinomialConvolverFunctor< MyBinomialConvolver, double >
      CurvatureBCFct;
    BinomialConvolverEstimator< MyBinomialConvolver, TangentBCFct> BCTangentEstimator;
    BinomialConvolverEstimator< MyBinomialConvolver, CurvatureBCFct> BCCurvatureEstimator;
    
    c.startClock();
    BCTangentEstimator.init( h, r.begin(), r.end(), gridcurve.isClosed() );
    std::vector<RealPoint> BCTangents = 
      estimateQuantity( BCTangentEstimator, r.begin(), r.end() );
    double TBCTan = c.stopClock();

    c.startClock();
    BCCurvatureEstimator.init( h, r.begin(), r.end(), gridcurve.isClosed() );
    std::vector<double> BCCurvatures =
      estimateQuantity( BCCurvatureEstimator, r.begin(), r.end() );
    double TBCCurv = c.stopClock();

    // Output
    std::cout << "# Shape = "<< name <<std::endl
        << "# Time-BCtangent = "<<TBCTan <<std::endl
        << "# Time-BCcurvature = "<<TBCCurv<<std::endl
        << "# Time-MStangent = "<<TMST<<std::endl
        << "# id x y tangentx tangenty curvature"
        << " BCtangentx BCtangenty BCcurvature"
        << " MStangentx MStangenty"
        << std::endl;  
    unsigned int i = 0;
    for ( ConstIteratorOnPoints it = r.begin(), it_end = r.end();
    it != it_end; ++it, ++i )
      {
  Point p = *it;
  std::cout << i << setprecision( 15 )
      << " " << p[ 0 ] << " " << p[ 1 ] 
      << " " << trueTangents[ i ][ 0 ]
      << " " << trueTangents[ i ][ 1 ]
      << " " << trueCurvatures[ i ]
      << " " << BCTangents[ i ][ 0 ]
      << " " << BCTangents[ i ][ 1 ]
      << " " << BCCurvatures[ i ]
      << " " << MSTangents[ i ][ 0 ]
      << " " << MSTangents[ i ][ 1 ]
      << std::endl;
      }
    return true;
  }    
  catch ( InputException e )
    {
      std::cerr << "[compareShapeEstimators]"
    << " error in finding a bel." << std::endl;
      return false;
    }
}
/**
 * Example of a test. To be completed.
 *
 */
bool testBinomialConvolver()
{
  unsigned int nbok = 0;
  unsigned int nb = 0;
  
  trace.beginBlock ( "Testing block ..." );
  typedef PointVector<2, double> RealPoint;
  std::vector< RealPoint > points;
#ifdef CPP11_INITIALIZER_LIST
  points.push_back( RealPoint( { 0.0, 0.0 } ) ); 
  points.push_back( RealPoint( { 1.0, 0.0 } ) ); 
  points.push_back( RealPoint( { 2.0, 0.0 } ) ); 
  points.push_back( RealPoint( { 2.0, 1.0 } ) ); 
  points.push_back( RealPoint( { 2.0, 2.0 } ) ); 
  points.push_back( RealPoint( { 1.0, 2.0 } ) ); 
  points.push_back( RealPoint( { 0.0, 2.0 } ) ); 
  points.push_back( RealPoint( { 0.0, 1.0 } ) ); 
#else
   points.push_back( RealPoint(  0.0, 0.0  ) ); 
  points.push_back( RealPoint(  1.0, 0.0  ) ); 
  points.push_back( RealPoint(  2.0, 0.0  ) ); 
  points.push_back( RealPoint(  2.0, 1.0  ) ); 
  points.push_back( RealPoint( 2.0, 2.0  ) ); 
  points.push_back( RealPoint( 1.0, 2.0  ) ); 
  points.push_back( RealPoint(  0.0, 2.0  ) ); 
  points.push_back( RealPoint(  0.0, 1.0  ) ); 
#endif
  
  typedef std::vector< RealPoint >::const_iterator ConstIteratorOnPoints;
  typedef BinomialConvolver<ConstIteratorOnPoints, double> MyBinomialConvolver;
  
  for ( unsigned int n = 1; n < 10; ++n )
    {
      trace.info() << "Binomial convolver n=" << n << std::endl;
      MyBinomialConvolver bcc( n );
      bcc.init( 1.0, points.begin(), points.end(), true );
      for ( unsigned int i = 0; i < 8; ++i )
        std::cout << i
            << " " << bcc.x( i ).first
            << " " << bcc.x( i ).second
            << " " << bcc.tangent( i ).first
            << " " << bcc.tangent( i ).second
            << " " << bcc.curvature( i )
            << std::endl;
    }
  unsigned int n = MyBinomialConvolver::suggestedSize( 1.0, points.begin(), points.end() );
  trace.info() << "Binomial convolver suggested n=" 
         << n
         << std::endl;

  typedef TangentFromBinomialConvolverFunctor< MyBinomialConvolver, RealPoint >
    TangentBCFct;
  typedef CurvatureFromBinomialConvolverFunctor< MyBinomialConvolver, double >
    CurvatureBCFct;
  BinomialConvolverEstimator< MyBinomialConvolver, TangentBCFct> tgtEstimator;
  BinomialConvolverEstimator< MyBinomialConvolver, CurvatureBCFct> curvEstimator;
  tgtEstimator.init( 1.0, points.begin(), points.end(), true );
  curvEstimator.init( 1.0, points.begin(), points.end(), true );
  for ( ConstIteratorOnPoints it = points.begin(), it_end = points.end();
  it != it_end; ++it )
    {
      std::cout << *it 
    << " " << tgtEstimator.eval( it ) 
    << " " << curvEstimator.eval( it ) 
    << std::endl;
    }
  nbok += true ? 1 : 0; 
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") "
         << "true == true" << std::endl;
  trace.endBlock();
  
  return nbok == nb;
}
Exemple #3
0
int main( int argc, char** argv )
{
  // parse command line ----------------------------------------------
  po::options_description general_opt("Allowed options are: ");
  general_opt.add_options()
    ("help,h", "display this message")
    ("FreemanChain,f", po::value<std::string>(), "FreemanChain file name")
    ("GridStep,step", po::value<double>()->default_value(1.0), "Grid step");
  
  
  
  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, general_opt), vm);  
  po::notify(vm);    
  if(vm.count("help")||argc<=1 || (not(vm.count("FreemanChain"))) )
    {
      trace.info()<< "Tangent using a binomial convolver " <<std::endl << "Basic usage: "<<std::endl
		  << "\t tangentBC [options] --FreemanChain  <fileName> "<<std::endl
		  << general_opt << "\n"
		  << "NB: the file may contain several freeman chains." << "\n";
      return 0;
    }
  
  
  double h = vm["GridStep"].as<double>();  


 
  if(vm.count("FreemanChain")){
    string fileName = vm["FreemanChain"].as<string>();

    typedef Z2i::Space Space; 
    typedef Space::Point Point; 
    typedef RealPointVector<2> RealPoint; 
    typedef Space::Integer Integer;  
    typedef FreemanChain<Integer> FreemanChain; 
    typedef vector< Point > Storage;
    typedef Storage::const_iterator ConstIteratorOnPoints; 

    vector< FreemanChain > vectFcs =  
      PointListReader< Point >:: getFreemanChainsFromFile<Integer> (fileName); 

    for(unsigned int i=0; i<vectFcs.size(); i++){

      bool isClosed = vectFcs.at(i).isClosed(); 
      cout << "# grid curve " << i << "/" << vectFcs.size() << " "
      << ( (isClosed)?"closed":"open" ) << endl;

      Storage vectPts; 
      FreemanChain::getContourPoints( vectFcs.at(i), vectPts ); 

      // Binomial
      std::cout << "# Curvature estimation from binomial convolution" << std::endl;
      typedef BinomialConvolver<ConstIteratorOnPoints, double> MyBinomialConvolver;
      std::cout << "# mask size = " << 
      MyBinomialConvolver::suggestedSize( h, vectPts.begin(), vectPts.end() ) << std::endl;
      typedef 
	TangentFromBinomialConvolverFunctor< MyBinomialConvolver, RealPoint >
	TangentBCFct;
      BinomialConvolverEstimator< MyBinomialConvolver, TangentBCFct> 
	BCTangentEstimator;

      BCTangentEstimator.init( h, vectPts.begin(), vectPts.end(), isClosed );

      vector<RealPoint> tangents( vectPts.size() ); 
      BCTangentEstimator.eval( vectPts.begin(), vectPts.end(), 
			       tangents.begin() ); 

      // Output
      cout << "# id tangent.x tangent.y angle(atan2(y,x))" << endl;  
      unsigned int j = 0;
      for ( ConstIteratorOnPoints 
	      it = vectPts.begin(), it_end = vectPts.end();
	    it != it_end; ++it, ++j ) 
	{
	  double x = tangents[ j ][ 0 ];
	  double y = tangents[ j ][ 1 ];
	  cout << j << setprecision( 15 )
	       << " " << x << " " << y 
	       << " " << atan2( y, x )
	       << endl;
	}

    }

  }
  return 0;
}
Exemple #4
0
int main( int argc, char** argv )
{
  // parse command line ----------------------------------------------
  po::options_description general_opt("Allowed options are: ");
  general_opt.add_options()
    ("help,h", "display this message")
    ("FreemanChain,f", po::value<std::string>(), "FreemanChain file name")
    ("GridStep,step", po::value<double>()->default_value(1.0), "Grid step");
  
  
  bool parseOK=true;
  po::variables_map vm;
  try{
    po::store(po::parse_command_line(argc, argv, general_opt), vm);  
  }catch(const std::exception& ex){
    parseOK=false;
    trace.info()<< "Error checking program options: "<< ex.what()<< endl;
  }
  po::notify(vm);    
  if(!parseOK || vm.count("help")||argc<=1 || (!(vm.count("FreemanChain"))) )
    {
      trace.info()<< "Curvature using a binomial convolver " <<std::endl << "Basic usage: "<<std::endl
      << "\t curvatureBC [options] --FreemanChain  <fileName> "<<std::endl
      << general_opt << "\n";
      return 0;
    }
  
  
  double h = vm["GridStep"].as<double>();  


 
  if(vm.count("FreemanChain")){
    string fileName = vm["FreemanChain"].as<string>();

    typedef Z2i::Space Space; 
    typedef Space::Point Point; 
    typedef Space::Integer Integer;  
    typedef FreemanChain<Integer> FreemanChain; 
    typedef vector< Point > Storage;
    typedef Storage::const_iterator ConstIteratorOnPoints; 

    vector< FreemanChain > vectFcs =  PointListReader< Point >:: getFreemanChainsFromFile<Integer> (fileName); 
   

    for(unsigned int i=0; i<vectFcs.size(); i++){

      bool isClosed = vectFcs.at(i).isClosed(); 
      cout << "# grid curve " << i+1 << "/" << vectFcs.size() << " "
      << ( (isClosed)?"closed":"open" ) << endl;

      Storage vectPts; 
      FreemanChain::getContourPoints( vectFcs.at(i), vectPts ); 

      // Binomial
      std::cout << "# Curvature estimation from binomial convolution" << std::endl;
      typedef BinomialConvolver<ConstIteratorOnPoints, double> MyBinomialConvolver;
      std::cout << "# mask size = " << 
      MyBinomialConvolver::suggestedSize( h, vectPts.begin(), vectPts.end() ) << std::endl;
      typedef CurvatureFromBinomialConvolverFunctor< MyBinomialConvolver, double >
      CurvatureBCFct;
      BinomialConvolverEstimator< MyBinomialConvolver, CurvatureBCFct> BCCurvatureEstimator;

      BCCurvatureEstimator.init( h, vectPts.begin(), vectPts.end(), isClosed );

      vector <double> curvatures( vectPts.size() ); 
      BCCurvatureEstimator.eval( vectPts.begin(), vectPts.end(), curvatures.begin() ); 

      // Output
      cout << "# id curvature" << endl;  
      unsigned int j = 0;
      for ( ConstIteratorOnPoints it = vectPts.begin(), it_end = vectPts.end();
      it != it_end; ++it, ++j ) {
  cout << j << setprecision( 15 )
       << " " << curvatures[ j ] << endl;
      }

   }

 }
 
  return 0;
}