/
itkSymmetricEigenAnalysisImageFilterTest.cxx
381 lines (297 loc) · 12.7 KB
/
itkSymmetricEigenAnalysisImageFilterTest.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkCastImageFilter.h"
#include "itkImageFileWriter.h"
#include "itkSymmetricSecondRankTensor.h"
#include "itkSymmetricEigenAnalysisImageFilter.h"
#include "itkTestingMacros.h"
#include "itkImageRegionIterator.h"
/**
* Function to convert image of double precison vector pixels to a
* representation of scalar values that are easy to view and test.
* NOTE: Similar function in itkBSplineScatteredDataPointSetToImageFilterTest5.cxx
*/
template <typename InternalImageType>
static void
makeTestableScalarImage(typename InternalImageType::Pointer internalImage, std::string outputFilename)
{ // using OutputPixelType = unsigned char;
using OutputPixelType = uint8_t;
using OutputImageType = itk::Image<OutputPixelType, 3>;
OutputImageType::Pointer outputImage = OutputImageType::New();
outputImage->CopyInformation(internalImage);
outputImage->SetRegions(internalImage->GetBufferedRegion());
outputImage->Allocate(true);
auto myiterator = itk::ImageRegionConstIterator<InternalImageType>(internalImage, internalImage->GetBufferedRegion());
auto myOutiterator = itk::ImageRegionIterator<OutputImageType>(outputImage, outputImage->GetBufferedRegion());
// Convert vector image to magnitude and scale to use range of png values
float max_magnitude_value = 0.0;
while (!myiterator.IsAtEnd())
{
const auto arr = myiterator.Get();
const auto magvalue = std::sqrt(arr[0] * arr[0] + arr[1] * arr[1] + arr[2] * arr[2]);
max_magnitude_value = std::max<float>(max_magnitude_value, magvalue);
++myiterator;
}
const float scale_factor = 255.0 / ceil(max_magnitude_value);
myOutiterator.GoToBegin();
myiterator.GoToBegin();
while (!myOutiterator.IsAtEnd())
{
// Convert vector image to magnitude and scale to use range of png values
const auto arr = myiterator.Get();
const auto magvalue = std::sqrt(arr[0] * arr[0] + arr[1] * arr[1] + arr[2] * arr[2]);
myOutiterator.Set(magvalue * scale_factor);
++myiterator;
++myOutiterator;
}
// Write the result image
using WriterType = itk::ImageFileWriter<OutputImageType>;
auto writer = WriterType::New();
writer->SetFileName(outputFilename);
writer->SetInput(outputImage);
writer->Update();
}
namespace itk
{
template <typename TInputImage, typename TInternalImage, typename TOutputImage>
class SymmetricEigenAnalysisImageFilterHelper : public SymmetricEigenAnalysisImageFilter<TInputImage, TInternalImage>
{
public:
using Self = SymmetricEigenAnalysisImageFilterHelper;
using Superclass = SymmetricEigenAnalysisImageFilter<TInputImage, TInternalImage>;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
using InputImageType = TInputImage;
using InternalImageType = TInternalImage;
itkTypeMacro(SymmetricEigenAnalysisImageFilterHelper, SymmetricEigenAnalysisImageFilter);
itkNewMacro(Self);
static int
Exercise(itk::EigenValueOrderEnum order, std::string outputFilename)
{
using SymmetricEigenAnalysisImageFilterType = SymmetricEigenAnalysisImageFilter<InputImageType, InternalImageType>;
// Declare the type of the index to access images
using IndexType = itk::Index<InputImageType::ImageDimension>;
// Declare the type of the size
using SizeType = itk::Size<InputImageType::ImageDimension>;
// Declare the type of the Region
using RegionType = itk::ImageRegion<InputImageType::ImageDimension>;
// Create the input image
auto inputImage = InputImageType::New();
// Define its size, and start index
SizeType size;
size[0] = 8;
size[1] = 8;
size[2] = 8;
IndexType start;
start.Fill(0);
RegionType region;
region.SetIndex(start);
region.SetSize(size);
// Initialize the input image
inputImage->SetRegions(region);
inputImage->Allocate();
// Declare Iterator type for the input image
using IteratorType = itk::ImageRegionIteratorWithIndex<InputImageType>;
typename InputImageType::PixelType tensorValue;
tensorValue(0, 0) = 19.0;
tensorValue(0, 1) = 23.0;
tensorValue(0, 2) = 29.0;
tensorValue(1, 1) = 31.0;
tensorValue(1, 2) = 37.0;
tensorValue(2, 2) = 39.0;
// Create one iterator for the input image (this is a light object)
IteratorType it(inputImage, inputImage->GetRequestedRegion());
// Initialize the content of the input image
while (!it.IsAtEnd())
{
it.Set(tensorValue);
++it;
}
// Create the filter
auto filter = SymmetricEigenAnalysisImageFilterType::New();
// Dimension should be initialized to the input image dimension
ITK_TEST_EXPECT_EQUAL(filter->GetDimension(), InputImageType::ImageDimension);
ITK_TEST_SET_GET_VALUE(InputImageType::ImageDimension, filter->GetDimension());
// Set the input image
filter->SetInput(inputImage);
filter->SetFunctor(filter->GetFunctor());
filter->OrderEigenValuesBy(order);
filter->SetOrderEigenValuesBy(order);
ITK_TEST_SET_GET_VALUE(order, filter->GetOrderEigenValuesBy());
// Execute the filter
ITK_TRY_EXPECT_NO_EXCEPTION(filter->Update());
// Get the filter output
// It is important to do it AFTER the filter is Updated
// Because the object connected to the output may be changed
// by another during GenerateData() call
typename InternalImageType::Pointer internalImage = filter->GetOutput();
ITK_TRY_EXPECT_NO_EXCEPTION(makeTestableScalarImage<InternalImageType>(internalImage, outputFilename));
std::cout << "Test succeeded." << std::endl;
return EXIT_SUCCESS;
}
};
template <unsigned int TMatrixDimension, typename TInputImage, typename TInternalImage, typename TOutputImage>
class SymmetricEigenAnalysisFixedDimensionImageFilterHelper
: public SymmetricEigenAnalysisFixedDimensionImageFilter<TMatrixDimension, TInputImage, TInternalImage>
{
public:
using Self = SymmetricEigenAnalysisFixedDimensionImageFilterHelper;
using Superclass = SymmetricEigenAnalysisFixedDimensionImageFilter<TMatrixDimension, TInputImage, TInternalImage>;
using Pointer = SmartPointer<Self>;
using ConstPointer = SmartPointer<const Self>;
using InputImageType = TInputImage;
using InternalImageType = TInternalImage;
using OutputImageType = TOutputImage;
itkTypeMacro(SymmetricEigenAnalysisFixedDimensionImageFilterHelper, SymmetricEigenAnalysisFixedDimensionImageFilter);
itkNewMacro(Self);
static int
Exercise(itk::EigenValueOrderEnum order, std::string outputFilename)
{
using SymmetricEigenAnalysisFixedDimensionImageFilterType =
SymmetricEigenAnalysisFixedDimensionImageFilter<TMatrixDimension, InputImageType, InternalImageType>;
// Declare the type of the index to access images
using IndexType = itk::Index<InputImageType::ImageDimension>;
// Declare the type of the size
using SizeType = itk::Size<InputImageType::ImageDimension>;
// Declare the type of the Region
using RegionType = itk::ImageRegion<InputImageType::ImageDimension>;
// Create the input image
auto inputImage = InputImageType::New();
// Define its size, and start index
SizeType size;
size[0] = 8;
size[1] = 8;
size[2] = 8;
IndexType start;
start.Fill(0);
RegionType region;
region.SetIndex(start);
region.SetSize(size);
// Initialize the input image
inputImage->SetRegions(region);
inputImage->Allocate();
// Declare Iterator type for the input image
using IteratorType = itk::ImageRegionIteratorWithIndex<InputImageType>;
typename InputImageType::PixelType tensorValue;
tensorValue(0, 0) = 19.0;
tensorValue(0, 1) = 23.0;
tensorValue(0, 2) = 29.0;
tensorValue(1, 1) = 31.0;
tensorValue(1, 2) = 37.0;
tensorValue(2, 2) = 39.0;
// Create one iterator for the input image (this is a light object)
IteratorType it(inputImage, inputImage->GetRequestedRegion());
// Initialize the content of the input image
while (!it.IsAtEnd())
{
it.Set(tensorValue);
++it;
}
// Create the filter
typename SymmetricEigenAnalysisFixedDimensionImageFilterType::Pointer filter =
SymmetricEigenAnalysisFixedDimensionImageFilterType::New();
// Set the input image
filter->SetInput(inputImage);
filter->SetFunctor(filter->GetFunctor());
filter->OrderEigenValuesBy(order);
// Execute the filter
ITK_TRY_EXPECT_NO_EXCEPTION(filter->Update());
// Get the filter output
// It is important to do it AFTER the filter is Updated
// Because the object connected to the output may be changed
// by another during GenerateData() call
typename InternalImageType::Pointer internalImage = filter->GetOutput();
ITK_TRY_EXPECT_NO_EXCEPTION(makeTestableScalarImage<InternalImageType>(internalImage, outputFilename));
std::cout << "Test succeeded." << std::endl;
return EXIT_SUCCESS;
}
};
} // end namespace itk
int
itkSymmetricEigenAnalysisImageFilterTest(int argc, char * argv[])
{
if (argc < 4)
{
std::cout << "Usage: " << itkNameOfTestExecutableMacro(argv) << "outputImage order outputImageFixedDimension"
<< std::endl;
return EXIT_FAILURE;
}
bool testPassed = true;
// Define the dimension of the images
constexpr unsigned int Dimension = 3;
// Declare the pixel type
using InputPixelType = float;
using InternalPixelType = double;
using OutputPixelType = unsigned char;
// Define the symmetric tensor pixel type
using TensorType = itk::SymmetricSecondRankTensor<InputPixelType, Dimension>;
// Declare the types of the images
using InputImageType = itk::Image<TensorType, Dimension>;
// Define the type for storing the eigen-value
using InternalValueArray = itk::FixedArray<InternalPixelType, Dimension>;
using OutputValueArray = itk::FixedArray<OutputPixelType, Dimension>;
// Declare the types of the output images
using InternalImageType = itk::Image<InternalValueArray, Dimension>;
using OutputImageType = itk::Image<OutputValueArray, Dimension>;
// Declare the type for the filter
using FilterType = itk::SymmetricEigenAnalysisImageFilter<InputImageType, InternalImageType>;
// Create an instance to exercise basic object methods
auto filter = FilterType::New();
ITK_EXERCISE_BASIC_OBJECT_METHODS(filter, SymmetricEigenAnalysisImageFilter, UnaryFunctorImageFilter);
// Get the input arguments
auto order = static_cast<itk::EigenValueOrderEnum>(std::stoi(argv[2]));
const std::string outputFilename = argv[1];
// Test the filter
int testResult =
itk::SymmetricEigenAnalysisImageFilterHelper<InputImageType, InternalImageType, OutputImageType>::Exercise(
order, outputFilename);
if (testResult != EXIT_SUCCESS)
{
std::cout << "test SymmetricEigenAnalysisImageFilter failed" << std::endl;
testPassed = false;
}
// Test the fixed dimension filter
using FilterFixedDimensionType =
itk::SymmetricEigenAnalysisFixedDimensionImageFilter<Dimension, InputImageType, InternalImageType>;
auto orderFixedDimension = static_cast<itk::EigenValueOrderEnum>(std::stoi(argv[2]));
const std::string outputFilenameFixedDimension = argv[3];
// Create an instance to exercise basic object methods
auto filterFixedDimension = FilterFixedDimensionType::New();
ITK_EXERCISE_BASIC_OBJECT_METHODS(
filterFixedDimension, SymmetricEigenAnalysisFixedDimensionImageFilter, UnaryFunctorImageFilter);
int testFixedDimensionResult =
itk::SymmetricEigenAnalysisFixedDimensionImageFilterHelper<Dimension,
InputImageType,
InternalImageType,
OutputImageType>::Exercise(orderFixedDimension,
outputFilenameFixedDimension);
if (testFixedDimensionResult != EXIT_SUCCESS)
{
std::cout << "test SymmetricEigenAnalysisFixedImageImageFilter failed" << std::endl;
testPassed = false;
}
// All objects should be automatically destroyed at this point
if (testPassed)
{
return EXIT_SUCCESS;
}
else
{
return EXIT_FAILURE;
}
}