selxWBIRDemoTest.cxx 27 KB
Newer Older
Floris Berendsen's avatar
Floris Berendsen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*=========================================================================
 *
 *  Copyright Leiden University Medical Center, Erasmus University Medical 
 *  Center and contributors
 *
 *  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
 *
 *        http://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.
 *
 *=========================================================================*/

20
#include "selxSuperElastixFilter.h"
Floris Berendsen's avatar
Floris Berendsen committed
21
22
23
24
25
26
27
28
29
30
31

#include "selxItkSmoothingRecursiveGaussianImageFilterComponent.h"
#include "selxDisplacementFieldItkImageFilterSink.h"
#include "selxItkImageSource.h"

#include "selxElastixComponent.h"
#include "selxItkImageSink.h"

#include "selxItkImageRegistrationMethodv4Component.h"
#include "selxItkANTSNeighborhoodCorrelationImageToImageMetricv4.h"
#include "selxItkMeanSquaresImageToImageMetricv4.h"
32
33
34
35
36
#include "selxItkGradientDescentOptimizerv4.h"
#include "selxItkGaussianExponentialDiffeomorphicTransform.h"
#include "selxItkTransformDisplacementFilter.h"
#include "selxItkResampleFilter.h"

Floris Berendsen's avatar
Floris Berendsen committed
37
38
39
#include "selxItkImageSourceFixed.h"
#include "selxItkImageSourceMoving.h"

40
41
#include "DefaultComponents.h"

42
43
44
45
46
#include "itkTestingComparisonImageFilter.h"
#include "itkSubtractImageFilter.h"
#include "itkVectorMagnitudeImageFilter.h"
#include "itkStatisticsImageFilter.h"

47
#include "selxDataManager.h"
Floris Berendsen's avatar
Floris Berendsen committed
48
49
50
51
52
53
54
#include "gtest/gtest.h"

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

namespace selx {

55
/** Temporary helper function to handle elastix deformation field output */
56
  template < int Dimensionality, typename PixelType> void CopyselxDeformationField(const std::string filename)
Floris Berendsen's avatar
Floris Berendsen committed
57
58
59
60
  {
    typedef itk::ImageFileReader<itk::Image<itk::Vector<PixelType, Dimensionality>, Dimensionality>> ReaderType;
    typedef itk::ImageFileWriter<itk::Image<itk::Vector<PixelType, Dimensionality>, Dimensionality>> WriterType;

FBerendsen's avatar
FBerendsen committed
61
    typename ReaderType::Pointer reader = ReaderType::New();
Floris Berendsen's avatar
Floris Berendsen committed
62
    reader->SetFileName("deformationField.nii");
FBerendsen's avatar
FBerendsen committed
63
    typename WriterType::Pointer writer = WriterType::New();
Floris Berendsen's avatar
Floris Berendsen committed
64
65
66
67
68
    writer->SetFileName(filename);
    writer->SetInput(reader->GetOutput());
    writer->Update();
  };

69
/** A demo for our WBIR paper written as a Unit Test in the Google Test Framework */
Floris Berendsen's avatar
Floris Berendsen committed
70
class WBIRDemoTest : public ::testing::Test {
71
 
Floris Berendsen's avatar
Floris Berendsen committed
72
73
public:
  typedef Overlord::Pointer                 OverlordPointerType;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

  /** Fill SUPERelastix' component data base by registering various components */
  typedef TypeList < 
    DisplacementFieldItkImageFilterSinkComponent<2, float>,
    ItkImageSinkComponent<2, float>,
    ItkImageSourceFixedComponent<2, float>,
    ItkImageSourceMovingComponent<2, float>,
    ElastixComponent<2, float>,
    ItkImageRegistrationMethodv4Component<2, float>,
    ItkANTSNeighborhoodCorrelationImageToImageMetricv4Component<2, float>,
    ItkMeanSquaresImageToImageMetricv4Component < 2, float >,
    ItkGradientDescentOptimizerv4Component<double>,
    ItkGaussianExponentialDiffeomorphicTransformComponent<double, 2>,
    ItkTransformDisplacementFilterComponent<2, float, double >,
    ItkResampleFilterComponent<2, float, double > > RegisterComponents;

