DGtal 1.3.0
Loading...
Searching...
No Matches
testITKDicomReader.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/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
53using namespace std;
54using namespace DGtal;
55
57// Functions for testing class ITKReader.
59
60std::vector<std::string>
61getFirstDicomSerieFileNames(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
79template <typename Image>
80void
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
98template <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
128TEST_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
Aim: implements a model of CImageContainer using a ITK Image.
Aim: implements association bewteen points lying in a digital domain and values.
Definition: Image.h:70
Aim: model of CConstBidirectionalRangeFromPoint that adapts any range of elements bounded by two iter...
std::ostream & info()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:154
STL namespace.
static Image importDICOM(const std::vector< std::string > &filenames, const TFunctor &aFunctor=TFunctor())
void testSpatialInformation()
std::vector< std::string > getFirstDicomSerieFileNames(const std::string &path)
void testImportDICOM()
TEST_CASE("Testing ITKReader")
SECTION("Testing constant forward iterators")
REQUIRE(domain.isInside(aPoint))