DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testConstImageAdapter.cpp
1 
35 
36 #include <iostream>
37 
38 #include "DGtal/base/Common.h"
39 #include "DGtal/helpers/StdDefs.h"
40 #include "ConfigTest.h"
41 
42 #include "DGtal/base/BasicFunctors.h"
43 #include "DGtal/images/ImageSelector.h"
44 #include "DGtal/images/ConstImageAdapter.h"
45 
47 
48 using namespace std;
49 using namespace DGtal;
50 
51 template <typename R>
52 void displayRange(const R& r)
53 {
54  typedef typename R::ConstIterator I;
55  for (I it = r.begin(), itEnd = r.end();
56  it != itEnd; ++it)
57  {
58  trace.info() << *it << " ";
59  }
60  trace.info() << std::endl;
61 }
62 
64 int main( int argc, char** argv )
65 {
66  unsigned int nbok = 0;
67  unsigned int nb = 0;
68 
69  trace.beginBlock ( "Test for ConstImageAdapter" );
70  trace.info() << "Args:";
71  for ( int i = 0; i < argc; ++i )
72  trace.info() << " " << argv[ i ];
73  trace.info() << endl;
74 
75  using namespace Z2i;
76 
77  trace.beginBlock("Image creation");
78 
79  //domain
80  const Integer size = 5;
81  Point p = Point::diagonal(0);
82  Point q = Point::diagonal(size-1);
83  Domain d(p,q);
84 
85  //image
87  Image img(d);
88 
89  //fill
90  const int maximalValue = size*size;
91  Image::Range::OutputIterator it = img.range().outputIterator();
92  for (int i = 0; i < maximalValue; ++i)
93  *it++ = i;
94 
95  //display values
96  Image::ConstRange r = img.constRange();
97  std::copy( r.begin(), r.end(), std::ostream_iterator<int>(cout,", ") );
98  cout << endl;
99  trace.endBlock();
100 
101 
102  const int thresholdValue = maximalValue/2;
103  trace.beginBlock("Implicit thresholding");
104 
106  DefaultFunctor g;
107  Thresholder<Image::Value> t( thresholdValue );
110 
111  //display values
114  ra = a.constRange();
115  std::copy( ra.begin(), ra.end(), std::ostream_iterator<int>(cout,", ") );
116  cout << endl;
117 
118  std::vector<int> to_vector(25);
119  std::copy(ra.begin(), ra.end(), to_vector.begin());
120  for (int i = 0; i < 25; i++)
121  {
122  if (i<=12)
123  {
124  if (to_vector[i]==1)
125  {
126  cout << "ok, ";
127  nbok += true ? 1 : 0; nb++;
128  }
129  else
130  {
131  cout << "!ok, ";
132  nbok += false ? 1 : 0; nb++;
133  }
134  }
135  else
136  {
137  if (to_vector[i]==0)
138  {
139  cout << "ok, ";
140  nbok += true ? 1 : 0; nb++;
141  }
142  else
143  {
144  cout << "!ok, ";
145  nbok += false ? 1 : 0; nb++;
146  }
147  }
148  }
150 
151  cout << endl;
152 
153  trace.endBlock();
154 
155  bool res = (nbok == nb);
156 
157  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
158  trace.endBlock();
159  return res ? 0 : 1;
160 }
161 // //