  typedef SuperElastixFilter<RegisterComponents>          SuperElastixFilterType;

Floris Berendsen's avatar
Floris Berendsen committed
92
93
94
95
96
97
  typedef Blueprint::Pointer                BlueprintPointerType;
  typedef Blueprint::ConstPointer           BlueprintConstPointerType;
  typedef Blueprint::ParameterMapType       ParameterMapType;
  typedef Blueprint::ParameterValueType     ParameterValueType;
  typedef DataManager DataManagerType;

98
99
100
101
102
  typedef itk::Image<float, 2> Image2DType;
  typedef itk::ImageFileReader<Image2DType> ImageReader2DType;
  typedef itk::ImageFileWriter<Image2DType> ImageWriter2DType;

  typedef itk::Image<itk::Vector<float, 2>, 2> VectorImage2DType;
103
  typedef itk::ImageFileReader<VectorImage2DType> VectorImageReader2DType;
104
105
  typedef itk::ImageFileWriter<VectorImage2DType> VectorImageWriter2DType;

106
107
108
109
110
111
112
  typedef itk::Testing::ComparisonImageFilter<Image2DType, Image2DType> ComparisonImageFilterType;
  // Unfortunately comparing Vector Images cannot by done by this filter.
  // typedef itk::Testing::ComparisonImageFilter<VectorImage2DType, VectorImage2DType> ComparisonVectorImageFilterType;
  //typedef itk::VectorImageToImageAdaptor<float, 2> VectorImageToImageAdaptorType;
  typedef itk::SubtractImageFilter< VectorImage2DType, VectorImage2DType, VectorImage2DType> SubtractVectorImageFilterType;
  typedef itk::VectorMagnitudeImageFilter< VectorImage2DType, Image2DType > VectorMagnitudeImageFilterType;
  typedef itk::StatisticsImageFilter<Image2DType> StatisticsImageFilterType;
113

Floris Berendsen's avatar
Floris Berendsen committed
114
  virtual void SetUp() {
115
116
117
118
119
120
121
122
123
124
125
    baselineImageReader = ImageReader2DType::New();
    compareImageFilter = ComparisonImageFilterType::New();
    compareImageFilter->SetValidInput(baselineImageReader->GetOutput());
    compareImageFilter->SetDifferenceThreshold(0.0);

    baselineVectorImageReader = VectorImageReader2DType::New();
    subtractVectorImageFilter = SubtractVectorImageFilterType::New();
    VectorMagnitudeImageFilterType::Pointer vectorMagnitudeImageFilter = VectorMagnitudeImageFilterType::New();
    statisticsImageFilter = StatisticsImageFilterType::New();

    subtractVectorImageFilter->SetInput1(baselineVectorImageReader->GetOutput());
126
    
127
128
    vectorMagnitudeImageFilter->SetInput(subtractVectorImageFilter->GetOutput());
    statisticsImageFilter->SetInput(vectorMagnitudeImageFilter->GetOutput());
Floris Berendsen's avatar
Floris Berendsen committed
129
130
131
132
133
134
135
  }

  virtual void TearDown() {
    itk::ObjectFactoryBase::UnRegisterAllFactories();
  }

  BlueprintPointerType blueprint;
136
  SuperElastixFilterType::Pointer superElastixFilter;
137
138
139
140
141
142
143

