DGtal  1.0.0
testITKio.cpp
Go to the documentation of this file.
1 
30 #include "ConfigTest.h"
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/images/ImageSelector.h"
35 #include "DGtal/images/CImage.h"
36 #include "DGtal/io/writers/ITKWriter.h"
37 #include "DGtal/io/readers/ITKReader.h"
38 using namespace DGtal;
39 
40 #include <string>
41 using std::endl;
42 using std::string;
43 
45 // Functions for testing ITK io
47 template <typename Image>
48 bool
49 test_image(const string& filename)
50 {
51  BOOST_CONCEPT_ASSERT(( concepts::CImage<Image> ));
52 
53  typedef typename Image::Domain::Point Point;
54  Point point0;
55  Point point1;
56  for (typename Image::Domain::Dimension kk=0; kk<Image::Domain::dimension; kk++)
57  {
58  point0[kk] = 5;
59  point1[kk] = 10;
60  }
61 
62  typedef typename Image::Domain Domain;
63  const Domain domain(point0, point1);
65 
66  typedef typename std::vector<typename Image::Value> Values;
67  Values values;
68  values.reserve(domain.size());
69  for (typename Domain::Size kk=0; kk<domain.size(); kk++)
70  values.push_back(rand());
71 
72  std::copy(values.begin(), values.end(), image.range().outputIterator());
73 
74  trace.info() << image << endl;
75  trace.info() << "writing " << filename << endl;
76  if (!ITKWriter<Image>::exportITK(filename, image)) return false;
77 
78  trace.info() << "reading " << filename << endl;
79  Image image_read = ITKReader<Image>::importITK(filename);
80  trace.info() << image_read << endl;
81 
82  if (image_read.domain().lowerBound() != image.domain().lowerBound()) trace.warning() << "lowerBound mismatch!!" << endl;
83  if (image_read.domain().upperBound() != image.domain().upperBound()) trace.warning() << "upperBound mismatch!!" << endl;
84 
85  typename Image::ConstRange::ConstIterator iter_read = image_read.constRange().begin();
86  typename Image::ConstRange::ConstIterator iter_read_end = image_read.constRange().end();
87  typename Values::const_iterator iter_value = values.begin();
88  typename Values::const_iterator iter_value_end = values.end();
89  while (iter_value!=iter_value_end && iter_read!=iter_read_end)
90  {
91  if ((*iter_read)!=(*iter_value)) {
92  trace.error() << "values mismatch" << endl;
93  return false;
94  }
95  iter_value++;
96  iter_read++;
97  }
98 
99  return true;
100 }
101 
102 bool testITKio()
103 {
104  unsigned int nbok = 0;
105  unsigned int nb = 0;
106 
107  nb += 8;
108  trace.beginBlock ( "Testing 2D ITK image value types ..." );
109  nbok += test_image<ImageSelector<Z2i::Domain, int>::Type>("image_2d_int.mha");
110  nbok += test_image<ImageSelector<Z2i::Domain, bool>::Type>("image_2d_bool.mha");
111  nbok += test_image<ImageSelector<Z2i::Domain, unsigned int>::Type>("image_2d_unsigned_int.mha");
112  nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_2d_unsigned_char.mha");
113  nbok += test_image<ImageSelector<Z2i::Domain, unsigned long>::Type>("image_2d_unsigned_long.mha");
114  nbok += test_image<ImageSelector<Z2i::Domain, long>::Type>("image_2d_long.mha");
115  nbok += test_image<ImageSelector<Z2i::Domain, float>::Type>("image_2d_float.mha");
116  nbok += test_image<ImageSelector<Z2i::Domain, double>::Type>("image_2d_double.mha");
117  trace.endBlock();
118 
119  nb += 8;
120  trace.beginBlock ( "Testing 3D ITK image value types ..." );
121  nbok += test_image<ImageSelector<Z3i::Domain, int>::Type>("image_3d_int.mha");
122  nbok += test_image<ImageSelector<Z3i::Domain, bool>::Type>("image_3d_bool.mha");
123  nbok += test_image<ImageSelector<Z3i::Domain, unsigned int>::Type>("image_3d_unsigned_int.mha");
124  nbok += test_image<ImageSelector<Z3i::Domain, unsigned char>::Type>("image_3d_unsigned_char.mha");
125  nbok += test_image<ImageSelector<Z3i::Domain, unsigned long>::Type>("image_3d_unsigned_long.mha");
126  nbok += test_image<ImageSelector<Z3i::Domain, long>::Type>("image_3d_long.mha");
127  nbok += test_image<ImageSelector<Z3i::Domain, float>::Type>("image_3d_float.mha");
128  nbok += test_image<ImageSelector<Z3i::Domain, double>::Type>("image_3d_double.mha");
129  trace.endBlock();
130 
131  nb += 3;
132  trace.beginBlock ( "Testing 2D ITK image formats ..." );
133  nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.jpg"); nb--; // jpg is lossy
134  nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.png");
135  nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.bmp");
136  trace.endBlock();
137 
138  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
139 
140  return nbok == nb;
141 }
142 
144 // Standard services - public :
145 
146 int main( int /*argc*/, char** /*argv*/ )
147 {
148  bool res = testITKio(); // && ... other tests
149  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
150  return res ? 0 : 1;
151 }
152 // //
void beginBlock(const std::string &keyword="")
HyperRectDomain< Space > Domain
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
Trace trace
Definition: Common.h:144
double endBlock()
int main(int, char **)
Definition: testITKio.cpp:146
Domain domain
static Image importITK(const std::string &filename, const TFunctor &aFunctor=TFunctor())
ConstRange constRange() const
Definition: Image.h:203
std::ostream & emphase()
DGtal is the top-level namespace which contains all DGtal functions and types.
MyPointD Point
Definition: testClone2.cpp:383
std::ostream & warning()
std::ostream & info()
const Domain & domain() const
Definition: Image.h:192
bool testITKio()
Definition: testITKio.cpp:102
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:102
std::ostream & error()
Image image(domain)
bool test_image(const string &filename)
Definition: testITKio.cpp:49
Export a 2D/3D Image using the ITK formats.
Definition: ITKWriter.h:63