selxRegistrationItkv4Test.cxx 19.7 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"
21
22
23
24
25
26
27
28
29

#include "TransformComponent1.h"
#include "MetricComponent1.h"
#include "GDOptimizer3rdPartyComponent.h"
#include "GDOptimizer4thPartyComponent.h"
#include "SSDMetric3rdPartyComponent.h"
#include "SSDMetric4thPartyComponent.h"

#include "selxItkSmoothingRecursiveGaussianImageFilterComponent.h"
30
#include "selxDisplacementFieldItkImageFilterSink.h"
31
#include "selxItkImageSource.h"
32
#include "selxItkImageSink.h"
33
34

#include "selxItkImageRegistrationMethodv4Component.h"
35
#include "selxItkANTSNeighborhoodCorrelationImageToImageMetricv4.h"
36
#include "selxItkMeanSquaresImageToImageMetricv4.h"
37
#include "selxItkGradientDescentOptimizerv4.h"
38
39
40
#include "selxItkImageSourceFixed.h"
#include "selxItkImageSourceMoving.h"

Floris Berendsen's avatar
Floris Berendsen committed
41
42
43
44
45
46
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"

#include "itkMeshFileReader.h"
#include "itkMeshFileWriter.h" 

47
48
#include "DefaultComponents.h"

49
#include "selxDataManager.h"
50
51
52
53
54
55
56
57
58
59
#include "gtest/gtest.h"

namespace selx {

class RegistrationItkv4Test : public ::testing::Test {
public:
  typedef Blueprint::Pointer                BlueprintPointerType;
  typedef Blueprint::ConstPointer           BlueprintConstPointerType;
  typedef Blueprint::ParameterMapType       ParameterMapType;
  typedef Blueprint::ParameterValueType     ParameterValueType;
60
  typedef DataManager DataManagerType;
61
62
  
     /** register all example components */
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  typedef TypeList < TransformComponent1,
    MetricComponent1,
    GDOptimizer3rdPartyComponent,
    GDOptimizer4thPartyComponent,
    SSDMetric3rdPartyComponent,
    SSDMetric4thPartyComponent,
    DisplacementFieldItkImageFilterSinkComponent<3, double>,
    DisplacementFieldItkImageFilterSinkComponent<2, float>,
    ItkImageSinkComponent<3, double>,
    ItkImageSinkComponent<2, float>,
    ItkImageSourceFixedComponent<2, float>,
    ItkImageSourceMovingComponent<2, float>,
    ItkImageSourceFixedComponent<3, double>,
    ItkImageSourceMovingComponent<3, double>,
    ItkSmoothingRecursiveGaussianImageFilterComponent<3, double>,
    ItkSmoothingRecursiveGaussianImageFilterComponent<2, double>,
    ItkSmoothingRecursiveGaussianImageFilterComponent<3, float>,
    ItkSmoothingRecursiveGaussianImageFilterComponent<2, float>,
    ItkImageRegistrationMethodv4Component<3, double>,
    ItkImageRegistrationMethodv4Component<2, float>,
    ItkANTSNeighborhoodCorrelationImageToImageMetricv4Component<3, double>,
    ItkMeanSquaresImageToImageMetricv4Component<3, double>,
    ItkANTSNeighborhoodCorrelationImageToImageMetricv4Component<2, float>,
86
87
    ItkMeanSquaresImageToImageMetricv4Component < 2, float >,
    ItkGradientDescentOptimizerv4Component<double>> RegisterComponents;
88
89
	
  typedef SuperElastixFilter<RegisterComponents>          SuperElastixFilterType;
90
91
92
93
94
95
96
97

  typedef itk::Image<float, 2> Image2DType;
  typedef itk::ImageFileReader<Image2DType> ImageReader2DType;
  typedef itk::ImageFileWriter<Image2DType> ImageWriter2DType;

  typedef itk::Image<double, 3> Image3DType;
  typedef itk::ImageFileReader<Image3DType> ImageReader3DType;
  typedef itk::ImageFileWriter<Image3DType> ImageWriter3DType;
98

99
100
  typedef itk::Image<itk::Vector<double,3>, 3> DisplacementImage3DType;
  typedef itk::ImageFileWriter<DisplacementImage3DType> DisplacementImageWriter3DType;
101
102

  virtual void SetUp() {
103
 
104

105
106
107
108
109
110
111
  }

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

