selxRegistrationItkv4Test.cxx 20.1 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
38
39
#include "selxItkImageSourceFixed.h"
#include "selxItkImageSourceMoving.h"

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

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

46
#include "selxDataManager.h"
47
48
49
50
51
52
53
54
55
56
#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;
57
  typedef DataManager DataManagerType;
58
59
60
61
62
63
64
65
66
  typedef SuperElastixFilter<bool>          SuperElastixFilterType;

  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;
67

68
69
  typedef itk::Image<itk::Vector<double,3>, 3> DisplacementImage3DType;
  typedef itk::ImageFileWriter<DisplacementImage3DType> DisplacementImageWriter3DType;
70
71
72
73
74
75
76
77
78
79

  virtual void SetUp() {
    /** register all example components */
    ComponentFactory<TransformComponent1>::RegisterOneFactory();
    ComponentFactory<MetricComponent1>::RegisterOneFactory();
    ComponentFactory<GDOptimizer3rdPartyComponent>::RegisterOneFactory();
    ComponentFactory<GDOptimizer4thPartyComponent>::RegisterOneFactory();
    ComponentFactory<SSDMetric3rdPartyComponent>::RegisterOneFactory();
    ComponentFactory<SSDMetric4thPartyComponent>::RegisterOneFactory();

80
    ComponentFactory<DisplacementFieldItkImageFilterSinkComponent<3, double>>::RegisterOneFactory();
81
82
    ComponentFactory<DisplacementFieldItkImageFilterSinkComponent<2, float>>::RegisterOneFactory();
    
83
84
    ComponentFactory<ItkImageSinkComponent<3, double>>::RegisterOneFactory();
    ComponentFactory<ItkImageSinkComponent<2, float>>::RegisterOneFactory();
85
86
87
88
89
90

    ComponentFactory<ItkImageSourceFixedComponent<2, float>>::RegisterOneFactory();
    ComponentFactory<ItkImageSourceMovingComponent<2, float>>::RegisterOneFactory();

    ComponentFactory<ItkImageSourceFixedComponent<3, double>>::RegisterOneFactory();
    ComponentFactory<ItkImageSourceMovingComponent<3, double>>::RegisterOneFactory();
91
92
93
94
95
96
97
98


    ComponentFactory<ItkSmoothingRecursiveGaussianImageFilterComponent<3, double>>::RegisterOneFactory();
    ComponentFactory<ItkSmoothingRecursiveGaussianImageFilterComponent<2, double>>::RegisterOneFactory();
    ComponentFactory<ItkSmoothingRecursiveGaussianImageFilterComponent<3, float>>::RegisterOneFactory();
    ComponentFactory<ItkSmoothingRecursiveGaussianImageFilterComponent<2, float>>::RegisterOneFactory();

    ComponentFactory<ItkImageRegistrationMethodv4Component<3, double>>::RegisterOneFactory();
99
    ComponentFactory<ItkImageRegistrationMethodv4Component<2, float>>::RegisterOneFactory();
100

101
    ComponentFactory<ItkANTSNeighborhoodCorrelationImageToImageMetricv4Component<3, double>>::RegisterOneFactory();
102
    ComponentFactory<ItkMeanSquaresImageToImageMetricv4Component<3, double>>::RegisterOneFactory();
103
104
105
106

    ComponentFactory<ItkANTSNeighborhoodCorrelationImageToImageMetricv4Component<2, float>>::RegisterOneFactory();
    ComponentFactory<ItkMeanSquaresImageToImageMetricv4Component<2, float>>::RegisterOneFactory();

107
108
109
110
111
112
113
  }

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

  BlueprintPointerType blueprint;
Floris Berendsen's avatar
Floris Berendsen committed
114
  
115

116
117
};

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

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

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

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

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


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

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

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

156
157
158
159
160
  // 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
161
162
  DataManagerType::Pointer dataManager = DataManagerType::New();

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  // 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());
    
186
187
}

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

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

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

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

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

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


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

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

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

235
236
237
238
239
  // 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
240
  DataManagerType::Pointer dataManager = DataManagerType::New();
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263

  // 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());
