DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
testDicomReader.cpp
Go to the documentation of this file.
1
16
29
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>
53
bool
testDicomReader
()
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>
93
bool
testDicomReaderFromDirectory
()
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>
116
bool
testSpatialInformation
()
117
{
118
const
std::string path = testPath +
"samples/dicomSample"
;
119
120
const
std::vector<std::string> fileNames =
getFirstDicomSerieFileNames
( path );
121
122
typedef
ImageContainerByITKImage<Z3i::Domain, PixelType>
Image3D;
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
142
bool
testIOException
()
143
{
144
//Default image selector = STLVector
145
typedef
ImageContainerBySTLVector<Z3i::Domain, int >
Image3D;
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 caught. 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>
>()
173
&&
testDicomReader
<
ImageContainerBySTLVector<Z3i::Domain, uint16_t>
>()
174
&&
testDicomReader<ImageContainerByITKImage<Z3i::Domain, unsigned char>
>()
175
&&
testDicomReader
<
ImageContainerByITKImage<Z3i::Domain, uint16_t>
>()
176
&&
testDicomReaderFromDirectory<ImageContainerBySTLVector<Z3i::Domain, unsigned char>
>()
177
&&
testDicomReaderFromDirectory
<
ImageContainerByITKImage<Z3i::Domain, uint16_t>
>()
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
// //
DGtal::ImageContainerByITKImage
Aim: implements a model of CImageContainer using a ITK Image.
Definition
ImageContainerByITKImage.h:94
DGtal::ImageContainerBySTLVector
Definition
ImageContainerBySTLVector.h:127
DGtal::Image::constRange
ConstRange constRange() const
Definition
Image.h:203
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::Trace::info
std::ostream & info()
DGtal::Trace::endBlock
double endBlock()
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::trace
Trace trace
std
STL namespace.
DGtal::DicomReader::importDicomSeries
static ImageContainer importDicomSeries(const std::vector< std::string > &filenames, const Functor &aFunctor=Functor())
DGtal::DicomReader::importDicom
static ImageContainer importDicom(const std::string &aFilename, const Functor &aFunctor=Functor())
testSpatialInformation
bool testSpatialInformation()
Definition
testDicomReader.cpp:116
testDicomReaderFromDirectory
bool testDicomReaderFromDirectory()
Definition
testDicomReader.cpp:93
testDicomReader
bool testDicomReader()
Definition
testDicomReader.cpp:53
getFirstDicomSerieFileNames
static std::vector< std::string > getFirstDicomSerieFileNames(const std::string &path)
Definition
testDicomReader.cpp:75
testIOException
bool testIOException()
Definition
testDicomReader.cpp:142
main
int main(int, char **)
Definition
testIntegerComputer.cpp:331
image
Image image(domain)
tests
io
readers
testDicomReader.cpp
Generated on
for DGtal by
1.18.0