DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
testITKReader.cpp
Go to the documentation of this file.
1
16
29
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
47
53
template
<
typename
PixelType>
54
void
testSpatialInformation
(
const
std::string &filename)
55
{
56
typedef
DGtal::ImageContainerByITKImage < DGtal::Z3i::Domain, PixelType > DGtalImage;
57
58
DGtalImage img =
DGtal::ITKReader<DGtalImage>::importITK
(filename);
59
60
typename
DGtalImage::ITKImagePointer dgtal_itk = img.getITKImagePointer();
61
62
63
typedef
itk::Image<PixelType, 3> ItkImage;
64
typedef
itk::ImageFileReader<ItkImage> ReaderType;
65
typename
ReaderType::Pointer reader = ReaderType::New();
66
reader->SetFileName(filename);
67
68
reader->Update();
69
70
typename
ItkImage::Pointer itk = reader->GetOutput();
71
72
INFO(
"Checking spacing"
)
73
REQUIRE
( dgtal_itk->GetSpacing() == itk->GetSpacing() );
74
INFO(
"Checking origin"
)
75
REQUIRE
( dgtal_itk->GetOrigin() == itk->GetOrigin() );
76
INFO(
"Checking direction"
)
77
REQUIRE
( dgtal_itk->GetDirection() == itk->GetDirection() );
78
}
79
80
81
82
TEST_CASE
(
"Testing ITKReader"
)
83
{
84
// Default image selector = STLVector
85
typedef
ImageContainerBySTLVector<Z3i::Domain, unsigned char>
Image3DUC;
86
typedef
ImageContainerBySTLVector<Z3i::Domain, uint16_t>
Image3D16b;
87
typedef
ImageContainerBySTLVector<Z3i::Domain, double>
Image3Dd;
88
89
SECTION
(
90
"Testing feature io/readers of ITKReader with 16 bits (uint16) images"
)
91
{
92
Image3D16b im =
ITKReader<Image3D16b>::importITK
(
93
testPath +
"samples/lobsterCroped16b.mhd"
);
94
REQUIRE
( ( im(
Z3i::Point
( 35, 29, 3 ) ) == 60400 ) );
95
}
96
97
SECTION
(
98
"Testing feature io/readers of ITKReader with rescaled 16 bits (uint16) "
99
"images"
)
100
{
101
typedef
DGtal::functors::Rescaling<uint16_t, unsigned char>
RescalFCT;
102
RescalFCT resc = RescalFCT( 0, 65535, 0, 255 );
103
Image3DUC im =
ITKReader<Image3DUC>::importITK
(
104
testPath +
"samples/lobsterCroped16b.mhd"
, resc );
105
REQUIRE
( ( im(
Z3i::Point
( 35, 29, 3 ) ) == resc( 60400 ) ) );
106
}
107
SECTION
(
108
"Testing behavior of ITKReader on non existent image file"
)
109
{
110
bool
caughtException =
false
;
111
const
std::string filename = testPath +
"samples/null.mhd"
;
//non existent file
112
try
113
{
114
Image3DUC im =
ITKReader<Image3DUC>::importITK
(filename);
115
}
116
catch
(exception &)
117
{
118
caughtException =
true
;
119
trace
.
info
() <<
"Exception was correctly caught"
<< std::endl;
120
}
121
REQUIRE
( caughtException ==
true
);
122
}
123
124
SECTION
(
125
"Checking spatial information when loading through DGtal"
)
126
{
127
testSpatialInformation<int16_t>
(testPath +
"samples/lobsterCroped16b.mhd"
);
128
testSpatialInformation<int16_t>
(testPath +
"samples/lobsterCropedB16b.mhd"
);
129
}
130
131
SECTION
(
132
"Checking import with/without shifted domain"
)
133
134
{
135
Image3Dd imShifted =
ITKReader<Image3Dd>::importITK
(testPath +
"samples/lobsterCroped2.nii.gz"
,
136
ITKIOTrait<Image3Dd::Value>::DefaultReadFunctor
(),
true
);
137
138
Image3Dd imNonShifted =
ITKReader<Image3Dd>::importITK
(testPath +
"samples/lobsterCroped2.nii.gz"
,
false
);
139
140
REQUIRE
( ( imShifted(
Z3i::Point
( 144, 100, 21 ) ) == 113 ) );
141
REQUIRE
( ( imNonShifted(
Z3i::Point
( 94, 50, 11 ) ) == 113 ) );
142
Z3i::Point
pTarget (95,76,7);
143
auto
i = 101*pTarget[1]+pTarget[0]+pTarget[2]*101*101;
144
trace
.
info
() <<
"value:"
<< *(imNonShifted.range().begin()+i);
145
REQUIRE
( ( *(imNonShifted.range().begin()+i) == *(imShifted.range().begin()+i ) ));
146
REQUIRE
( ( *(imNonShifted.range().begin()+i) == 68 ));
147
}
148
149
150
}
151
DGtal::ImageContainerBySTLVector
Definition
ImageContainerBySTLVector.h:127
DGtal::Trace::info
std::ostream & info()
DGtal::Z3i::Point
Space::Point Point
Definition
StdDefs.h:168
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::ITKIOTrait::DefaultReadFunctor
functors::Cast< Value > DefaultReadFunctor
Definition
ITKIOTrait.h:65
DGtal::ITKReader::importITK
static Image importITK(const std::string &filename, const TFunctor &aFunctor=TFunctor(), bool shiftDomainUsingOrigin=true)
DGtal::functors::Rescaling
Aim: Functor allowing to rescale a value. Values of the initial scale [initMin,initMax] are rescaled ...
Definition
BasicFunctors.h:906
TEST_CASE
TEST_CASE("int container traits", "[int][traits]")
Definition
testContainerTraits.cpp:53
testSpatialInformation
bool testSpatialInformation()
Definition
testDicomReader.cpp:116
SECTION
SECTION("Testing constant forward iterators")
Definition
testSimpleRandomAccessRangeFromPoint.cpp:66
REQUIRE
REQUIRE(domain.isInside(aPoint))
tests
io
readers
testITKReader.cpp
Generated on
for DGtal by
1.18.0