DGtal  0.9.3
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);
64  Image image(domain);
65 
66  typedef typename std::vector<typename Image::Value> Values;
67  Values values;
68  for (typename Domain::Size kk=0; kk<domain.size(); kk++)
69  values.push_back(rand());
70 
71  std::copy(values.begin(), values.end(), image.range().outputIterator());
72 
73  trace.info() << image << endl;
74  trace.info() << "writing " << filename << endl;
75  if (!ITKWriter<Image>::exportITK(filename, image)) return false;
76 
77  trace.info() << "reading " << filename << endl;
78  Image image_read = ITKReader<Image>::importITK(filename);
79  trace.info() << image_read << endl;
80 
81  if (image_read.domain().lowerBound() != image.domain().lowerBound()) trace.warning() << "lowerBound mismatch!!" << endl;
82  if (image_read.domain().upperBound() != image.domain().upperBound()) trace.warning() << "upperBound mismatch!!" << endl;
83 
84  typename Image::ConstRange::ConstIterator iter_read = image_read.constRange().begin();
85  typename Image::ConstRange::ConstIterator iter_read_end = image_read.constRange().end();
86  typename Values::const_iterator iter_value = values.begin();
87  typename Values::const_iterator iter_value_end = values.end();
88  while (iter_value!=iter_value_end && iter_read!=iter_read_end)
89  {
90  if ((*iter_read)!=(*iter_value)) {
91  trace.error() << "values mismatch" << endl;
92  return false;
93  }
94  iter_value++;
95  iter_read++;
96  }
97 
98  return true;
99 }
100 
101 bool testITKio()
102 {
103  unsigned int nbok = 0;
104  unsigned int nb = 0;
105 
106  nbok += 6;
107  trace.beginBlock ( "Testing 2D ITK image value types ..." );
108  nb += test_image<ImageSelector<Z2i::Domain, int>::Type>("image_2d_int.mha");
109  nb += test_image<ImageSelector<Z2i::Domain, bool>::Type>("image_2d_bool.mha");
110  nb += test_image<ImageSelector<Z2i::Domain, unsigned int>::Type>("image_2d_unsigned_int.mha");
111  nb += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_2d_unsigned_char.mha");
112  nb += test_image<ImageSelector<Z2i::Domain, float>::Type>("image_2d_float.mha");
113  nb += test_image<ImageSelector<Z2i::Domain, double>::Type>("image_2d_double.mha");
114  trace.endBlock();
115 
116  nbok += 6;
117  trace.beginBlock ( "Testing 3D ITK image value types ..." );
118  nb += test_image<ImageSelector<Z3i::Domain, int>::Type>("image_3d_int.mha");
119  nb += test_image<ImageSelector<Z3i::Domain, bool>::Type>("image_3d_bool.mha");
120  nb += test_image<ImageSelector<Z3i::Domain, unsigned int>::Type>("image_3d_unsigned_int.mha");
121  nb += test_image<ImageSelector<Z3i::Domain, unsigned char>::Type>("image_3d_unsigned_char.mha");
122  nb += test_image<ImageSelector<Z3i::Domain, float>::Type>("image_3d_float.mha");
123  nb += test_image<ImageSelector<Z3i::Domain, double>::Type>("image_3d_double.mha");
124  trace.endBlock();
125 
126  nbok += 3;
127  trace.beginBlock ( "Testing 2D ITK image formats ..." );
128  nb += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.jpg"); nbok--; // jpg is lossy
129  nb += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.png");
130  nb += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.bmp");
131  trace.endBlock();
132 
133  trace.info() << "(" << nbok << "/" << nb << ") " << endl;
134 
135  return nbok == nb;
136 }
137 
139 // Standard services - public :
140 
141 int main( int /*argc*/, char** /*argv*/ )
142 {
143  bool res = testITKio(); // && ... other tests
144  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
145  return res ? 0 : 1;
146 }
147 // //
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
const Domain domain(Point(1, 2), Point(6, 5))
Trace trace
Definition: Common.h:137
double endBlock()
int main(int, char **)
Definition: testITKio.cpp:141
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
static Image importITK(const std::string &filename, const Functor &aFunctor=Functor())
bool testITKio()
Definition: testITKio.cpp:101
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
Range range()
Definition: Image.h:214