DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testReverseDT.cpp
1 
30 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/images/ImageSelector.h"
35 #include "DGtal/geometry/volumes/distance/DistanceTransformation.h"
36 #include "DGtal/geometry/volumes/distance/ReverseDistanceTransformation.h"
37 #include "DGtal/io/colormaps/HueShadeColorMap.h"
38 #include "DGtal/kernel/sets/DigitalSetBySTLSet.h"
39 #include "DGtal/io/boards/Board2D.h"
40 #include "DGtal/images/imagesSetsUtils/SimpleThresholdForegroundPredicate.h"
41 
43 
44 using namespace std;
45 using namespace DGtal;
46 
47 
48 
49 template<typename Image>
50 void randomSeeds(Image &input, const unsigned int nb, const int value)
51 {
52  typename Image::Point p, low = input.lowerBound();
53  typename Image::Vector ext;
54 
55  ext = input.extent();
56 
57  for (unsigned int k = 0 ; k < nb; k++)
58  {
59  for (unsigned int dim = 0; dim < Image::dimension; dim++)
60  {
61  p[dim] = rand() % (ext[dim]) + low[dim];
62  }
63  input.setValue(p, value);
64  }
65 }
66 
67 
68 
70 // Functions for testing class ReverseDT.
72 
76 bool testReverseDT()
77 {
78  unsigned int nbok = 0;
79  unsigned int nb = 0;
80 
82 
83  trace.beginBlock ( "Testing Reverse DT in 2D ..." );
84 
85  Z2i::Point a (2, 2 );
86  Z2i::Point b ( 15, 15 );
87 
89  Image image (Z2i::Domain( a, b ));
90 
91  for ( unsigned k = 0; k < 49; k++ )
92  {
93  a[0] = ( k / 7 ) + 5;
94  a[1] = ( k % 7 ) + 5;
95  image.setValue ( a, 128 );
96  }
97 
98  a = Z2i::Point(2,2);
99 
101  Predicate aPredicate(image,0);
102 
105 
106  ImageDT result = dt.compute ( );
107  trace.info() << result<< std::endl;
108  //ReverseDT
109  trace.warning()<<"DT:"<<endl;
110  ImageDT::ConstIterator it = result.begin();
111  for (unsigned int y = 2; y < 16; y++)
112  {
113  for (unsigned int x = 2; x < 16; x++)
114  {
115  std::cout << (*it) << " ";
116  ++it;
117  }
118  std::cout << std::endl;
119  }
120 
121 
124 
125  ImageRDT reconstruction = reverseDT.reconstruction( result );
126 
127  // board.clear();
128  //drawImage<Hue>(board, result, (DGtal::int64_t)0, (DGtal::int64_t)10);
129  // board.saveSVG ( "image-REDTtest.svg" );
130 
131  trace.warning()<<"REDT:"<<endl;
132  ImageRDT::ConstIterator it2 = reconstruction.begin();
133  for (unsigned int y = 2; y < 16; y++)
134  {
135  for (unsigned int x = 2; x < 16; x++)
136  {
137  std::cout << (int)(*it2) << " ";
138  ++it2;
139  }
140  std::cout << std::endl;
141  }
142 
143  //Checking
144  bool ok=true;
145  ImageRDT::ConstIterator itrec = reconstruction.begin(), itend = reconstruction.end();
146  Image::ConstIterator itinit = image.begin();
147  for( ; itrec != itend; ++itrec,++itinit)
148  if ((*itrec) == 0)
149  ok = ok & ((*itinit) == 0);
150 
151  nbok += ok ? 1 : 0;
152  nb++;
153  trace.info() << "(" << nbok << "/" << nb << ") "
154  << "true == true" << std::endl;
155  trace.endBlock();
156 
157  return nbok == nb;
158 }
159 
160 bool testReverseDTL1()
161 {
162  unsigned int nbok = 0;
163  unsigned int nb = 0;
164 
166 
167  trace.beginBlock ( "Testing Reverse DT in 2D with L1 metric ..." );
168 
169  Z2i::Point a (2, 2 );
170  Z2i::Point b ( 15, 15 );
171 
173  Image image ( Z2i::Domain( a, b ));
174 
175  for ( unsigned k = 0; k < 49; k++ )
176  {
177  a[0] = ( k / 7 ) + 5;
178  a[1] = ( k % 7 ) + 5;
179  image.setValue ( a, 128 );
180  }
181  a = Z2i::Point(2, 2 );
182 
183 
185  Predicate aPredicate(image,0);
186 
189 
190  ImageDT result = dt.compute ( );
191 
192 
193  //ReverseDT
194  trace.warning()<<"DT:"<<endl;
195  ImageDT::ConstIterator it = result.begin();
196  for (unsigned int y = 2; y < 16; y++)
197  {
198  for (unsigned int x = 2; x < 16; x++)
199  {
200  std::cout << (int)(*it) << " ";
201  ++it;
202  }
203  std::cout << std::endl;
204  }
205 
206 
209 
210  ImageRDT reconstruction = reverseDT.reconstruction( result );
211 
212  trace.warning()<<"REDT:"<<endl;
213  ImageRDT::ConstIterator it2 = reconstruction.begin();
214  for (unsigned int y = 2; y < 16; y++)
215  {
216  for (unsigned int x = 2; x < 16; x++)
217  {
218  std::cout << (int)(*it2) << " ";
219  ++it2;
220  }
221  std::cout << std::endl;
222  }
223 
224  //Checking
225  bool ok=true;
226  ImageRDT::ConstIterator itrec = reconstruction.begin(), itend = reconstruction.end();
227  Image::ConstIterator itinit = image.begin();
228  for( ; itrec != itend; ++itrec,++itinit)
229  if ((*itrec) == 0)
230  ok = ok & ((*itinit) == 0);
231 
232  nbok += ok ? 1 : 0;
233  nb++;
234  trace.info() << "(" << nbok << "/" << nb << ") "
235  << "true == true" << std::endl;
236  trace.endBlock();
237 
238  return nbok == nb;
239 }
240 bool testReverseDTL1simple()
241 {
242  unsigned int nbok = 0;
243  unsigned int nb = 0;
244 
246 
247  trace.beginBlock ( "Testing Reverse DT in 2D with L1 metric ..." );
248 
249  Z2i::Point a (2, 2 );
250  Z2i::Point b ( 15, 15 );
251 
253 
254 
257 
258 
259  ImageDT result ( Z2i::Domain(a, b ));
260  result.setValue(Z2i::Point(5,7), 3);
261  result.setValue(Z2i::Point(9,7), 4);
262 
263  //ReverseDT
264  trace.warning()<<"DT:"<<endl;
265  ImageDT::ConstIterator it = result.begin();
266  for (unsigned int y = 2; y < 16; y++)
267  {
268  for (unsigned int x = 2; x < 16; x++)
269  {
270  std::cout << (int)(*it) << " ";
271  ++it;
272  }
273  std::cout << std::endl;
274  }
275 
276 
279 
280  ImageRDT reconstruction = reverseDT.reconstruction( result );
281 
282  trace.warning()<<"REDT:"<<endl;
283  ImageRDT::ConstIterator it2 = reconstruction.begin();
284  for (unsigned int y = 2; y < 16; y++)
285  {
286  for (unsigned int x = 2; x < 16; x++)
287  {
288  std::cout << (int)(*it2) << " ";
289  ++it2;
290  }
291  std::cout << std::endl;
292  }
293 
294 
295  nbok += true ? 1 : 0;
296  nb++;
297  trace.info() << "(" << nbok << "/" << nb << ") "
298  << "true == true" << std::endl;
299  trace.endBlock();
300 
301  return nbok == nb;
302 }
303 
308 bool testReverseDTSet()
309 {
310  unsigned int nbok = 0;
311  unsigned int nb = 0;
312 
314 
315  trace.beginBlock ( "Testing Reverse DT in 2D ..." );
316 
317  Z2i::Point a (2, 2 );
318  Z2i::Point b ( 15, 15 );
319 
321  Image image (Z2i::Domain( a, b ));
322 
323  for ( unsigned k = 0; k < 49; k++ )
324  {
325  a[0] = ( k / 7 ) + 5;
326  a[1] = ( k % 7 ) + 5;
327  image.setValue ( a, 128 );
328  }
329  a = Z2i::Point(2, 2 );
330 
331 
333  Predicate aPredicate(image,0);
334 
337 
338 
339  ImageDT result = dt.compute ( );
340 
341 
342  //ReverseDT
343  trace.warning()<<"DT:"<<endl;
344  ImageDT::ConstIterator it = result.begin();
345  for (unsigned int y = 2; y < 16; y++)
346  {
347  for (unsigned int x = 2; x < 16; x++)
348  {
349  std::cout << (*it) << " ";
350  ++it;
351  }
352  std::cout << std::endl;
353  }
354 
355 
357 
359 
360 
361  Set reconstruction(result.domain());
362  reverseDT.reconstructionAsSet<Set>( reconstruction, result );
363 
364  Board2D board;
365  board << reconstruction;
366  board.saveSVG ( "image-REDTtestSet.svg" );
367 
368  trace.warning()<<"REDT:"<<endl;
369  for(Set::ConstIterator it2 = reconstruction.begin(),
370  itend2 = reconstruction.end();
371  it2!=itend2;
372  ++it2)
373  trace.info() << (*it2) << " ";
374 
375  //Checking
376  bool ok=true;
377 
378  nbok += ok ? 1 : 0;
379  nb++;
380  trace.info() << "(" << nbok << "/" << nb << ") "
381  << "true == true" << std::endl;
382  trace.endBlock();
383 
384 
385  return nbok == nb;
386 
387 }
388 
390 // Standard services - public :
391 
392 int main( int argc, char** argv )
393 {
394  trace.beginBlock ( "Testing class ReverseDT" );
395  trace.info() << "Args:";
396  for ( int i = 0; i < argc; ++i )
397  trace.info() << " " << argv[ i ];
398  trace.info() << endl;
399 
400  bool res = testReverseDT() && testReverseDTSet()
401  && testReverseDTL1() && testReverseDTL1simple(); // && ... other tests
402 
403  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
404  trace.endBlock();
405  return res ? 0 : 1;
406 }
407 // //