DGtal 1.3.0
Loading...
Searching...
No Matches
Functions
testITKio.cpp File Reference
#include "ConfigTest.h"
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/images/ImageSelector.h"
#include "DGtal/images/CImage.h"
#include "DGtal/io/writers/ITKWriter.h"
#include "DGtal/io/readers/ITKReader.h"
#include <string>

Go to the source code of this file.

Functions

template<typename Image >
bool test_image (const string &filename)
 
bool testITKSpacingIO ()
 
bool testITKio ()
 
int main (int, char **)
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Pierre Gueth (pierr.nosp@m.e.gu.nosp@m.eth@g.nosp@m.mail.nosp@m..com ) Laboratoire d'InfoRmatique en Image et Systemes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2013/10/28

Functions for testing class ITKio.

This file is part of the DGtal library.

Definition in file testITKio.cpp.

Function Documentation

◆ main()

int main ( int  ,
char **   
)

Definition at line 193 of file testITKio.cpp.

194{
195 bool res = testITKio(); // && ... other tests
196 trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
197 return res ? 0 : 1;
198}
std::ostream & emphase()
Trace trace
Definition: Common.h:154
bool testITKio()
Definition: testITKio.cpp:146

References DGtal::Trace::emphase(), testITKio(), and DGtal::trace.

◆ test_image()

template<typename Image >
bool test_image ( const string &  filename)

Definition at line 49 of file testITKio.cpp.

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 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}
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:70
const Domain & domain() const
Definition: Image.h:192
ConstRange constRange() const
Definition: Image.h:203
std::ostream & warning()
std::ostream & error()
std::ostream & info()
static Image importITK(const std::string &filename, const TFunctor &aFunctor=TFunctor(), bool shiftDomainUsingOrigin=true)
Export a 2D/3D Image using the ITK formats.
Definition: ITKWriter.h:65
Aim: Defines the concept describing a read/write image, having an output iterator.
Definition: CImage.h:103
MyPointD Point
Definition: testClone2.cpp:383
Domain domain
HyperRectDomain< Space > Domain

References DGtal::SimpleRandomAccessConstRangeFromPoint< TConstIterator, DistanceFunctor >::begin(), DGtal::Image< TImageContainer >::constRange(), DGtal::Image< TImageContainer >::domain(), domain, DGtal::SimpleRandomAccessConstRangeFromPoint< TConstIterator, DistanceFunctor >::end(), DGtal::Trace::error(), DGtal::ITKReader< TImage >::importITK(), DGtal::Trace::info(), DGtal::HyperRectDomain< TSpace >::size(), DGtal::trace, and DGtal::Trace::warning().

◆ testITKio()

bool testITKio ( )

Definition at line 146 of file testITKio.cpp.

147{
148 unsigned int nbok = 0;
149 unsigned int nb = 0;
150
151 nb += 8;
152 trace.beginBlock ( "Testing 2D ITK image value types ..." );
153 nbok += test_image<ImageSelector<Z2i::Domain, int>::Type>("image_2d_int.mha");
154 nbok += test_image<ImageSelector<Z2i::Domain, bool>::Type>("image_2d_bool.mha");
155 nbok += test_image<ImageSelector<Z2i::Domain, unsigned int>::Type>("image_2d_unsigned_int.mha");
156 nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_2d_unsigned_char.mha");
157 nbok += test_image<ImageSelector<Z2i::Domain, unsigned long>::Type>("image_2d_unsigned_long.mha");
158 nbok += test_image<ImageSelector<Z2i::Domain, long>::Type>("image_2d_long.mha");
159 nbok += test_image<ImageSelector<Z2i::Domain, float>::Type>("image_2d_float.mha");
160 nbok += test_image<ImageSelector<Z2i::Domain, double>::Type>("image_2d_double.mha");
161 trace.endBlock();
162
163 nb += 8;
164 trace.beginBlock ( "Testing 3D ITK image value types ..." );
165 nbok += test_image<ImageSelector<Z3i::Domain, int>::Type>("image_3d_int.mha");
166 nbok += test_image<ImageSelector<Z3i::Domain, bool>::Type>("image_3d_bool.mha");
167 nbok += test_image<ImageSelector<Z3i::Domain, unsigned int>::Type>("image_3d_unsigned_int.mha");
168 nbok += test_image<ImageSelector<Z3i::Domain, unsigned char>::Type>("image_3d_unsigned_char.mha");
169 nbok += test_image<ImageSelector<Z3i::Domain, unsigned long>::Type>("image_3d_unsigned_long.mha");
170 nbok += test_image<ImageSelector<Z3i::Domain, long>::Type>("image_3d_long.mha");
171 nbok += test_image<ImageSelector<Z3i::Domain, float>::Type>("image_3d_float.mha");
172 nbok += test_image<ImageSelector<Z3i::Domain, double>::Type>("image_3d_double.mha");
173 trace.endBlock();
174
175 nb += 3;
176 trace.beginBlock ( "Testing 2D ITK image formats ..." );
177 nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.jpg"); nb--; // jpg is lossy
178 nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.png");
179 nbok += test_image<ImageSelector<Z2i::Domain, unsigned char>::Type>("image_unsigned_char.bmp");
180 trace.endBlock();
181
182 trace.info() << "(" << nbok << "/" << nb << ") " << endl;
183 nb += 1;
184 trace.beginBlock ( "Testing 3D ITK image with spacing ..." );
185 nbok += testITKSpacingIO();
186 trace.endBlock();
187 return nbok == nb;
188}
void beginBlock(const std::string &keyword="")
double endBlock()
bool testITKSpacingIO()
Definition: testITKio.cpp:103

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::Trace::info(), testITKSpacingIO(), and DGtal::trace.

