DGtal  1.2.0
testITKReader.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "ConfigTest.h"
34 #include "DGtalCatch.h"
35 #include "DGtal/helpers/StdDefs.h"
36 #include "DGtal/images/ImageContainerBySTLVector.h"
37 #include "DGtal/io/readers/ITKReader.h"
39 
40 using namespace std;
41 using namespace DGtal;
42 
44 // Functions for testing class ITKReader.
46 
52 template <typename PixelType>
53 void testSpatialInformation(const std::string &filename)
54 {
56 
57  DGtalImage img = DGtal::ITKReader<DGtalImage>::importITK(filename);
58 
59  typename DGtalImage::ITKImagePointer dgtal_itk = img.getITKImagePointer();
60 
61 
62  typedef itk::Image<PixelType, 3> ItkImage;
63  typedef itk::ImageFileReader<ItkImage> ReaderType;
64  typename ReaderType::Pointer reader = ReaderType::New();
65  reader->SetFileName(filename);
66 
67  reader->Update();
68 
69  typename ItkImage::Pointer itk = reader->GetOutput();
70 
71  INFO( "Checking spacing" )
72  REQUIRE( dgtal_itk->GetSpacing() == itk->GetSpacing() );
73  INFO( "Checking origin" )
74  REQUIRE( dgtal_itk->GetOrigin() == itk->GetOrigin() );
75  INFO( "Checking direction" )
76  REQUIRE( dgtal_itk->GetDirection() == itk->GetDirection() );
77 }
78 
79 
80 
81 TEST_CASE( "Testing ITKReader" )
82 {
83  // Default image selector = STLVector
87 
88  SECTION(
89  "Testing feature io/readers of ITKReader with 16 bits (uint16) images" )
90  {
91  Image3D16b im = ITKReader<Image3D16b>::importITK(
92  testPath + "samples/lobsterCroped16b.mhd" );
93  REQUIRE( ( im( Z3i::Point( 35, 29, 3 ) ) == 60400 ) );
94  }
95 
96  SECTION(
97  "Testing feature io/readers of ITKReader with rescaled 16 bits (uint16) "
98  "images" )
99  {
101  RescalFCT resc = RescalFCT( 0, 65535, 0, 255 );
102  Image3DUC im = ITKReader<Image3DUC>::importITK(
103  testPath + "samples/lobsterCroped16b.mhd", resc );
104  REQUIRE( ( im( Z3i::Point( 35, 29, 3 ) ) == resc( 60400 ) ) );
105  }
106  SECTION(
107  "Testing behavior of ITKReader on non existent image file" )
108  {
109  bool caughtException = false;
110  const std::string filename = testPath + "samples/null.mhd"; //non existent file
111  try
112  {
113  Image3DUC im = ITKReader<Image3DUC>::importITK(filename);
114  }
115  catch(exception &)
116  {
117  caughtException = true;
118  trace.info() <<"Exception was correctly caught" << std::endl;
119  }
120  REQUIRE( caughtException == true);
121  }
122 
123  SECTION(
124  "Checking spatial information when loading through DGtal" )
125  {
126  testSpatialInformation<int16_t>(testPath + "samples/lobsterCroped16b.mhd" );
127  testSpatialInformation<int16_t>(testPath + "samples/lobsterCropedB16b.mhd" );
128  }
129 
130  SECTION(
131  "Checking import with/without shifted domain")
132 
133  {
134  Image3Dd imShifted = ITKReader<Image3Dd>::importITK(testPath + "samples/lobsterCroped2.nii",
136 
137  Image3Dd imNonShifted = ITKReader<Image3Dd>::importITK(testPath + "samples/lobsterCroped2.nii", false );
138 
139  REQUIRE( ( imShifted( Z3i::Point( 144, 100, 21 ) ) == 113 ) );
140  REQUIRE( ( imNonShifted( Z3i::Point( 94, 50, 11 ) ) == 113 ) );
141  Z3i::Point pTarget (95,76,7);
142  auto i = 101*pTarget[1]+pTarget[0]+pTarget[2]*101*101;
143  trace.info() << "value:" << *(imNonShifted.range().begin()+i);
144  REQUIRE( ( *(imNonShifted.range().begin()+i) == *(imShifted.range().begin()+i ) ));
145  REQUIRE( ( *(imNonShifted.range().begin()+i) == 68 ));
146  }
147 
148 
149 }
150 
Aim: implements a model of CImageContainer using a ITK Image.
std::ostream & info()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
Aim: Import a 2D/3D Image using the ITK formats.
Definition: ITKReader.h:80
static Image importITK(const std::string &filename, const TFunctor &aFunctor=TFunctor(), bool shiftDomainUsingOrigin=true)
Aim: Define a simple functor using the static cast operator.
Aim: Functor allowing to rescale a value. Values of the initial scale [initMin,initMax] are rescaled ...
void testSpatialInformation(const std::string &filename)
TEST_CASE("Testing ITKReader")
SECTION("Testing constant forward iterators")
REQUIRE(domain.isInside(aPoint))