Exemplo n.º 1
0
void dssLength() {
  typedef DGtal::ImageContainerBySTLVector< DGtal::Z2i::Domain, unsigned char> Image;
  std::string filename = examplesPath + "contourS.pgm";
  Image image = DGtal::PGMReader<Image>::importPGM(filename);

  typedef DGtal::functors::IntervalThresholder<Image::Value> Binarizer;
  Binarizer b(1, 135);
  DGtal::functors::PointFunctorPredicate<Image, Binarizer> predicate(image, b);

  DGtal::Z2i::KSpace ks;
  ks.init( image.domain().lowerBound(), image.domain().upperBound(), true );
  DGtal::SurfelAdjacency<2> sAdj(true);

  std::vector< std::vector< DGtal::Z2i::SCell > > contours;
  DGtal::Surfaces<DGtal::Z2i::KSpace>::extractAll2DSCellContours(contours, ks, sAdj, predicate);

  DGtal::Z2i::Curve c;
  c.initFromSCellsVector( contours.at(1) );
  
  typedef DGtal::Z2i::Curve::PointsRange Range;
  Range r = c.getPointsRange();

  DGtal::DSSLengthEstimator< Range::ConstCirculator > DSSlength;
  DSSlength.init(1, r.c(), r.c());
  DGtal::trace.info() << "Length: " << DSSlength.eval() << std::endl;
  
}
int main()
{

  //! [freemanChainFromImage-imageImport]
  typedef DGtal::ImageContainerBySTLVector< Z2i::Domain, unsigned char> Image;
  std::string filename =  examplesPath + "samples/circleR10modif.pgm";
  Image image = DGtal::PNMReader<Image>::importPGM(filename); 
  //! [freemanChainFromImage-imageImport]
  
  //![freemanChainFromImage-ksspace]
  Z2i::KSpace ks;
  ks.init( image.domain().lowerBound(), image.domain().upperBound(), true );
  //![freemanChainFromImage-ksspace]

  //! [freemanChainFromImage-setAppend]
  Z2i::DigitalSet set2d (image.domain());
  SetPredicate<Z2i::DigitalSet> set2dPredicate( set2d );
  SetFromImage<Z2i::DigitalSet>::append<Image>(set2d, image, 1, 255);
  //! [freemanChainFromImage-setAppend]


  //! [freemanChainFromImage-displaySet]
  Board2D aBoard;
  aBoard << set2d;
  aBoard << image.domain();  
  //! [freemanChainFromImage-displaySet]

  //! [freemanChainFromImage-adj]
  SurfelAdjacency<2> sAdj( true );
  //! [freemanChainFromImage-adj]

  //! [freemanChainFromImage-extraction]
  std::vector< std::vector< Z2i::Point >  >  vectContoursBdryPointels;
  Surfaces<Z2i::KSpace>::extractAllPointContours4C( vectContoursBdryPointels,
                ks, set2dPredicate, sAdj );  
  //! [freemanChainFromImage-extraction]
  

  GradientColorMap<int> cmap_grad( 0, (const int)vectContoursBdryPointels.size() );
  cmap_grad.addColor( Color( 50, 50, 255 ) );
  cmap_grad.addColor( Color( 255, 0, 0 ) );
  cmap_grad.addColor( Color( 255, 255, 10 ) );
  cmap_grad.addColor( Color( 25, 255, 255 ) );
  cmap_grad.addColor( Color( 255, 25, 255 ) );
  cmap_grad.addColor( Color( 25, 25, 25 ) );
  
  //! [freemanChainFromImage-fcConstruction]
  for(unsigned int i=0; i<vectContoursBdryPointels.size(); i++){
    //  Constructing and displaying FreemanChains from contours. 
    FreemanChain<Z2i::Integer> fc (vectContoursBdryPointels.at(i));    
    //! [freemanChainFromImage-fcConstruction]
    //! [freemanChainFromImage-fcdysplay]
    aBoard << SetMode( fc.className(), "InterGrid" );
    aBoard<< CustomStyle( fc.className(), 
            new CustomColors(  cmap_grad(i),  Color::None ) );        
    aBoard << fc;
    //! [freemanChainFromImage-fcdysplay]
  }   

  aBoard.saveEPS("freemanChainFromImage.eps");
  return 0;
}
int main()
{
  //shape
  typedef Flower2D<Z2i::Space> Flower; 
  Flower2D<Z2i::Space> flower(Z2i::Point(0,0), 20, 5, 5, 0);
  
  //! [shapeGridCurveEstimator-dig]
  //implicit digitization of a shape of type Flower 
  //into a digital space of type Space
  double h = 1; 
  GaussDigitizer<Z2i::Space,Flower> dig;  
  dig.attach( flower );
  dig.init( flower.getLowerBound()+Z2i::Vector(-1,-1),
            flower.getUpperBound()+Z2i::Vector(1,1), h ); 
  //! [shapeGridCurveEstimator-dig]
  
  //! [shapeGridCurveEstimator-prepareTracking]
  //Khalimsky space
  Z2i::KSpace ks;
  ks.init( dig.getLowerBound(), dig.getUpperBound(), true );
  //adjacency (4-connectivity)
  SurfelAdjacency<2> sAdj( true );
  //! [shapeGridCurveEstimator-prepareTracking]

  //! [shapeGridCurveEstimator-tracking]
  //searching for one boundary element
  Z2i::SCell bel = Surfaces<Z2i::KSpace>::findABel( ks, dig, 1000 );
  //tracking
  vector<Z2i::Point> boundaryPoints;
  Surfaces<Z2i::KSpace>
    ::track2DBoundaryPoints( boundaryPoints, ks, sAdj, dig, bel );
  //! [shapeGridCurveEstimator-tracking]

  //! [shapeGridCurveEstimator-instantiation]
  Z2i::Curve c;
  c.initFromVector( boundaryPoints );  
  //! [shapeGridCurveEstimator-instantiation]
  
  DGtal::Board2D aBoard;
  aBoard << c; 
  aBoard.saveEPS("DisplayGridCurve1.eps");  
  
  //! [shapeGridCurveEstimator-getRange]
  //range of points
  typedef Z2i::Curve::PointsRange Range; 
  Range r = c.getPointsRange(); 
  //! [shapeGridCurveEstimator-getRange]
  
  //! [shapeGridCurveEstimator-lengthEstimation]
  //length estimation
  DSSLengthEstimator< Range::ConstIterator > DSSlength;
  DSSlength.init( h, r.begin(), r.end(), c.isClosed() );
  double length1 = DSSlength.eval();
  trace.info() << "Length (h=" << h << "): " << length1 << endl; 
  //! [shapeGridCurveEstimator-lengthEstimation]

//@TODO correct init method of trueLengthEstimator (remove &flower)
  //! [shapeGridCurveEstimator-trueLengthEstimation]
  typedef ParametricShapeArcLengthFunctor< Flower > Length;
  TrueGlobalEstimatorOnPoints< 
    Range::ConstIterator, 
    Flower, 
    Length  >  trueLengthEstimator;
  trueLengthEstimator.init( h, r.begin(), r.end(), &flower, c.isClosed());
  double trueLength = trueLengthEstimator.eval(); 
  trace.info() << "ground truth: " << trueLength << endl; 
  //! [shapeGridCurveEstimator-trueLengthEstimation]

  //! [shapeGridCurveEstimator-higher]
  //implicit digitization at higher resolution
  h = 0.1; 
  dig.init( flower.getLowerBound()+Z2i::Vector(-1,-1),
            flower.getUpperBound()+Z2i::Vector(1,1), h ); 
  //a greater domain is needed in the Khalimsky space
  ks.init( dig.getLowerBound(), dig.getUpperBound(), true );
  //searching for one boundary element
  bel = Surfaces<Z2i::KSpace>::findABel( ks, dig, 10000 );
  //tracking
  Surfaces<Z2i::KSpace>
    ::track2DBoundaryPoints( boundaryPoints, ks, sAdj, dig, bel );
  //reset grid curve and its points range
  c.initFromVector( boundaryPoints );
  Range r2 = c.getPointsRange(); 
  //estimate length
  DSSlength.init( h, r2.begin(), r2.end(), c.isClosed() );
  double length2 = DSSlength.eval();
  trace.info() << "Length (h=" << h << "): " << length2 << endl;  
  //! [shapeGridCurveEstimator-higher]
  
  aBoard.clear(); 
  aBoard << c; 
  aBoard.saveEPS("DisplayGridCurve01.eps");  
  
  return 0;

}