264
}
265
266
267
268
269
270
271
TEST_F(RegistrationItkv4Test, WithMeanSquaresMetric)
{
  /** make example blueprint configuration */
  blueprint = Blueprint::New();

  ParameterMapType component0Parameters;
  component0Parameters["NameOfClass"] = { "ItkImageRegistrationMethodv4Component" };
272
  component0Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
273
  blueprint->AddComponent("RegistrationMethod", component0Parameters);
274
275
276

  ParameterMapType component1Parameters;
  component1Parameters["NameOfClass"] = { "ItkImageSourceFixedComponent" };
277
  component1Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
278
  blueprint->AddComponent("FixedImageSource", component1Parameters);
279
280
281

  ParameterMapType component2Parameters;
  component2Parameters["NameOfClass"] = { "ItkImageSourceMovingComponent" };
282
  component2Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
283
  blueprint->AddComponent("MovingImageSource", component2Parameters);
284
285

  ParameterMapType component3Parameters;
286
  component3Parameters["NameOfClass"] = { "ItkImageSinkComponent" };
287
  component3Parameters["Dimensionality"] = { "3" }; // should be derived from the outputs
288
  blueprint->AddComponent("ResultImageSink", component3Parameters);
289
290
291

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

295
296

  ParameterMapType connection1Parameters;
297
  connection1Parameters["NameOfInterface"] = { "itkImageFixedInterface" };
298
  blueprint->AddConnection("FixedImageSource", "RegistrationMethod", connection1Parameters);
299
300

  ParameterMapType connection2Parameters;
301
  connection2Parameters["NameOfInterface"] = { "itkImageMovingInterface" };
302
  blueprint->AddConnection("MovingImageSource", "RegistrationMethod", connection2Parameters);
303
304

  ParameterMapType connection3Parameters;
305
  connection3Parameters["NameOfInterface"] = { "itkImageInterface" };
306
  blueprint->AddConnection("RegistrationMethod", "ResultImageSink", connection3Parameters);
307
308
309

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

312
313
314
315
316
  // 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
317
  DataManagerType::Pointer dataManager = DataManagerType::New();
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

  // 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());

342
}
343

344
TEST_F(RegistrationItkv4Test, DisplacementField)
345
346
347
348
349
350
{
  /** make example blueprint configuration */
  blueprint = Blueprint::New();

  ParameterMapType component0Parameters;
  component0Parameters["NameOfClass"] = { "ItkImageRegistrationMethodv4Component" };
351
  component0Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
352
  blueprint->AddComponent("RegistrationMethod", component0Parameters);
353
354
355

  ParameterMapType component1Parameters;
  component1Parameters["NameOfClass"] = { "ItkImageSourceFixedComponent" };
356
  component1Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
357
  blueprint->AddComponent("FixedImageSource", component1Parameters);
358
359
360

  ParameterMapType component2Parameters;
  component2Parameters["NameOfClass"] = { "ItkImageSourceMovingComponent" };
361
  component2Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
362
  blueprint->AddComponent("MovingImageSource", component2Parameters);
363
364

  ParameterMapType component3Parameters;
365
  component3Parameters["NameOfClass"] = { "ItkImageSinkComponent" };
366
  component3Parameters["Dimensionality"] = { "3" }; // should be derived from the outputs
367
  blueprint->AddComponent("ResultImageSink", component3Parameters);
368
369
370

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

374
375
  ParameterMapType component5Parameters;
  component5Parameters["NameOfClass"] = { "ItkANTSNeighborhoodCorrelationImageToImageMetricv4Component" };
376
  component5Parameters["Dimensionality"] = { "3" }; // should be derived from the inputs
377
  blueprint->AddComponent("Metric", component5Parameters);
378
379


380
  ParameterMapType connection1Parameters;
381
  connection1Parameters["NameOfInterface"] = { "itkImageFixedInterface" };
382
  blueprint->AddConnection("FixedImageSource", "RegistrationMethod", connection1Parameters);
383
384

  ParameterMapType connection2Parameters;
385
  connection2Parameters["NameOfInterface"] = { "itkImageMovingInterface" };
386
  blueprint->AddConnection("MovingImageSource", "RegistrationMethod", connection2Parameters);
387
388

  ParameterMapType connection3Parameters;
389
  connection3Parameters["NameOfInterface"] = { "itkImageInterface" };
390
  blueprint->AddConnection("RegistrationMethod", "ResultImageSink", connection3Parameters);
391
392
393

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

396
397
  ParameterMapType connection5Parameters;
  connection5Parameters["NameOfInterface"] = { "itkMetricv4Interface" };
398
  blueprint->AddConnection("Metric", "RegistrationMethod", connection5Parameters);
399

400
401
402
  // Instantiate SuperElastix
  SuperElastixFilterType::Pointer superElastixFilter;
  EXPECT_NO_THROW(superElastixFilter = SuperElastixFilterType::New());
403

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

  // 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());

436
}
437
} // namespace selx
438