  ImageReader2DType::Pointer baselineImageReader;
  ComparisonImageFilterType::Pointer compareImageFilter;
  VectorImageReader2DType::Pointer baselineVectorImageReader;
  SubtractVectorImageFilterType::Pointer subtractVectorImageFilter;
  StatisticsImageFilterType::Pointer statisticsImageFilter;

Floris Berendsen's avatar
Floris Berendsen committed
144
145
};

146
/** Experiment 2a: ITKv4 framework, stationary velocity field transform, ANTs neighborhood correlation metric */
Floris Berendsen's avatar
Floris Berendsen committed
147
148
149
150
151
TEST_F(WBIRDemoTest, itkv4_SVF_ANTSCC)
{
  /** make example blueprint configuration */
  blueprint = Blueprint::New();

152
153
154
155
  blueprint->AddComponent("RegistrationMethod", { { "NameOfClass", { "ItkImageRegistrationMethodv4Component" } } });
  blueprint->AddComponent("Metric", { { "NameOfClass", { "ItkANTSNeighborhoodCorrelationImageToImageMetricv4Component" } } });
  blueprint->AddComponent("Optimizer", { { "NameOfClass", { "ItkGradientDescentOptimizerv4Component" } },
  { "NumberOfIterations", { "100" } },
156
  { "LearningRate", { "100" } } });
157
  blueprint->AddComponent("Transform", { { "NameOfClass", { "ItkGaussianExponentialDiffeomorphicTransformComponent" } } });
Floris Berendsen's avatar
Floris Berendsen committed
158

159
160
  blueprint->AddComponent("ResampleFilter", { { "NameOfClass", { "ItkResampleFilterComponent" } } });
  blueprint->AddComponent("TransformDisplacementFilter", { { "NameOfClass", { "ItkTransformDisplacementFilterComponent" } } });
Floris Berendsen's avatar
Floris Berendsen committed
161

162
163
164
165
  blueprint->AddComponent("FixedImageSource", { { "NameOfClass", { "ItkImageSourceFixedComponent" } } });
  blueprint->AddComponent("MovingImageSource", { { "NameOfClass", { "ItkImageSourceMovingComponent" } } });
  blueprint->AddComponent("ResultImageSink", { { "NameOfClass", { "ItkImageSinkComponent" } } });
  blueprint->AddComponent("ResultDisplacementFieldSink", { { "NameOfClass", { "DisplacementFieldItkImageFilterSinkComponent" } } });
Floris Berendsen's avatar
Floris Berendsen committed
166

167
  //optionally, tie properties to connection to avoid ambiguities
168
169
  //blueprint->AddConnection("FixedImageSource", "RegistrationMethod", { { "NameOfInterface", { "itkImageFixedInterface" } } });
  blueprint->AddConnection("FixedImageSource", "RegistrationMethod", { {} });
Floris Berendsen's avatar
Floris Berendsen committed
170

171
  //optionally, tie properties to connection to avoid ambiguities
172
173
  //blueprint->AddConnection("MovingImageSource", "RegistrationMethod", { { "NameOfInterface", { "itkImageMovingInterface" } } });
  blueprint->AddConnection("MovingImageSource", "RegistrationMethod", { {} });
Floris Berendsen's avatar
Floris Berendsen committed
174

175
  //optionally, tie properties to connection to avoid ambiguities
176
177
  //blueprint->AddConnection("RegistrationMethod", "ResultImageSink", { { "NameOfInterface", { "itkImageSourceInterface" } } });
  blueprint->AddConnection("ResampleFilter", "ResultImageSink", { {} });
Floris Berendsen's avatar
Floris Berendsen committed
178

179
  //optionally, tie properties to connection to avoid ambiguities
180
181
  //blueprint->AddConnection("RegistrationMethod", "ResultDisplacementFieldSink", { { "NameOfInterface", { "DisplacementFieldItkImageSourceInterface" } } });
  blueprint->AddConnection("TransformDisplacementFilter", "ResultDisplacementFieldSink", { {} });
Floris Berendsen's avatar
Floris Berendsen committed
182

183
  //optionally, tie properties to connection to avoid ambiguities
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  //blueprint->AddConnection("Metric", "RegistrationMethod", { { "NameOfInterface", { "itkMetricv4Interface" } } });
  blueprint->AddConnection("Metric", "RegistrationMethod", { {} });

  blueprint->AddConnection("FixedImageSource", "Transform", { {} });
  blueprint->AddConnection("Transform", "RegistrationMethod", { {} });
  blueprint->AddConnection("Optimizer", "RegistrationMethod", { {} });
  blueprint->AddConnection("RegistrationMethod", "TransformDisplacementFilter", { {} });
  blueprint->AddConnection("FixedImageSource", "TransformDisplacementFilter", { {} });
  blueprint->AddConnection("RegistrationMethod", "ResampleFilter", { {} });
  blueprint->AddConnection("FixedImageSource", "ResampleFilter", { {} });
  blueprint->AddConnection("MovingImageSource", "ResampleFilter", { {} });

  // Data manager provides the paths to the input and output data for unit tests
  DataManagerType::Pointer dataManager = DataManagerType::New();
Floris Berendsen's avatar
Floris Berendsen committed
198

199
  blueprint->WriteBlueprint(dataManager->GetOutputFile("itkv4_SVF_ANTSCC.dot"));
200

201
  // Instantiate SuperElastix
202
  EXPECT_NO_THROW(superElastixFilter = SuperElastixFilterType::New());
Floris Berendsen's avatar
Floris Berendsen committed
203

204
  // Set up the readers and writers
205
206
  ImageReader2DType::Pointer fixedImageReader = ImageReader2DType::New();
  fixedImageReader->SetFileName(dataManager->GetInputFile("coneA2d64.mhd"));
Floris Berendsen's avatar
Floris Berendsen committed
207

208
209
210
  ImageReader2DType::Pointer movingImageReader = ImageReader2DType::New();
  movingImageReader->SetFileName(dataManager->GetInputFile("coneB2d64.mhd"));
  
211
212
  //ImageWriter2DType::Pointer resultImageWriter = ImageWriter2DType::New();
  //resultImageWriter->SetFileName(dataManager->GetOutputFile("itkv4_SVF_ANTSCC_Image.mhd"));
Floris Berendsen's avatar
Floris Berendsen committed
213

214
215
  //VectorImageWriter2DType::Pointer vectorImageWriter = VectorImageWriter2DType::New();
  //vectorImageWriter->SetFileName(dataManager->GetOutputFile("itkv4_SVF_ANTSCC_Displacement.mhd"));
216
  
217
218
219
220
  // Connect SuperElastix in an itk pipeline
  superElastixFilter->SetInput("FixedImageSource", fixedImageReader->GetOutput());
  superElastixFilter->SetInput("MovingImageSource", movingImageReader->GetOutput());

221
222
223
224
225
226
227
228
  //resultImageWriter->SetInput(superElastixFilter->GetOutput<Image2DType>("ResultImageSink"));
  //vectorImageWriter->SetInput(superElastixFilter->GetOutput<VectorImage2DType>("ResultDisplacementFieldSink"));

  baselineImageReader->SetFileName(dataManager->GetBaselineFile("itkv4_SVF_ANTSCC_Image.mhd"));
  baselineVectorImageReader->SetFileName(dataManager->GetBaselineFile("itkv4_SVF_ANTSCC_Displacement.mhd"));

  compareImageFilter->SetTestInput(superElastixFilter->GetOutput<Image2DType>("ResultImageSink"));
  subtractVectorImageFilter->SetInput2(superElastixFilter->GetOutput<VectorImage2DType>("ResultDisplacementFieldSink"));
229
230
231
232
233
234
235

  EXPECT_NO_THROW(superElastixFilter->SetBlueprint(blueprint));

  //Optional Update call
  //superElastixFilter->Update();

  // Update call on the writers triggers SuperElastix to configure and execute
236
237
238
239
240
241
242
243
  //EXPECT_NO_THROW(resultImageWriter->Update());
  //EXPECT_NO_THROW(vectorImageWriter->Update());

  EXPECT_NO_THROW(compareImageFilter->Update());
  EXPECT_NO_THROW(statisticsImageFilter->Update());

  EXPECT_EQ(0, compareImageFilter->GetNumberOfPixelsWithDifferences());
  EXPECT_LT(statisticsImageFilter->GetSumOutput()->Get(), 1e-16);
244
245
246
247
248
249
250
251
252

}

/** Experiment 2b: ITKv4 framework, stationary velocity field transform, mean squared differences metric */
TEST_F(WBIRDemoTest, itkv4_SVF_MSD)
{
  /** make example blueprint configuration */
  blueprint = Blueprint::New();

253
254
255
256
257
258
  blueprint->AddComponent("RegistrationMethod", { { "NameOfClass", { "ItkImageRegistrationMethodv4Component" } } });
  blueprint->AddComponent("Metric", { { "NameOfClass", { "ItkMeanSquaresImageToImageMetricv4Component" } } });
  blueprint->AddComponent("Optimizer", { { "NameOfClass", { "ItkGradientDescentOptimizerv4Component" } },
                                         { "NumberOfIterations", { "100" } },
                                         { "LearningRate", { "0.001" } } });
  blueprint->AddComponent("Transform", { { "NameOfClass", { "ItkGaussianExponentialDiffeomorphicTransformComponent" } } });
259

260
261
  blueprint->AddComponent("ResampleFilter", { { "NameOfClass", { "ItkResampleFilterComponent" } } });
  blueprint->AddComponent("TransformDisplacementFilter", { { "NameOfClass", { "ItkTransformDisplacementFilterComponent" } } });
262

263
264
265
266
  blueprint->AddComponent("FixedImageSource", { { "NameOfClass", { "ItkImageSourceFixedComponent" } } });
  blueprint->AddComponent("MovingImageSource", { { "NameOfClass", { "ItkImageSourceMovingComponent" } } });
  blueprint->AddComponent("ResultImageSink", { { "NameOfClass", { "ItkImageSinkComponent" } } });
  blueprint->AddComponent("ResultDisplacementFieldSink", { { "NameOfClass", { "DisplacementFieldItkImageFilterSinkComponent" } } });
267
268

  //optionally, tie properties to connection to avoid ambiguities
269
270
  //blueprint->AddConnection("FixedImageSource", "RegistrationMethod", { { "NameOfInterface", { "itkImageFixedInterface" } } });
  blueprint->AddConnection("FixedImageSource", "RegistrationMethod", { {} });
271
272

  //optionally, tie properties to connection to avoid ambiguities
273
274
  //blueprint->AddConnection("MovingImageSource", "RegistrationMethod", { { "NameOfInterface", { "itkImageMovingInterface" } } });
  blueprint->AddConnection("MovingImageSource", "RegistrationMethod", { {} });
275
276

  //optionally, tie properties to connection to avoid ambiguities
277
278
  //blueprint->AddConnection("RegistrationMethod", "ResultImageSink", { { "NameOfInterface", { "itkImageSourceInterface" } } });
  blueprint->AddConnection("ResampleFilter", "ResultImageSink", { {} });
279
280

  //optionally, tie properties to connection to avoid ambiguities
281
282
  //blueprint->AddConnection("RegistrationMethod", "ResultDisplacementFieldSink", { { "NameOfInterface", { "DisplacementFieldItkImageSourceInterface" } } });
  blueprint->AddConnection("TransformDisplacementFilter", "ResultDisplacementFieldSink", { {} });
283
284

  //optionally, tie properties to connection to avoid ambiguities
285
286
287
288
289
290
291
292
293
294
295
  //blueprint->AddConnection("Metric", "RegistrationMethod", { { "NameOfInterface", { "itkMetricv4Interface" } } });
  blueprint->AddConnection("Metric", "RegistrationMethod", { {} });

  blueprint->AddConnection("FixedImageSource", "Transform", { {} });
  blueprint->AddConnection("Transform", "RegistrationMethod", { {} });
  blueprint->AddConnection("Optimizer", "RegistrationMethod", { {} });
  blueprint->AddConnection("RegistrationMethod", "TransformDisplacementFilter", { {} });
  blueprint->AddConnection("FixedImageSource", "TransformDisplacementFilter", { {} });
  blueprint->AddConnection("RegistrationMethod", "ResampleFilter", { {} });
  blueprint->AddConnection("FixedImageSource", "ResampleFilter", { {} });
  blueprint->AddConnection("MovingImageSource", "ResampleFilter", { {} });
296

297
298
299
300
  // Data manager provides the paths to the input and output data for unit tests
  DataManagerType::Pointer dataManager = DataManagerType::New();

  blueprint->WriteBlueprint(dataManager->GetOutputFile("itkv4_SVF_MSD.dot"));
301
302
303
304

  // Instantiate SuperElastix
  EXPECT_NO_THROW(superElastixFilter = SuperElastixFilterType::New());

305
  
306
307
308
309
310
311
312
  // Set up the readers and writers
  ImageReader2DType::Pointer fixedImageReader = ImageReader2DType::New();
  fixedImageReader->SetFileName(dataManager->GetInputFile("coneA2d64.mhd"));

  ImageReader2DType::Pointer movingImageReader = ImageReader2DType::New();
  movingImageReader->SetFileName(dataManager->GetInputFile("coneB2d64.mhd"));

313
314
  //ImageWriter2DType::Pointer resultImageWriter = ImageWriter2DType::New();
  //resultImageWriter->SetFileName(dataManager->GetOutputFile("itkv4_SVF_MSD_Image.mhd"));
315

316
317
  //VectorImageWriter2DType::Pointer vectorImageWriter = VectorImageWriter2DType::New();
  //vectorImageWriter->SetFileName(dataManager->GetOutputFile("itkv4_SVF_MSD_Displacement.mhd"));
318
319

  // Connect SuperElastix in an itk pipeline
320
321
  superElastixFilter->SetInput("FixedImageSource", fixedImageReader->GetOutput());
  superElastixFilter->SetInput("MovingImageSource", movingImageReader->GetOutput());
Floris Berendsen's avatar
Floris Berendsen committed
322

323
324
  //resultImageWriter->SetInput(superElastixFilter->GetOutput<Image2DType>("ResultImageSink"));
  //vectorImageWriter->SetInput(superElastixFilter->GetOutput<VectorImage2DType>("ResultDisplacementFieldSink"));
Floris Berendsen's avatar
Floris Berendsen committed
325

326
327
  baselineImageReader->SetFileName(dataManager->GetBaselineFile("itkv4_SVF_MSD_Image.mhd"));
  baselineVectorImageReader->SetFileName(dataManager->GetBaselineFile("itkv4_SVF_MSD_Displacement.mhd"));
Floris Berendsen's avatar
Floris Berendsen committed
328

329
330
331
332
333
  compareImageFilter->SetTestInput(superElastixFilter->GetOutput<Image2DType>("ResultImageSink"));
  subtractVectorImageFilter->SetInput2(superElastixFilter->GetOutput<VectorImage2DType>("ResultDisplacementFieldSink"));

  EXPECT_NO_THROW(superElastixFilter->SetBlueprint(blueprint));
    
334
  //Optional Update call
335
336
  //superElastixFilter->Update();

337
  // Update call on the writers triggers SuperElastix to configure and execute
338
339
  //EXPECT_NO_THROW(resultImageWriter->Update());
  //EXPECT_NO_THROW(vectorImageWriter->Update());
340

341
342
343
344
345
346
  EXPECT_NO_THROW(compareImageFilter->Update());
  EXPECT_NO_THROW(statisticsImageFilter->Update());

  EXPECT_EQ(0, compareImageFilter->GetNumberOfPixelsWithDifferences());
  EXPECT_LT(statisticsImageFilter->GetSumOutput()->Get(), 1e-16);
  }
Floris Berendsen's avatar
Floris Berendsen committed
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/** Experiment 1a: elastix framework, B-spline transform, normalized correlation metric */
TEST_F(WBIRDemoTest, elastix_BS_NCC)
{
  /** make example blueprint configuration */
  blueprint = Blueprint::New();

  ParameterMapType component0Parameters;
  component0Parameters["NameOfClass"] = { "ElastixComponent" };
  //component0Parameters["RegistrationPreset"] = { "rigid" };
  component0Parameters["Transform"] = { "BSplineTransform" }; 
  component0Parameters["Metric"] = { "AdvancedNormalizedCorrelation" };
  blueprint->AddComponent("RegistrationMethod", component0Parameters);

  ParameterMapType component1Parameters;
  component1Parameters["NameOfClass"] = { "ItkImageSourceFixedComponent" };
  blueprint->AddComponent("FixedImageSource", component1Parameters);

  ParameterMapType component2Parameters;
  component2Parameters["NameOfClass"] = { "ItkImageSourceMovingComponent" };
  blueprint->AddComponent("MovingImageSource", component2Parameters);

  ParameterMapType component3Parameters;
  component3Parameters["NameOfClass"] = { "ItkImageSinkComponent" };
  blueprint->AddComponent("ResultImageSink", component3Parameters);

  ParameterMapType connection1Parameters;
  //optionally, tie properties to connection to avoid ambiguities
  //connection1Parameters["NameOfInterface"] = { "itkImageFixedInterface" };
  blueprint->AddConnection("FixedImageSource", "RegistrationMethod", connection1Parameters);

  ParameterMapType connection2Parameters;
  //optionally, tie properties to connection to avoid ambiguities
  //connection2Parameters["NameOfInterface"] = { "itkImageMovingInterface" };
  blueprint->AddConnection("MovingImageSource", "RegistrationMethod", connection2Parameters);

  ParameterMapType connection3Parameters;
  //optionally, tie properties to connection to avoid ambiguities
  //connection3Parameters["NameOfInterface"] = { "GetItkImageInterface" };
  blueprint->AddConnection("RegistrationMethod", "ResultImageSink", connection3Parameters);

  blueprint->WriteBlueprint("elastix_BS_NCC.dot");

  // Instantiate SuperElastix
  EXPECT_NO_THROW(superElastixFilter = SuperElastixFilterType::New());

  // Data manager provides the paths to the input and output data for unit tests
  DataManagerType::Pointer dataManager = DataManagerType::New();

  // Set up the readers and writers
  ImageReader2DType::Pointer fixedImageReader = ImageReader2DType::New();
  fixedImageReader->SetFileName(dataManager->GetInputFile("coneA2d64.mhd"));

  ImageReader2DType::Pointer movingImageReader = ImageReader2DType::New();
  movingImageReader->SetFileName(dataManager->GetInputFile("coneB2d64.mhd"));

403
404
  //ImageWriter2DType::Pointer resultImageWriter = ImageWriter2DType::New();
  //resultImageWriter->SetFileName(dataManager->GetOutputFile("elastix_BS_NCC_Image.mhd"));
405

406
407
  //VectorImageWriter2DType::Pointer vectorImageWriter = VectorImageWriter2DType::New();
  //vectorImageWriter->SetFileName(dataManager->GetOutputFile("elastix_BS_NCC_Displacement.mhd"));
408
409
410
411
412

  // Connect SuperElastix in an itk pipeline
  superElastixFilter->SetInput("FixedImageSource", fixedImageReader->GetOutput());
  superElastixFilter->SetInput("MovingImageSource", movingImageReader->GetOutput());

413
414
415
416
417
418
419
420
421
422
423
424
425
  //resultImageWriter->SetInput(superElastixFilter->GetOutput<Image2DType>("ResultImageSink"));
  //vectorImageWriter->SetInput(superElastixFilter->GetOutput<VectorImage2DType>("ResultDisplacementFieldSink"));
  
  baselineImageReader->SetFileName(dataManager->GetBaselineFile("elastix_BS_NCC_Image.mhd"));
  baselineVectorImageReader->SetFileName(dataManager->GetBaselineFile("elastix_BS_NCC_Displacement.mhd"));

  compareImageFilter->SetTestInput(superElastixFilter->GetOutput<Image2DType>("ResultImageSink"));
  //elastix component does not have an deformation output, but writes deformationfield to disk.
  //subtractVectorImageFilter->SetInput2(superElastixFilter->GetOutput<VectorImage2DType>("ResultDisplacementFieldSink"));
  VectorImageReader2DType::Pointer elastixDeformationFieldReader = VectorImageReader2DType::New();
  elastixDeformationFieldReader->SetFileName("deformationField.nii");
  subtractVectorImageFilter->SetInput2(elastixDeformationFieldReader->GetOutput());
  
426
427
428
429
430
431
  EXPECT_NO_THROW(superElastixFilter->SetBlueprint(blueprint));

  //Optional Update call
  //superElastixFilter->Update();

  // Update call on the writers triggers SuperElastix to configure and execute
432
  //EXPECT_NO_THROW(resultImageWriter->Update());
433
434
  //EXPECT_NO_THROW(vectorImageWriter->Update());

435
436
437
438
439
440
  EXPECT_NO_THROW(compareImageFilter->Update());
  EXPECT_NO_THROW(statisticsImageFilter->Update());

  EXPECT_EQ(0, compareImageFilter->GetNumberOfPixelsWithDifferences());
  EXPECT_LT(statisticsImageFilter->GetSumOutput()->Get(), 1e-16);

441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
}

/** Experiment 1b: elastix framework, B-spline transform, mean squared differences metric */
TEST_F(WBIRDemoTest, elastix_BS_MSD)
{
  /** make example blueprint configuration */
  blueprint = Blueprint::New();

  ParameterMapType component0Parameters;
  component0Parameters["NameOfClass"] = { "ElastixComponent" };
  //component0Parameters["RegistrationPreset"] = { "rigid" };
  component0Parameters["Transform"] = { "BSplineTransform" };
  component0Parameters["Metric"] = { "AdvancedMeanSquares" };
  blueprint->AddComponent("RegistrationMethod", component0Parameters);

  ParameterMapType component1Parameters;
  component1Parameters["NameOfClass"] = { "ItkImageSourceFixedComponent" };
  blueprint->AddComponent("FixedImageSource", component1Parameters);

  ParameterMapType component2Parameters;
  component2Parameters["NameOfClass"] = { "ItkImageSourceMovingComponent" };
  blueprint->AddComponent("MovingImageSource", component2Parameters);

  ParameterMapType component3Parameters;
  component3Parameters["NameOfClass"] = { "ItkImageSinkComponent" };
  blueprint->AddComponent("ResultImageSink", component3Parameters);

  ParameterMapType connection1Parameters;
  //optionally, tie properties to connection to avoid ambiguities
  //connection1Parameters["NameOfInterface"] = { "itkImageFixedInterface" };
  blueprint->AddConnection("FixedImageSource", "RegistrationMethod", connection1Parameters);

  ParameterMapType connection2Parameters;
  //optionally, tie properties to connection to avoid ambiguities
  //connection2Parameters["NameOfInterface"] = { "itkImageMovingInterface" };
  blueprint->AddConnection("MovingImageSource", "RegistrationMethod", connection2Parameters);

  ParameterMapType connection3Parameters;
  //optionally, tie properties to connection to avoid ambiguities
  //connection3Parameters["NameOfInterface"] = { "GetItkImageInterface" };
  blueprint->AddConnection("RegistrationMethod", "ResultImageSink", connection3Parameters);

  blueprint->WriteBlueprint("elastix_BS_MSD.dot");

  // Instantiate SuperElastix
  EXPECT_NO_THROW(superElastixFilter = SuperElastixFilterType::New());

  // Data manager provides the paths to the input and output data for unit tests
  DataManagerType::Pointer dataManager = DataManagerType::New();

  // Set up the readers and writers
  ImageReader2DType::Pointer fixedImageReader = ImageReader2DType::New();
  fixedImageReader->SetFileName(dataManager->GetInputFile("coneA2d64.mhd"));

  ImageReader2DType::Pointer movingImageReader = ImageReader2DType::New();
  movingImageReader->SetFileName(dataManager->GetInputFile("coneB2d64.mhd"));

498
499
  //ImageWriter2DType::Pointer resultImageWriter = ImageWriter2DType::New();
  //resultImageWriter->SetFileName(dataManager->GetOutputFile("elastix_BS_MSD_Image.mhd"));
500

501
502
  //VectorImageWriter2DType::Pointer vectorImageWriter = VectorImageWriter2DType::New();
  //vectorImageWriter->SetFileName(dataManager->GetOutputFile("elastix_BS_MSD_Displacement.mhd"));
503
504
505
506
507

  // Connect SuperElastix in an itk pipeline
  superElastixFilter->SetInput("FixedImageSource", fixedImageReader->GetOutput());
  superElastixFilter->SetInput("MovingImageSource", movingImageReader->GetOutput());

508
509
510
511
512
513
514
515
516
517
518
519
  //resultImageWriter->SetInput(superElastixFilter->GetOutput<Image2DType>("ResultImageSink"));
  //vectorImageWriter->SetInput(superElastixFilter->GetOutput<VectorImage2DType>("ResultDisplacementFieldSink"));

  baselineImageReader->SetFileName(dataManager->GetBaselineFile("elastix_BS_MSD_Image.mhd"));
  baselineVectorImageReader->SetFileName(dataManager->GetBaselineFile("elastix_BS_MSD_Displacement.mhd"));

  compareImageFilter->SetTestInput(superElastixFilter->GetOutput<Image2DType>("ResultImageSink"));
  //elastix component does not have an deformation output, but writes deformationfield to disk.
  //subtractVectorImageFilter->SetInput2(superElastixFilter->GetOutput<VectorImage2DType>("ResultDisplacementFieldSink"));
  VectorImageReader2DType::Pointer elastixDeformationFieldReader = VectorImageReader2DType::New();
  elastixDeformationFieldReader->SetFileName("deformationField.nii");
  subtractVectorImageFilter->SetInput2(elastixDeformationFieldReader->GetOutput());
520
521
522
523
524
525
526

  EXPECT_NO_THROW(superElastixFilter->SetBlueprint(blueprint));

  //Optional Update call
  //superElastixFilter->Update();

  // Update call on the writers triggers SuperElastix to configure and execute
527
  //EXPECT_NO_THROW(resultImageWriter->Update());
528
  //EXPECT_NO_THROW(vectorImageWriter->Update());
529
530
531
532
533
534

  EXPECT_NO_THROW(compareImageFilter->Update());
  EXPECT_NO_THROW(statisticsImageFilter->Update());

  EXPECT_EQ(0, compareImageFilter->GetNumberOfPixelsWithDifferences());
  EXPECT_LT(statisticsImageFilter->GetSumOutput()->Get(), 1e-16);
535
536
}

537
} // namespace selx
Floris Berendsen's avatar
Floris Berendsen committed
538