Referenced by main().

◆ testITKSpacingIO()

bool testITKSpacingIO ( )

Definition at line 103 of file testITKio.cpp.

104{
107 Image3DITK input = ITKReader<Image3DITK>::importITK("image_3d_int.mha");
108 Image3DITK copy(input.domain());
109 Image3DITK::ImageSpacing s (0.2, 0.3, 0.4);
110 trace.info() << "setting image spacing to 0.2, 0.3, 0.4" << std::endl;
111 copy.setImageSpacing(s);
112 for (auto p: input.domain() ) {copy.setValue(p, input(p));}
113 ITKWriter<Image3DITK>::exportITK("image_3d_intSpace0.2.mha", copy);
114 Image3DITK check = ITKReader<Image3DITK>::importITK("image_3d_intSpace0.2.mha");
115 s = check.getImageSpacing();
116 trace.info() << "reading image spacing after write (should be 0.2, 0.3, 0.4)" << std::endl;
117 trace.info() << "spacing: " << s[0] << " " << s[1] << " " << s[2] << std::endl;
118 Image3D img (input.domain());
120 ITKWriter<Image3D>::exportITK("imageVect_3d_intSpace0.8.mha", img, Z3i::RealPoint(0.8, 0.9, 1.0));
121 Image3DITK check3 = ITKReader<Image3DITK>::importITK("imageVect_3d_intSpace0.8.mha");
122 auto s3 = check3.getImageSpacing();
123 trace.info() << "reading image spacing after export with spacing mention (should be 0.8, 0.9, 1.0)" << std::endl;
124 trace.info() << "spacing: " << s3[0] << " " << s3[1] << " " << s3[2] << std::endl;
125
127 Image2DITK input2 = ITKReader<Image2DITK>::importITK("image_2d_int.mha");
128 Image2DITK copy2(input2.domain());
129 Image2DITK::ImageSpacing s2 (0.2, 0.3);
130 trace.info() << "setting image spacing to 0.2, 0.3" << std::endl;
131 copy2.setImageSpacing(s2);
132 for (auto p: input2.domain() ) {copy2.setValue(p, input2(p));}
133 ITKWriter<Image2DITK>::exportITK("image_2d_intSpace0.2.mha", copy2);
134 Image2DITK check2 = ITKReader<Image2DITK>::importITK("image_2d_intSpace0.2.mha");
135 s2 = check2.getImageSpacing();
136 trace.info() << "reading image spacing after write (should be 0.2, 0.3)" << std::endl;
137 trace.info() << "spacing: " << s2[0] << " " << s2[1] << std::endl;
138
139
140 return s[0] == 0.2 && s[1] == 0.3 && s[2] == 0.4 &&
141 s2[0] == 0.2 && s2[1] == 0.3 &&
142 s3[0] == 0.8 && s3[1] == 0.9 && s3[2] == 1.0;
143}
Aim: implements a model of CImageContainer using a ITK Image.
static bool exportITK(const std::string &filename, const Image &aImage, const Functor &aFunctor=Functor())
Aim: Define a simple default functor that just returns its argument.

References DGtal::ITKWriter< TImage, TFunctor >::exportITK(), DGtal::ITKReader< TImage >::importITK(), DGtal::Trace::info(), and DGtal::trace.

Referenced by testITKio().