  BlueprintPointerType blueprint;
Floris Berendsen's avatar
Floris Berendsen committed
112
  
113

114
115
};

116
TEST_F(RegistrationItkv4Test, ImagesOnly)
117
{
118
119
  /** make example blueprint configuration */
  blueprint = Blueprint::New();
120

121
122
  ParameterMapType component0Parameters;
  component0Parameters["NameOfClass"] = { "ItkImageRegistrationMethodv4Component" };
123
  component0Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
124
  blueprint->AddComponent("RegistrationMethod", component0Parameters);
125

126
127
  ParameterMapType component1Parameters;
  component1Parameters["NameOfClass"] = { "ItkImageSourceFixedComponent" };
128
  component1Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
129
  blueprint->AddComponent("FixedImageSource", component1Parameters);
130

131
132
  ParameterMapType component2Parameters;
  component2Parameters["NameOfClass"] = { "ItkImageSourceMovingComponent" };
133
  component2Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
134
  blueprint->AddComponent("MovingImageSource", component2Parameters);
135

136
  ParameterMapType component3Parameters;
137
  component3Parameters["NameOfClass"] = { "ItkImageSinkComponent" };
138
  component3Parameters["Dimensionality"] = { "3" }; // should be derived from the outputs
139
  blueprint->AddComponent("ResultImageSink", component3Parameters);
140
141


142
  ParameterMapType connection1Parameters;
143
  connection1Parameters["NameOfInterface"] = { "itkImageFixedInterface" };
144
  blueprint->AddConnection("FixedImageSource", "RegistrationMethod", connection1Parameters);
145

146
  ParameterMapType connection2Parameters;
147
  connection2Parameters["NameOfInterface"] = { "itkImageMovingInterface" };
148
  blueprint->AddConnection("MovingImageSource", "RegistrationMethod", connection2Parameters);
149

150
  ParameterMapType connection3Parameters;
151
  connection3Parameters["NameOfInterface"] = { "itkImageInterface" };
152
  blueprint->AddConnection("RegistrationMethod", "ResultImageSink", connection3Parameters);
153

154
155
156
157
158
  // Instantiate SuperElastix
  SuperElastixFilterType::Pointer superElastixFilter;
  EXPECT_NO_THROW(superElastixFilter = SuperElastixFilterType::New());

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

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  // Set up the readers and writers
  ImageReader3DType::Pointer fixedImageReader = ImageReader3DType::New();
  fixedImageReader->SetFileName(dataManager->GetInputFile("sphereA3d.mhd"));

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

  ImageWriter3DType::Pointer resultImageWriter = ImageWriter3DType::New();
  resultImageWriter->SetFileName(dataManager->GetOutputFile("RegistrationItkv4Test_ImagesOnly.mhd"));

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

  EXPECT_NO_THROW(superElastixFilter->SetBlueprint(blueprint));

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

  // Update call on the writers triggers SuperElastix to configure and execute
  EXPECT_NO_THROW(resultImageWriter->Update());
    
184
185
}

186
187
188
189
190
191
192
TEST_F(RegistrationItkv4Test, WithANTSCCMetric)
{
  /** make example blueprint configuration */
  blueprint = Blueprint::New();

  ParameterMapType component0Parameters;
  component0Parameters["NameOfClass"] = { "ItkImageRegistrationMethodv4Component" };
193
  component0Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
194
  blueprint->AddComponent("RegistrationMethod", component0Parameters);
195
196
197

  ParameterMapType component1Parameters;
  component1Parameters["NameOfClass"] = { "ItkImageSourceFixedComponent" };
198
  component1Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
199
  blueprint->AddComponent("FixedImageSource", component1Parameters);
200

201
202
  ParameterMapType component2Parameters;
  component2Parameters["NameOfClass"] = { "ItkImageSourceMovingComponent" };
203
  component2Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
204
  blueprint->AddComponent("MovingImageSource", component2Parameters);
205
206

  ParameterMapType component3Parameters;
207
  component3Parameters["NameOfClass"] = { "ItkImageSinkComponent" };
208
  component3Parameters["Dimensionality"] = { "3" }; // should be derived from the outputs
209
  blueprint->AddComponent("ResultImageSink", component3Parameters);
210
211
212

  ParameterMapType component4Parameters;
  component4Parameters["NameOfClass"] = { "ItkANTSNeighborhoodCorrelationImageToImageMetricv4Component" };
213
  component4Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
214
  blueprint->AddComponent("Metric", component4Parameters);
215

216
217
218
  ParameterMapType component5Parameters;
  component5Parameters["NameOfClass"] = { "ItkGradientDescentOptimizerv4Component" };
  blueprint->AddComponent("Optimizer", component5Parameters);
219
220

  ParameterMapType connection1Parameters;
221
  connection1Parameters["NameOfInterface"] = { "itkImageFixedInterface" };
222
  blueprint->AddConnection("FixedImageSource", "RegistrationMethod", connection1Parameters);
223
224

  ParameterMapType connection2Parameters;
225
  connection2Parameters["NameOfInterface"] = { "itkImageMovingInterface" };
226
  blueprint->AddConnection("MovingImageSource", "RegistrationMethod", connection2Parameters);
227
228

  ParameterMapType connection3Parameters;
229
  connection3Parameters["NameOfInterface"] = { "itkImageInterface" };
230
231
  blueprint->AddConnection("RegistrationMethod", "ResultImageSink", connection3Parameters);
  
232
233
  ParameterMapType connection4Parameters;
  connection4Parameters["NameOfInterface"] = { "itkMetricv4Interface" };
234
  blueprint->AddConnection("Metric", "RegistrationMethod", connection4Parameters);
235

236
237
238
239
240
  ParameterMapType connection5Parameters;
  connection5Parameters["NameOfInterface"] = { "itkOptimizerv4Interface" };
  blueprint->AddConnection("Optimizer", "RegistrationMethod", connection5Parameters);


241
242
243
244
245
  // Instantiate SuperElastix
  SuperElastixFilterType::Pointer superElastixFilter;
  EXPECT_NO_THROW(superElastixFilter = SuperElastixFilterType::New());

  // Data manager provides the paths to the input and output data for unit tests
246
  DataManagerType::Pointer dataManager = DataManagerType::New();
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269

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

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

  ImageWriter3DType::Pointer resultImageWriter = ImageWriter3DType::New();
  resultImageWriter->SetFileName(dataManager->GetOutputFile("RegistrationItkv4Test_WithANTSCCMetric.mhd"));

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

  EXPECT_NO_THROW(superElastixFilter->SetBlueprint(blueprint));

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

  // Update call on the writers triggers SuperElastix to configure and execute
  EXPECT_NO_THROW(resultImageWriter->Update());
270
}
271
272
273
274
275
276
277
TEST_F(RegistrationItkv4Test, WithMeanSquaresMetric)
{
  /** make example blueprint configuration */
  blueprint = Blueprint::New();

  ParameterMapType component0Parameters;
  component0Parameters["NameOfClass"] = { "ItkImageRegistrationMethodv4Component" };
278
  component0Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
279
  blueprint->AddComponent("RegistrationMethod", component0Parameters);
280
281
282

  ParameterMapType component1Parameters;
  component1Parameters["NameOfClass"] = { "ItkImageSourceFixedComponent" };
283
  component1Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
284
  blueprint->AddComponent("FixedImageSource", component1Parameters);
285
286
287

  ParameterMapType component2Parameters;
  component2Parameters["NameOfClass"] = { "ItkImageSourceMovingComponent" };
288
  component2Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
289
  blueprint->AddComponent("MovingImageSource", component2Parameters);
290
291

  ParameterMapType component3Parameters;
292
  component3Parameters["NameOfClass"] = { "ItkImageSinkComponent" };
293
  component3Parameters["Dimensionality"] = { "3" }; // should be derived from the outputs
294
  blueprint->AddComponent("ResultImageSink", component3Parameters);
295
296
297

  ParameterMapType component4Parameters;
  component4Parameters["NameOfClass"] = { "ItkMeanSquaresImageToImageMetricv4Component" };
298
  component4Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
299
  blueprint->AddComponent("Metric", component4Parameters);
300

301
302

  ParameterMapType connection1Parameters;
303
  connection1Parameters["NameOfInterface"] = { "itkImageFixedInterface" };
304
  blueprint->AddConnection("FixedImageSource", "RegistrationMethod", connection1Parameters);
305
306

  ParameterMapType connection2Parameters;
307
  connection2Parameters["NameOfInterface"] = { "itkImageMovingInterface" };
308
  blueprint->AddConnection("MovingImageSource", "RegistrationMethod", connection2Parameters);
309
310

  ParameterMapType connection3Parameters;
311
  connection3Parameters["NameOfInterface"] = { "itkImageInterface" };
312
  blueprint->AddConnection("RegistrationMethod", "ResultImageSink", connection3Parameters);
313
314
315

  ParameterMapType connection4Parameters;
  connection4Parameters["NameOfInterface"] = { "itkMetricv4Interface" };
316
  blueprint->AddConnection("Metric", "RegistrationMethod", connection4Parameters);
317

318
319
320
321
322
  // Instantiate SuperElastix
  SuperElastixFilterType::Pointer superElastixFilter;
  EXPECT_NO_THROW(superElastixFilter = SuperElastixFilterType::New());

  // Data manager provides the paths to the input and output data for unit tests
323
  DataManagerType::Pointer dataManager = DataManagerType::New();
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347

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

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

  ImageWriter3DType::Pointer resultImageWriter = ImageWriter3DType::New();
  resultImageWriter->SetFileName(dataManager->GetOutputFile("RegistrationItkv4Test_WithMeanSquaresMetric.mhd"));

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

  EXPECT_NO_THROW(superElastixFilter->SetBlueprint(blueprint));

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

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

348
}
349

350
TEST_F(RegistrationItkv4Test, DisplacementField)
351
352
353
354
355
356
{
  /** make example blueprint configuration */
  blueprint = Blueprint::New();

  ParameterMapType component0Parameters;
  component0Parameters["NameOfClass"] = { "ItkImageRegistrationMethodv4Component" };
357
  component0Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
358
  blueprint->AddComponent("RegistrationMethod", component0Parameters);
359
360
361

  ParameterMapType component1Parameters;
  component1Parameters["NameOfClass"] = { "ItkImageSourceFixedComponent" };
362
  component1Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
363
  blueprint->AddComponent("FixedImageSource", component1Parameters);
364
365
366

  ParameterMapType component2Parameters;
  component2Parameters["NameOfClass"] = { "ItkImageSourceMovingComponent" };
367
  component2Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
368
  blueprint->AddComponent("MovingImageSource", component2Parameters);
369
370

  ParameterMapType component3Parameters;
371
  component3Parameters["NameOfClass"] = { "ItkImageSinkComponent" };
372
  component3Parameters["Dimensionality"] = { "3" }; // should be derived from the outputs
373
  blueprint->AddComponent("ResultImageSink", component3Parameters);
374
375
376

  ParameterMapType component4Parameters;
  component4Parameters["NameOfClass"] = { "DisplacementFieldItkImageFilterSinkComponent" };
377
  component4Parameters["Dimensionality"] = { "3" }; // should be derived from the outputs
378
  blueprint->AddComponent("ResultDisplacementFieldSink", component4Parameters);
379

380
381
  ParameterMapType component5Parameters;
  component5Parameters["NameOfClass"] = { "ItkANTSNeighborhoodCorrelationImageToImageMetricv4Component" };
382
  component5Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
383
  blueprint->AddComponent("Metric", component5Parameters);
384
385


386
  ParameterMapType connection1Parameters;
387
  connection1Parameters["NameOfInterface"] = { "itkImageFixedInterface" };
388
  blueprint->AddConnection("FixedImageSource", "RegistrationMethod", connection1Parameters);
389
390

  ParameterMapType connection2Parameters;
391
  connection2Parameters["NameOfInterface"] = { "itkImageMovingInterface" };
392
  blueprint->AddConnection("MovingImageSource", "RegistrationMethod", connection2Parameters);
393
394

  ParameterMapType connection3Parameters;
395
  connection3Parameters["NameOfInterface"] = { "itkImageInterface" };
396
  blueprint->AddConnection("RegistrationMethod", "ResultImageSink", connection3Parameters);
397
398
399

  ParameterMapType connection4Parameters;
  connection4Parameters["NameOfInterface"] = { "DisplacementFieldItkImageSourceInterface" };
400
  blueprint->AddConnection("RegistrationMethod", "ResultDisplacementFieldSink", connection4Parameters);
401

402
403
  ParameterMapType connection5Parameters;
  connection5Parameters["NameOfInterface"] = { "itkMetricv4Interface" };
404
  blueprint->AddConnection("Metric", "RegistrationMethod", connection5Parameters);
405

406
407
408
  // Instantiate SuperElastix
  SuperElastixFilterType::Pointer superElastixFilter;
  EXPECT_NO_THROW(superElastixFilter = SuperElastixFilterType::New());
409

410
  // Data manager provides the paths to the input and output data for unit tests
411
  DataManagerType::Pointer dataManager = DataManagerType::New();
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

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

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

  ImageWriter3DType::Pointer resultImageWriter = ImageWriter3DType::New();
  resultImageWriter->SetFileName(dataManager->GetOutputFile("RegistrationItkv4Test_DisplacementField_image.mhd"));

  DisplacementImageWriter3DType::Pointer resultDisplacementWriter = DisplacementImageWriter3DType::New();
  resultDisplacementWriter->SetFileName(dataManager->GetOutputFile("RegistrationItkv4Test_DisplacementField_displacement.mhd"));


  // Connect SuperElastix in an itk pipeline
  superElastixFilter->SetInput("FixedImageSource", fixedImageReader->GetOutput());
  superElastixFilter->SetInput("MovingImageSource", movingImageReader->GetOutput());
  resultImageWriter->SetInput(superElastixFilter->GetOutput<Image3DType>("ResultImageSink"));
  resultDisplacementWriter->SetInput(superElastixFilter->GetOutput<DisplacementImage3DType>("ResultDisplacementFieldSink"));

  EXPECT_NO_THROW(superElastixFilter->SetBlueprint(blueprint));

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

  // Update call on the writers triggers SuperElastix to configure and execute
  EXPECT_NO_THROW(resultImageWriter->Update());
  EXPECT_NO_THROW(resultDisplacementWriter->Update());

442
}
443
} // namespace selx
444