DGtal  1.5.beta
testImagesSetsUtilities.cpp
Go to the documentation of this file.
1 
36 #include <iostream>
37 #include "DGtal/base/Common.h"
38 
39 #include "DGtal/images/imagesSetsUtils/ImageFromSet.h"
40 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
41 
42 #include "DGtal/kernel/sets/DigitalSetInserter.h"
43 #include "DGtal/images/ImageHelper.h"
44 
45 #include "DGtal/images/ImageContainerBySTLMap.h"
46 #include "DGtal/images/ImageContainerBySTLVector.h"
47 #include "DGtal/images/ImageSelector.h"
48 
49 #include "DGtal/helpers/StdDefs.h"
50 
51 #include "ConfigTest.h"
52 
54 
55 using namespace std;
56 using namespace DGtal;
57 using namespace Z2i;
58 
60 // Functions for testing ImageHelper functions.
62 
63 template <typename P>
64 struct Norm1
65 {
66  typedef P Point;
67  typedef typename P::Coordinate Value;
68 
69  Value operator()(const Point& p)
70  {
71  return p.norm1();
72  }
73 };
74 
80 {
81 
83 
84  unsigned int nbok = 0;
85  unsigned int nb = 0;
86 
87  trace.beginBlock ( "Testing ImageFromSet ..." );
88 
89  Point a(0,0);
90  Point b(23,435);
91  Point c(12,12);
92 
93  Domain d(a,b);
94  DigitalSet aSet(d);
95  aSet.insert(c);
96 
97  Image image(d);
98  //assign to the points belonging to aSet the value 128
99  imageFromRangeAndValue(aSet, image, 128);
100  //ie. the value of c is 128 but the value of a remains 0
101  nbok += ( (image(c) == 128)&&(image(a) == 0) ) ? 1 : 0;
102  nb++;
103  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
104 
105  Image image2(d);
106  Norm1<Point> n;
107  //fill image2 from n
108  imageFromFunctor(image2, n);
109  nbok += ( (image2(c) == (int)c.norm1())
110  &&(image2(a) == (int)a.norm1())
111  &&(image2(b) == (int)b.norm1()) )? 1 : 0;
112  nb++;
113  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
114 
115  Image image3 = image;
116  //fill image3 from image2
117  imageFromImage(image3, const_cast<Image const&>(image2));
118  //image2 and image3 should be equal,
119  //but both different from image
120  Image::ConstRange rimg = image.constRange();
121  Image::ConstRange rimg2 = image2.constRange();
122  Image::ConstRange rimg3 = image3.constRange();
123  bool flag2 = std::equal(rimg.begin(), rimg.end(), rimg2.begin());
124  bool flag3 = std::equal(rimg.begin(), rimg.end(), rimg3.begin());
125  bool flag23 = std::equal(rimg2.begin(), rimg2.end(), rimg3.begin());
126  nbok += ( (!flag2) && (!flag3) && flag23 )?1:0;
127  nb++;
128  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
129 
130  trace.endBlock();
131 
132  return nbok == nb;
133 }
134 
136 {
137  unsigned int nbok = 0;
138  unsigned int nb = 0;
139 
140  trace.beginBlock ( "Testing SetFromImage ..." );
141 
142  //some points
143  Point a(0,0);
144  Point b(3,3);
145  Point p(1,1);
146  Point q(2,2);
147  Point r(1,2);
148 
149  //image construction
151  Domain d(a,b);
152  Image image(d,1);
153  image.setValue(p,127);
154  image.setValue(q,128);
155  image.setValue(r,10);
156 
157  //image content
158  Image::ConstRange range = image.constRange();
159  std::copy(range.begin(), range.end(), ostream_iterator<Image::Value>(cout, " ") );
160  cout << endl;
161 
162  //set tests
163 
164  DigitalSet aSet(d);
165  DigitalSetInserter<DigitalSet> inserter(aSet);
166  //all points whose value <= 126
167  setFromImage( image, inserter, 126 );
168  //ie, all points except p and q
169  nbok += ( (aSet.find(p) == aSet.end())
170  &&(aSet.find(q) == aSet.end())
171  &&(aSet.size()==(d.size()-2)) ) ? 1 : 0;
172  nb++;
173  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
174 
175  DigitalSet aSet2(d);
176  DigitalSetInserter<DigitalSet> inserter2(aSet2);
177  //all points whose value <= 127
178  setFromImage( image, inserter2, 127 );
179  //ie, all points except q
180  nbok += ( (aSet2.find(q) == aSet2.end())
181  &&(aSet2.size()==(d.size()-1)) ) ? 1 : 0;
182  nb++;
183  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
184 
185  DigitalSet aSet3(d);
186  DigitalSetInserter<DigitalSet> inserter3(aSet3);
187  //all points whose value <= 128
188  setFromImage( image, inserter3, 128 );
189  //ie, all points
190  nbok += ( aSet3.size()==d.size() ) ? 1 : 0;
191  nb++;
192  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
193 
194  DigitalSet aSet4(d);
195  DigitalSetInserter<DigitalSet> inserter4(aSet4);
196  //all points whose value is between 2 and 100
197  setFromImage( image, inserter4, 2, 100 );
198  //ie, only point r
199  nbok += ( (aSet4.find(r)!=aSet4.end())&&(aSet4.size()==1) ) ? 1 : 0;
200  nb++;
201  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
202 
203  DigitalSet aSet5(d);
204  DigitalSetInserter<DigitalSet> inserter5(aSet5);
205  //predicate construction
206  using ValuePredicate = std::function<bool(const Image::Value &)>;
207  ValuePredicate equalTo1 = [](const Image::Value & v)
208  {
209  return v == 1;
210  };
212  //all points whose value is 1
213  setFromPointsRangeAndPredicate( d.begin(), d.end(), inserter5, pred );
214  //ie all points except p, q, and r
215  nbok += ( (aSet5.find(p) == aSet5.end())
216  &&(aSet5.find(q) == aSet5.end())
217  &&(aSet5.find(r) == aSet5.end())
218  &&(aSet5.size()==(d.size()-3)) ) ? 1 : 0;
219  nb++;
220  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
221 
222 
223  return nbok == nb;
224 }
225 
227 // Standard services - public :
228 
229 int main( int argc, char** argv )
230 {
231  trace.beginBlock ( "Testing class ImageHelper functions" );
232  trace.info() << "Args:";
233  for ( int i = 0; i < argc; ++i )
234  trace.info() << " " << argv[ i ];
235  trace.info() << endl;
236 
237  bool res = testImageFromSet() && testSetFromImage();
238  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
239  trace.endBlock();
240  return res ? 0 : 1;
241 }
242 // //
Aim: A wrapper class around a STL associative container for storing sets of digital points within som...
ConstIterator find(const Point &p) const
Aim: this output iterator class is designed to allow algorithms to insert points in the digital set....
const ConstIterator & end() const
const ConstIterator & begin() const
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:70
ConstRange constRange() const
Definition: Image.h:203
Aim: model of CConstBidirectionalRangeFromPoint that adapts any range of elements bounded by two iter...
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
void setFromImage(const I &aImg, const O &ito, const typename I::Value &aThreshold=0)
void imageFromFunctor(I &aImg, const F &aFun)
Trace trace
Definition: Common.h:153
void imageFromImage(I1 &aImg1, const I2 &aImg2)
void imageFromRangeAndValue(const It &itb, const It &ite, Im &aImg, const typename Im::Value &aValue=0)
void setFromPointsRangeAndPredicate(const I &itb, const I &ite, const O &ito, const P &aPred)
useful functions
Aim: The predicate returns true when the predicate returns true for the value assigned to a given poi...
MyPointD Point
Definition: testClone2.cpp:383
int main(int argc, char **argv)
bool testImageFromSet()
bool testSetFromImage()
Image image(domain)
ImageContainerBySTLVector< Domain, Value > Image