DGtal  1.4.beta
testITKDicomReader.cpp
Go to the documentation of this file.
1 
30 #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/images/ImageContainerByITKImage.h"
38 #include "DGtal/io/readers/ITKDicomReader.h"
39 // Required ITK files to read serie DICOM files
40 // DGtal must be compiled with "-DWITH_ITK=true" option
41 #if defined(__clang__)
42 #pragma clang diagnostic push
43 #pragma clang diagnostic ignored "-Wdocumentation"
44 #endif
45 #include <itkGDCMSeriesFileNames.h>
46 #if defined(__clang__)
47 #pragma clang diagnostic pop
48 #endif
49 
50 
52 
53 using namespace std;
54 using namespace DGtal;
55 
57 // Functions for testing class ITKReader.
59 
60 std::vector<std::string>
61 getFirstDicomSerieFileNames(const std::string &path)
62 {
63  typedef itk::GDCMSeriesFileNames NamesGeneratorType;
64  NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
65  nameGenerator->SetUseSeriesDetails( true );
66  nameGenerator->SetDirectory( path );
67 
68  typedef itk::GDCMSeriesFileNames::SeriesUIDContainerType SeriesIdContainer;
69  const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
70 
71  if (! seriesUID.empty() )
72  {
73  return nameGenerator->GetFileNames( *(seriesUID.begin()) );
74  }
75  return std::vector<std::string>();
76 }
77 
78 
79 template <typename Image>
80 void
82 {
83  std::vector<std::string> fileNames = getFirstDicomSerieFileNames( testPath + "samples/dicomSample" );
84 
85  Image image = ITKDicomReader<Image>::importDICOM( fileNames );
86 
87  unsigned int nbVal=0, nbPos = 0;
88  typename Image::ConstRange r = image.constRange();
89  for ( typename Image::ConstRange::ConstIterator it=r.begin(), itend=r.end() ; it != itend ; ++it )
90  {
91  ++nbVal;
92  if ( (*it) > 0 ) ++nbPos;
93  }
94 
95  REQUIRE( ( nbVal==2130048 && nbPos==296030 ) );
96 }
97 
98 template <typename PixelType>
100 {
101  std::vector<std::string> fileNames = getFirstDicomSerieFileNames( testPath + "samples/dicomSample" );
102 
104  DGtalImage img = ITKDicomReader<DGtalImage>::importDICOM( fileNames );
105  typename DGtalImage::ITKImagePointer dgtal_itk = img.getITKImagePointer();
106 
107  typedef itk::Image<PixelType, 3> ItkImage;
108  typedef itk::ImageSeriesReader<ItkImage> ItkReader;
109  typename ItkReader::Pointer reader = ItkReader::New();
110  reader->SetFileNames( fileNames );
111 
112  reader->Update();
113 
114  typename ItkImage::Pointer itk = reader->GetOutput();
115 
116  INFO( "Checking spacing" )
117  REQUIRE( dgtal_itk->GetSpacing() == itk->GetSpacing() );
118  INFO( "Checking origin" )
119  REQUIRE( dgtal_itk->GetOrigin() == itk->GetOrigin() );
120  INFO( "Checking direction" )
121  REQUIRE( dgtal_itk->GetDirection() == itk->GetDirection() );
122 }
123 
124 
125 
126 
127 
128 TEST_CASE( "Testing ITKReader" )
129 {
130  // Default image selector = STLVector
131 
132  SECTION(
133  "Testing ITKDicomReader with 8 bits ImageContainerBySTLVector images" )
134  {
136  testImportDICOM<Image>();
137  }
138 
139  SECTION(
140  "Testing ITKDicomReader with 8 bits ImageContainerByITKImage images" )
141  {
143  testImportDICOM<Image>();
144  }
145 
146  SECTION(
147  "Testing ITKDicomReader with 16 bits ImageContainerBySTLVector images" )
148  {
150  testImportDICOM<Image>();
151  }
152 
153  SECTION(
154  "Testing ITKDicomReader with 16 bits ImageContainerByITKImage images" )
155  {
157  testImportDICOM<Image>();
158  }
159 
160 
161 
162  SECTION(
163  "Testing behavior of ITKDicomReader on empty filenames vector" )
164  {
166  std::vector<std::string> filenames;
167  bool caughtException = false;
168  try
169  {
171  }
172  catch(exception &)
173  {
174  caughtException = true;
175  trace.info() <<"Exception was correctly caught" << std::endl;
176  }
177  REQUIRE( caughtException == true);
178  }
179 
180  SECTION(
181  "Testing ITKDicomReader with 8 bits ImageContainerByITKImage images keeps spatial information" )
182  {
183  testSpatialInformation<unsigned char>();
184  }
185 
186  SECTION(
187  "Testing ITKDicomReader with 16 bits ImageContainerByITKImage images keeps spatial information" )
188  {
189  testSpatialInformation<int16_t>();
190  }
191 
192 
193 }
194 
DGtal::SimpleRandomAccessConstRangeFromPoint
Aim: model of CConstBidirectionalRangeFromPoint that adapts any range of elements bounded by two iter...
Definition: SimpleRandomAccessConstRangeFromPoint.h:73
DGtal::ImageContainerByITKImage
Aim: implements a model of CImageContainer using a ITK Image.
Definition: ImageContainerByITKImage.h:93
DGtal::ImageContainerBySTLVector
Definition: ImageContainerBySTLVector.h:126
getFirstDicomSerieFileNames
std::vector< std::string > getFirstDicomSerieFileNames(const std::string &path)
Definition: testITKDicomReader.cpp:61
DGtal::trace
Trace trace
Definition: Common.h:154
DGtal::ImageContainerBySTLVector< Domain, Value >::ConstIterator
std::vector< Value >::const_iterator ConstIterator
Definition: ImageContainerBySTLVector.h:265
REQUIRE
REQUIRE(domain.isInside(aPoint))
TEST_CASE
TEST_CASE("Testing ITKReader")
Definition: testITKDicomReader.cpp:128
testSpatialInformation
void testSpatialInformation()
Definition: testITKDicomReader.cpp:99
DGtal::Trace::info
std::ostream & info()
Image
ImageContainerBySTLVector< Domain, Value > Image
Definition: testSimpleRandomAccessRangeFromPoint.cpp:45
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
DGtal::Image
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:69
SECTION
SECTION("Testing constant forward iterators")
Definition: testSimpleRandomAccessRangeFromPoint.cpp:66
DGtal::ITKDicomReader
Aim: Import a 2D/3D DICOM Image from file series.
Definition: ITKDicomReader.h:81
testImportDICOM
void testImportDICOM()
Definition: testITKDicomReader.cpp:81