Example #1
0
tuple image_import(const std::string filename)
{
	trace.emphase() << filename << endl;

	typedef ImageSelector<Domain, unsigned char>::Type Image;
	const Image image = GenericReader<Image>::import(filename);
	trace.info() << image << endl;

	const Point lower_bound = image.domain().lowerBound();
	const Point upper_bound = image.domain().upperBound();
	const Point delta = upper_bound-lower_bound+Point(1,1,1);
	trace.info() << lower_bound << " " << upper_bound << endl;

	const Image::ConstRange range = image.constRange();
	list flat;
	for (Image::ConstRange::ConstIterator iter=range.begin(), iter_end=range.end(); iter!=iter_end; iter++)
		flat.append(*iter);

	tuple payload = make_tuple(
			make_tuple(delta[2], delta[1], delta[0]),
			flat
			);

	return payload;
}
Example #2
0
int main( int argc, char** argv )
{
  trace.beginBlock ( "Example for ConstImageAdapter" );
  trace.info() << "Args:";
  for ( int i = 0; i < argc; ++i )
    trace.info() << " " << argv[ i ];
  trace.info() << endl;

  using namespace Z2i; 

  trace.beginBlock("Image creation");

  //domain
  const Integer size = 5; 
  Point p = Point::diagonal(0); 
  Point q = Point::diagonal(size-1); 
  Domain d(p,q); 

  //image
  typedef ImageSelector<Domain, int >::Type Image; 
  Image img(d); 

  //fill
  const int maximalValue = size*size; 
  Image::Range::OutputIterator it = img.range().outputIterator(); 
  for (int i = 0; i < maximalValue; ++i)
    *it++ = i;

  //display values 
  Image::ConstRange r = img.constRange(); 
  std::copy( r.begin(), r.end(), std::ostream_iterator<int>(cout,", ") ); 
  cout << endl; 
  trace.endBlock();
  

  const int thresholdValue = maximalValue/2; 
  trace.beginBlock("Implicit thresholding");

  //! [ConstImageAdapterConstruction]
  Thresholder<Image::Value> t( thresholdValue ); 
  ConstImageAdapter<Image, Thresholder<Image::Value>, bool> a(img, t); 
  //! [ConstImageAdapterConstruction]

  //display values 
  //! [ConstImageAdapterRange]
  ConstImageAdapter<Image, Thresholder<Image::Value>, bool>::ConstRange 
    ra = a.constRange(); 
  std::copy( ra.begin(), ra.end(), std::ostream_iterator<int>(cout,", ") ); 
  //! [ConstImageAdapterRange]
  cout << endl; 
  trace.endBlock();


  return 0;
}
/**
 * Example of a test. To be completed.
 *
 */
