DGtal  1.2.0
testDicomReader.cpp
Go to the documentation of this file.
1 
31 #include <iostream>
32 #include "DGtal/images/ImageContainerBySTLVector.h"
33 #include "DGtal/images/ImageContainerByITKImage.h"
34 #include "DGtal/helpers/StdDefs.h"
35 #include "DGtal/io/readers/DicomReader.h"
36 
37 #include "ConfigTest.h"
38 
40 
41 using namespace std;
42 using namespace DGtal;
43 
45 // Functions for testing class DicomReader.
47 
52 template <typename Image3D>
54 {
55  const std::string filename = testPath + "samples/dicomSample/1629.dcm";
56  Image3D image = DicomReader< Image3D >::importDicom( filename );
57 
58  trace.info() << image <<endl;
59 
60  unsigned int nbVal=0, nbPos = 0;
61  typename Image3D::ConstRange r = image.constRange();
62  for ( typename Image3D::ConstRange::ConstIterator it=r.begin(), itend=r.end() ; it != itend ; ++it )
63  {
64  nbVal++;
65  if ( (*it) > 0 ) nbPos++;
66  }
67 
68  trace.info() << "Number of points with (val>0) = " << nbVal << endl;
69 
70  return nbVal==2130048 && nbPos==296030;
71 }
72 
73 static
74 std::vector<std::string>
75 getFirstDicomSerieFileNames(const std::string &path)
76 {
77  typedef itk::GDCMSeriesFileNames NamesGeneratorType;
78  NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
79  nameGenerator->SetUseSeriesDetails( true );
80  nameGenerator->SetDirectory( path );
81 
82  typedef itk::GDCMSeriesFileNames::SeriesUIDContainerType SeriesIdContainer;
83  const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
84 
85  if (! seriesUID.empty() )
86  {
87  return nameGenerator->GetFileNames( *(seriesUID.begin()) );
88  }
89  return std::vector<std::string>();
90 }
91 
92 template <typename Image3D>
94 {
95  const std::string path = testPath + "samples/dicomSample";
96  const std::vector<std::string> fileNames = getFirstDicomSerieFileNames( path );
97  Image3D image = DicomReader< Image3D >::importDicomSeries( fileNames );
98 
99  trace.info() << image <<endl;
100 
101  unsigned int nbVal=0, nbPos = 0;
102  typename Image3D::ConstRange r = image.constRange();
103  for ( typename Image3D::ConstRange::ConstIterator it=r.begin(), itend=r.end() ; it != itend ; ++it )
104  {
105  nbVal++;
106  if ( (*it) > 0 ) nbPos++;
107  }
108 
109  trace.info() << "Number of points with (val>0) = " << nbVal << endl;
110 
111  return nbVal==2130048 && nbPos==296030;
112 }
113 
114 
115 template <typename PixelType>
117 {
118  const std::string path = testPath + "samples/dicomSample";
119 
120  const std::vector<std::string> fileNames = getFirstDicomSerieFileNames( path );
121 
123  Image3D image = DicomReader< Image3D >::importDicomSeries( fileNames );
124  typename Image3D::ITKImagePointer dgtal_itk = image.getITKImagePointer();
125 
126  typedef itk::Image<PixelType, 3> ItkImage;
127  typedef itk::ImageSeriesReader<ItkImage> ItkReader;
128  typename ItkReader::Pointer reader = ItkReader::New();
129  reader->SetFileNames( fileNames );
130 
131  reader->Update();
132 
133  typename ItkImage::Pointer itk = reader->GetOutput();
134 
135  const bool ok1 = ( dgtal_itk->GetSpacing() == itk->GetSpacing() );
136  const bool ok2 = ( dgtal_itk->GetOrigin() == itk->GetOrigin() );
137  const bool ok3 = ( dgtal_itk->GetDirection() == itk->GetDirection() );
138  return ok1 && ok2 && ok3;
139 }
140 
141 
143 {
144  //Default image selector = STLVector
146 
147  std::string filename = testPath + "samples/null.dcm";
148  try {
149  Image3D image = DicomReader< Image3D >::importDicom( filename );
150  }
151  catch(exception& e) {
152  trace.info() << "Exception catched. Message : " << e.what()<<endl;
153  return true;
154  }
155 
156  return false;
157 }
158 
159 
160 
162 // Standard services - public :
163 
164 int main( int argc, char** argv )
165 {
166  trace.beginBlock ( "Testing class DicomReader" );
167  trace.info() << "Args:";
168  for ( int i = 0; i < argc; ++i )
169  trace.info() << " " << argv[ i ];
170  trace.info() << endl;
171 
172  bool res = testDicomReader<ImageContainerBySTLVector<Z3i::Domain, unsigned char> >()
174  && testDicomReader<ImageContainerByITKImage<Z3i::Domain, unsigned char> >()
176  && testDicomReaderFromDirectory<ImageContainerBySTLVector<Z3i::Domain, unsigned char> >()
178  && testSpatialInformation<unsigned char>()
179  && testSpatialInformation<uint16_t>()
180  && testIOException();
181  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
182  trace.endBlock();
183 
184  return !res;
185 }
186 // //
Aim: implements a model of CImageContainer using a ITK Image.
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
MyDigitalSurface::ConstIterator ConstIterator
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
Aim: Import a 3D DICOM image from file series.
Definition: DicomReader.h:92
bool testSpatialInformation()
bool testDicomReaderFromDirectory()
int main(int argc, char **argv)
bool testDicomReader()
bool testIOException()
static std::vector< std::string > getFirstDicomSerieFileNames(const std::string &path)
Image::ConstRange ConstRange