DGtal  0.9.2
testReverseDT.cpp
1
30 #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/ExactPredicateLpSeparableMetric.h"
37 #include "DGtal/geometry/volumes/distance/ReverseDistanceTransformation.h"
39 #include "DGtal/kernel/sets/DigitalSetBySTLSet.h"
40 #include "DGtal/io/boards/Board2D.h"
41 #include "DGtal/images/SimpleThresholdForegroundPredicate.h"
43
44 using namespace std;
45 using namespace DGtal;
46 using namespace DGtal::functors;
47
48
49
50 template<typename Image>
51 void randomSeeds(Image &input, const unsigned int nb, const int value)
52 {
53  typename Image::Point p, low = input.lowerBound();
54  typename Image::Vector ext;
55
56  ext = input.extent();
57
58  for (unsigned int k = 0 ; k < nb; k++)
59  {
60  for (unsigned int dim = 0; dim < Image::dimension; dim++)
61  {
62  p[dim] = rand() % (ext[dim]) + low[dim];
63  }
64  input.setValue(p, value);
65  }
66 }
67
68
69
71 // Functions for testing class ReverseDT.
73
77 bool testReverseDT()
78 {
79  unsigned int nbok = 0;
80  unsigned int nb = 0;
81
82  trace.beginBlock ( "Testing Reverse DT in 2D ..." );
83
84  Z2i::Point a (2, 2 );
85  Z2i::Point b ( 15, 15 );
86
88  Image image (Z2i::Domain( a, b ));
89
90  for ( unsigned k = 0; k < 49; k++ )
91  {
92  a[0] = ( k / 7 ) + 5;
93  a[1] = ( k % 7 ) + 5;
94  image.setValue ( a, 128 );
95  }
96
97  a = Z2i::Point(2,2);
98
100  Predicate aPredicate(image,0);
101
103  Z2i::Domain dom(a,b);
104  L2Metric l2;
106
107  trace.info() << dt<< std::endl;
108  //ReverseDT
109  trace.warning()<<"DT:"<<endl;
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
122  Z2i::L2PowerMetric l2power;
123  RDT reverseDT(&dom,&dt,&l2power);
124
125
126  for(unsigned int j=2; j<16; j++)
127  {
128  for(unsigned int i=2; i<16; i++)
129  trace.info()<< reverseDT.getPowerVector(Z2i::Point(i,j))[0]<<","<<reverseDT.getPowerVector(Z2i::Point(i,j))[1]<<" ";
130  trace.info()<<std::endl;
131  }
132
133  trace.warning()<<"REDT:"<<endl;
134  RDT::ConstRange::ConstIterator it2 = reverseDT.constRange().begin();
135  for (unsigned int y = 2; y < 16; y++)
136  {
137  for (unsigned int x = 2; x < 16; x++)
138  {
139  std::cout << (*it2) << " ";
140  ++it2;
141  }
142  std::cout << std::endl;
143  }
144
145  //Checking
146  bool ok=true;
147  RDT::ConstRange::ConstIterator itrec = reverseDT.constRange().begin(), itend = reverseDT.constRange().end();
148  Image::ConstIterator itinit = image.begin();
149  for( ; itrec != itend; ++itrec,++itinit)
150  if ((*itrec) >= 0)
151  ok = ok & ((*itinit) == 0);
152
153
154  trace.info() << "vector (4,7)= "<< reverseDT.getPowerVector(Z2i::Point(4,7))<<std::endl;
155  nbok += (reverseDT.getPowerVector(Z2i::Point(4,7))==Z2i::Point(5,7)) ? 1 : 0;
156  nb++;
157  trace.info() << "(" << nbok << "/" << nb << ") "
158  << "ok" << std::endl;
159
160  trace.info() << "dist (4,7)= "<< reverseDT.metricPtr()->exactDistanceRepresentation(Z2i::Point(4,7), Z2i::Point(5,7))<<std::endl;
161  nbok += (reverseDT.metricPtr()->exactDistanceRepresentation(Z2i::Point(4,7), Z2i::Point(5,7))==1) ? 1 : 0;
162  nb++;
163  trace.info() << "(" << nbok << "/" << nb << ") "
164  << "true == true" << std::endl;
165
166  trace.info() << "power (4,7)= "<< reverseDT(Z2i::Point(4,7))<<std::endl;
167  nbok += (reverseDT(Z2i::Point(4,7))==0) ? 1 : 0;
168  nb++;
169  trace.info() << "(" << nbok << "/" << nb << ") "
170  << "true == true" << std::endl;
171
172
173
174
175  nbok += ok ? 1 : 0;
176  nb++;
177  trace.info() << "(" << nbok << "/" << nb << ") "
178  << "true == true" << std::endl;
179  trace.endBlock();
180
181  return nbok == nb;
182 }
183
184 bool testReverseDTL1()
185 {
186  unsigned int nbok = 0;
187  unsigned int nb = 0;
188
189  trace.beginBlock ( "Testing Reverse DT in 2D with L1 metric ..." );
190
191  Z2i::Point a (2, 2 );
192  Z2i::Point b ( 15, 15 );
193
195  Image image ( Z2i::Domain( a, b ));
196
197  for ( unsigned k = 0; k < 49; k++ )
198  {
199  a[0] = ( k / 7 ) + 5;
200  a[1] = ( k % 7 ) + 5;
201  image.setValue ( a, 128 );
202  }
203  a = Z2i::Point(2, 2 );
204
205
207  Predicate aPredicate(image,0);
208  Z2i::Domain dom= image.domain();
210  L1Metric l1;
212
213
214  //ReverseDT
215  trace.warning()<<"DT:"<<endl;
217  for (unsigned int y = 2; y < 16; y++)
218  {
219  for (unsigned int x = 2; x < 16; x++)
220  {
221  std::cout << (int)(*it) << " ";
222  ++it;
223  }
224  std::cout << std::endl;
225  }
226
228  Z2i::L1PowerMetric l1power;
229  RDT reverseDT(&dom, &dt, &l1power);
230
231
232  trace.warning()<<"Power"<<std::endl;
233  for(unsigned int j=2; j<16; j++)
234  {
235  for(unsigned int i=2; i<16; i++)
236  trace.info()<< reverseDT.getPowerVector(Z2i::Point(i,j))[0]<<","<<reverseDT.getPowerVector(Z2i::Point(i,j))[1]<<" ";
237  trace.info()<<std::endl;
238  }
239
240  trace.warning()<<"REDT:"<<endl;
241  RDT::ConstRange::ConstIterator it2 = reverseDT.constRange().begin();
242  for (unsigned int y = 2; y < 16; y++)
243  {
244  for (unsigned int x = 2; x < 16; x++)
245  {
246  std::cout << (int)(*it2) << " ";
247  ++it2;
248  }
249  std::cout << std::endl;
250  }
251
252  //Checking
253  bool ok=true;
254  RDT::ConstRange::ConstIterator itrec = reverseDT.constRange().begin(), itend = reverseDT.constRange().end();
255  Image::ConstIterator itinit = image.begin();
256  for( ; itrec != itend; ++itrec,++itinit)
257  if ((*itrec) >= 0)
258  ok = ok & ((*itinit) == 0);
259
260  nbok += ok ? 1 : 0;
261  nb++;
262  trace.info() << "(" << nbok << "/" << nb << ") "
263  << "true == true" << std::endl;
264  trace.endBlock();
265  return nbok == nb;
266 }
267
269 // Standard services - public :
270
271 int main( int argc, char** argv )
272 {
273  trace.beginBlock ( "Testing class ReverseDT" );
274  trace.info() << "Args:";
275  for ( int i = 0; i < argc; ++i )
276  trace.info() << " " << argv[ i ];
277  trace.info() << endl;
278
279  bool res = testReverseDT()
280  && testReverseDTL1(); // && ... other tests
281
282  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
283  trace.endBlock();
284  return res ? 0 : 1;
285 }
286 // //
void beginBlock(const std::string &keyword="")
Aim: implements weighted separable l_p metrics with exact predicates.
ExactPredicateLpSeparableMetric< Space, 1 > L1Metric
Definition: StdDefs.h:119
This class adapts any iterator so that operator* returns another element than the one pointed to by t...
Aim: Implementation of the linear in time distance transformation for separable metrics.
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
Trace trace
Definition: Common.h:130
ExactPredicateLpSeparableMetric< Space, 2 > L2Metric
Definition: StdDefs.h:118
STL namespace.
double endBlock()
functors namespace gathers all DGtal functors.
void setValue(const Point &aPoint, const Value &aValue)
Definition: Image.h:247
Aim: Implementation of the linear in time reverse distance transformation for separable metrics...
Aim: implements separable l_p metrics with exact predicates.
Aim: Define a simple Foreground predicate thresholding image values given a single thresold...
std::ostream & emphase()
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & warning()
std::ostream & info()