bool testImageFromSet()
{

  typedef ImageContainerBySTLVector<Domain,int> Image;

  unsigned int nbok = 0;
  unsigned int nb = 0;

  trace.beginBlock ( "Testing ImageFromSet ..." );

  Point a(0,0);
  Point b(23,435);
  Point c(12,12);

  Domain d(a,b);
  DigitalSet aSet(d);
  aSet.insert(c);

  Image image(d);
  //assign to the points belonging to aSet the value 128
  imageFromRangeAndValue(aSet, image, 128);
  //ie. the value of c is 128 but the value of a remains 0
  nbok += ( (image(c) == 128)&&(image(a) == 0) ) ? 1 : 0;
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;

  Image image2(d);
  Norm1<Point> n;
  //fill image2 from n
  imageFromFunctor(image2, n);
  nbok += ( (image2(c) == (int)c.norm1())
	    &&(image2(a) == (int)a.norm1())
	    &&(image2(b) == (int)b.norm1()) )? 1 : 0;
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;

  Image image3 = image;
  //fill image3 from image2
  imageFromImage(image3, const_cast<Image const&>(image2));
  //image2 and image3 should be equal,
  //but both different from image
  Image::ConstRange rimg = image.constRange();
  Image::ConstRange rimg2 = image2.constRange();
  Image::ConstRange rimg3 = image3.constRange();
  bool flag2 = std::equal(rimg.begin(), rimg.end(), rimg2.begin());
  bool flag3 = std::equal(rimg.begin(), rimg.end(), rimg3.begin());
  bool flag23 = std::equal(rimg2.begin(), rimg2.end(), rimg3.begin());
  nbok += ( (!flag2) && (!flag3) && flag23 )?1:0;
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;

  trace.endBlock();

  return nbok == nb;
}
int main( int argc, char** argv )
{
  unsigned int nbok = 0;
  unsigned int nb = 0;
    
  trace.beginBlock ( "Test for ConstImageAdapter" );
  trace.info() << "Args:";
  for ( int i = 0; i < argc; ++i )
    trace.info() << " " << argv[ i ];
  trace.info() << endl;

  using namespace Z2i; 

  trace.beginBlock("Image creation");

  //domain
  const Integer size = 5; 
  Point p = Point::diagonal(0); 
  Point q = Point::diagonal(size-1); 
  Domain d(p,q); 

  //image
  typedef ImageSelector<Domain, int >::Type Image; 
  Image img(d); 

  //fill
  const int maximalValue = size*size; 
  Image::Range::OutputIterator it = img.range().outputIterator(); 
  for (int i = 0; i < maximalValue; ++i)
    *it++ = i;

  //display values 
  Image::ConstRange r = img.constRange(); 
  std::copy( r.begin(), r.end(), std::ostream_iterator<int>(cout,", ") ); 
  cout << endl; 
  trace.endBlock();
  

  const int thresholdValue = maximalValue/2; 
  trace.beginBlock("Implicit thresholding");

  //! [ConstImageAdapterConstruction]
  DefaultFunctor g;
  Thresholder<Image::Value> t( thresholdValue );
  typedef ConstImageAdapter<Image, Domain, DefaultFunctor, bool, Thresholder<Image::Value> > MyConstImageAdapter;
  BOOST_CONCEPT_ASSERT(( CConstImage< MyConstImageAdapter > ));
  MyConstImageAdapter a(img, d, g, t); 
  //! [ConstImageAdapterConstruction]

  //display values 
  //! [ConstImageAdapterRange]
  ConstImageAdapter<Image, Domain, DefaultFunctor, bool, Thresholder<Image::Value> >::ConstRange 
    ra = a.constRange(); 
  std::copy( ra.begin(), ra.end(), std::ostream_iterator<int>(cout,", ") );
  cout << endl;
    
  std::vector<int> to_vector(25);
  std::copy(ra.begin(), ra.end(), to_vector.begin());
  for (int i = 0; i < 25; i++)
  {
    if (i<=12)
    {
     if (to_vector[i]==1)
     {
      cout << "ok, ";
      nbok += true ? 1 : 0; nb++;
     }
     else
     {
      cout << "!ok, ";
      nbok += false ? 1 : 0; nb++;
     }
    }
    else
    {
     if (to_vector[i]==0)
     {
      cout << "ok, ";
      nbok += true ? 1 : 0; nb++;
     }
     else
     {
      cout << "!ok, ";
      nbok += false ? 1 : 0; nb++;
     }
    }
  }
  //! [ConstImageAdapterRange]
  
  cout << endl; 

  trace.endBlock();

  bool res = (nbok == nb);
  
  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
  trace.endBlock();
  return res ? 0 : 1;
}
bool testSetFromImage()
{
  unsigned int nbok = 0;
  unsigned int nb = 0;

  trace.beginBlock ( "Testing SetFromImage ..." );

  //some points
  Point a(0,0);
  Point b(3,3);
  Point p(1,1);
  Point q(2,2);
  Point r(1,2);

  //image construction
  typedef ImageContainerBySTLMap<Domain,int> Image;
  Domain d(a,b);
  Image image(d,1);
  image.setValue(p,127);
  image.setValue(q,128);
  image.setValue(r,10);

  //image content
  Image::ConstRange range = image.constRange();
  std::copy(range.begin(), range.end(), ostream_iterator<Image::Value>(cout, " ") );
  cout << endl;

  //set tests

  DigitalSet aSet(d);
  DigitalSetInserter<DigitalSet> inserter(aSet);
  //all points whose value <= 126
  setFromImage( image, inserter, 126 );
  //ie, all points except p and q
  nbok += ( (aSet.find(p) == aSet.end())
	    &&(aSet.find(q) == aSet.end())
	    &&(aSet.size()==(d.size()-2)) ) ? 1 : 0;
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;

  DigitalSet aSet2(d);
  DigitalSetInserter<DigitalSet> inserter2(aSet2);
  //all points whose value <= 127
  setFromImage( image, inserter2, 127 );
  //ie, all points except q
  nbok += ( (aSet2.find(q) == aSet2.end())
	    &&(aSet2.size()==(d.size()-1)) ) ? 1 : 0;
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;

  DigitalSet aSet3(d);
  DigitalSetInserter<DigitalSet> inserter3(aSet3);
  //all points whose value <= 128
  setFromImage( image, inserter3, 128 );
  //ie, all points
  nbok += ( aSet3.size()==d.size() ) ? 1 : 0;
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;

  DigitalSet aSet4(d);
  DigitalSetInserter<DigitalSet> inserter4(aSet4);
  //all points whose value is between 2 and 100
  setFromImage( image, inserter4, 2, 100 );
  //ie, only point r
  nbok += ( (aSet4.find(r)!=aSet4.end())&&(aSet4.size()==1) ) ? 1 : 0;
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;

  DigitalSet aSet5(d);
  DigitalSetInserter<DigitalSet> inserter5(aSet5);
  //predicate construction
  typedef std::equal_to<Image::Value> EqualBinaryFunctor;
  typedef std::binder2nd<EqualBinaryFunctor> ValuePredicate;
  ValuePredicate equalTo1 (EqualBinaryFunctor(),1);
  functors::PointFunctorPredicate<Image, ValuePredicate> pred(image, equalTo1);
  //all points whose value is 1
  setFromPointsRangeAndPredicate( d.begin(), d.end(), inserter5, pred );
  //ie all points except p, q, and r
  nbok += ( (aSet5.find(p) == aSet5.end())
	    &&(aSet5.find(q) == aSet5.end())
	    &&(aSet5.find(r) == aSet5.end())
	    &&(aSet5.size()==(d.size()-3)) ) ? 1 : 0;
  nb++;
  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;


  return nbok == nb;
}