File: itkImageToSpatialObjectRegistrationTest.cxx

package info (click to toggle)
insighttoolkit5 5.4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704,592 kB
  • sloc: cpp: 784,579; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,934; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 460; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (388 lines) | stat: -rw-r--r-- 11,938 bytes parent folder | download | duplicates (2)
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
382
383
384
385
386
387
388
/*=========================================================================
 *
 *  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 "itkEllipseSpatialObject.h"
#include "itkLineSpatialObject.h"
#include "itkGroupSpatialObject.h"
#include "itkSpatialObjectToImageFilter.h"
#include "itkImageToSpatialObjectRegistrationMethod.h"
#include "itkOnePlusOneEvolutionaryOptimizer.h"
#include "itkEuler2DTransform.h"
#include "itkDiscreteGaussianImageFilter.h"
#include "itkNormalVariateGenerator.h"
#include "itkTestingMacros.h"

namespace itk
{

/** \class Iteration callback */
template <typename TOptimizer>
class IterationCallback : public Command
{
public:
  using Self = IterationCallback;
  using Superclass = itk::Command;
  using Pointer = itk::SmartPointer<Self>;
  using ConstPointer = itk::SmartPointer<const Self>;

  itkOverrideGetNameOfClassMacro(IterationCallback);
  itkNewMacro(Self);

  /** Type defining the optimizer */
  using OptimizerType = TOptimizer;


  /** Set Optimizer */
  void
  SetOptimizer(OptimizerType * optimizer)
  {
    m_Optimizer = optimizer;
    m_Optimizer->AddObserver(itk::IterationEvent(), this);
  }


  /** Execute method will print data at each iteration */
  void
  Execute(itk::Object * caller, const itk::EventObject & event) override
  {
    Execute((const itk::Object *)caller, event);
  }

  void
  Execute(const itk::Object *, const itk::EventObject & event) override
  {
    if (typeid(event) == typeid(itk::StartEvent))
    {
      std::cout << std::endl << "Position              Value";
      std::cout << std::endl << std::endl;
    }
    else if (typeid(event) == typeid(itk::IterationEvent))
    {
      std::cout << '#' << m_Optimizer->GetCurrentIteration()
                << " Current parameters = " << m_Optimizer->GetCurrentPosition() << std::endl;
    }
    else if (typeid(event) == typeid(itk::EndEvent))
    {
      std::cout << std::endl << std::endl;
      std::cout << "After " << m_Optimizer->GetCurrentIteration();
      std::cout << "  iterations " << std::endl;
      std::cout << "Solution is    = " << m_Optimizer->GetCurrentPosition();
      std::cout << std::endl;
    }
  }

protected:
  IterationCallback() = default;
  WeakPointer<OptimizerType> m_Optimizer;
};

/** \class Cost Function */
template <typename TFixedImage, typename TMovingSpatialObject>
class SimpleImageToSpatialObjectMetric : public ImageToSpatialObjectMetric<TFixedImage, TMovingSpatialObject>
{
public:
  /** Standard class type aliases. */
  using Self = SimpleImageToSpatialObjectMetric;
  using Superclass = ImageToSpatialObjectMetric<TFixedImage, TMovingSpatialObject>;
  using Pointer = SmartPointer<Self>;
  using ConstPointer = SmartPointer<const Self>;

  using PointType = Point<double, 2>;
  using PointListType = std::list<PointType>;
  using MovingSpatialObjectType = TMovingSpatialObject;
  using typename Superclass::ParametersType;
  using typename Superclass::DerivativeType;
  using typename Superclass::MeasureType;

  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  /** \see LightObject::GetNameOfClass() */
  itkOverrideGetNameOfClassMacro(SimpleImageToSpatialObjectMetric);

  enum
  {
    SpaceDimension = 3
  };

  /** Connect the MovingSpatialObject */
  void
  SetMovingSpatialObject(const MovingSpatialObjectType * object) override
  {
    if (!this->m_FixedImage)
    {
      std::cout << "Please set the image before the moving spatial object" << std::endl;
      return;
    }
    this->m_MovingSpatialObject = object;
    m_PointList.clear();
    using myIteratorType = itk::ImageRegionConstIteratorWithIndex<TFixedImage>;

    myIteratorType it(this->m_FixedImage, this->m_FixedImage->GetLargestPossibleRegion());

    itk::Point<double, 2> point;

    while (!it.IsAtEnd())
    {
      for (unsigned int i = 0; i < Self::ObjectDimension; ++i)
      {
        point[i] = it.GetIndex()[i];
      }

      if (this->m_MovingSpatialObject->IsInsideInWorldSpace(point, 99999))
      {
        m_PointList.push_back(point);
      }
      ++it;
    }

    std::cout << "Number of points in the metric = " << static_cast<unsigned long>(m_PointList.size()) << std::endl;
  }


  /** Get the Derivatives of the Match Measure */
  void
  GetDerivative(const ParametersType &, DerivativeType &) const override
  {
    return;
  }

  /** Get the Value for SingleValue Optimizers */
  MeasureType
  GetValue(const ParametersType & parameters) const override
  {
    double value;
    this->m_Transform->SetParameters(parameters);

    auto it = m_PointList.begin();

    Index<2> index;
    value = 0;
    while (it != m_PointList.end())
    {
      PointType transformedPoint = this->m_Transform->TransformPoint(*it);
      index = this->m_FixedImage->TransformPhysicalPointToIndex(transformedPoint);
      if (index[0] > 0L && index[1] > 0L &&
          index[0] < static_cast<long>(this->m_FixedImage->GetLargestPossibleRegion().GetSize()[0]) &&
          index[1] < static_cast<long>(this->m_FixedImage->GetLargestPossibleRegion().GetSize()[1]))
      {
        value += this->m_FixedImage->GetPixel(index);
      }
      ++it;
    }
    return value;
  }

  /** Get Value and Derivatives for MultipleValuedOptimizers */
  void
  GetValueAndDerivative(const ParametersType & parameters,
                        MeasureType &          Value,
                        DerivativeType &       Derivative) const override
  {
    Value = this->GetValue(parameters);
    this->GetDerivative(parameters, Derivative);
  }

private:
  PointListType m_PointList;
};

} // end namespace itk


int
itkImageToSpatialObjectRegistrationTest(int, char *[])
{
  using GroupType = itk::GroupSpatialObject<2>;
  using EllipseType = itk::EllipseSpatialObject<2>;

  // Create a group with 3 ellipses linked by lines.
  auto ellipse1 = EllipseType::New();
  auto ellipse2 = EllipseType::New();
  auto ellipse3 = EllipseType::New();

  // Set the radius
  ellipse1->SetRadiusInObjectSpace(10);
  ellipse2->SetRadiusInObjectSpace(10);
  ellipse3->SetRadiusInObjectSpace(10);

  // Place each ellipse at the right position to form a triangle
  EllipseType::PointType point;
  point[0] = 100;
  point[1] = 40;
  ellipse1->SetCenterInObjectSpace(point);
  ellipse1->Update();

  point[0] = 40;
  point[1] = 150;
  ellipse2->SetCenterInObjectSpace(point);
  ellipse2->Update();

  EllipseType::TransformType::OffsetType offset;
  offset[0] = 150;
  offset[1] = 150;
  // Moving the object using the ObjectToParentTransform should
  //   be equivalent to setting its CenterInObjectSpace
  ellipse3->GetModifiableObjectToParentTransform()->SetOffset(offset);
  ellipse3->Update();

  auto group = GroupType::New();
  group->AddChild(ellipse1);
  group->AddChild(ellipse2);
  group->AddChild(ellipse3);
  group->Update();

  using ImageType = itk::Image<double, 2>;

  using SpatialObjectToImageFilterType = itk::SpatialObjectToImageFilter<GroupType, ImageType>;
  auto imageFilter = SpatialObjectToImageFilterType::New();
  imageFilter->SetInput(group);
  ImageType::SizeType size;
  size[0] = 200;
  size[1] = 200;
  imageFilter->SetSize(size);
  imageFilter->Update();

  ImageType::Pointer image = imageFilter->GetOutput();

  // blurr the image to have a global maximum
  using GaussianFilterType = itk::DiscreteGaussianImageFilter<ImageType, ImageType>;
  auto gaussianFilter = GaussianFilterType::New();

  gaussianFilter->SetInput(image);
  constexpr double variance = 20;
  gaussianFilter->SetVariance(variance);
  gaussianFilter->Update();
  image = gaussianFilter->GetOutput();

  using RegistrationType = itk::ImageToSpatialObjectRegistrationMethod<ImageType, GroupType>;
  auto registration = RegistrationType::New();

  ITK_EXERCISE_BASIC_OBJECT_METHODS(registration, ImageToSpatialObjectRegistrationMethod, ProcessObject);

  using MetricType = itk::SimpleImageToSpatialObjectMetric<ImageType, GroupType>;
  auto metric = MetricType::New();

  std::cout << "metric = " << metric << std::endl;

  using InterpolatorType = itk::LinearInterpolateImageFunction<ImageType, double>;
  auto interpolator = InterpolatorType::New();

  using OptimizerType = itk::OnePlusOneEvolutionaryOptimizer;
  auto optimizer = OptimizerType::New();

  using TransformType = itk::Euler2DTransform<>;
  auto transform = TransformType::New();

  metric->SetTransform(transform);
  std::cout << "Number of Parameters  : " << metric->GetNumberOfParameters() << std::endl;
  ITK_TEST_EXPECT_EQUAL(metric->GetNumberOfParameters(), 3);

  // Test exception
  ITK_TRY_EXPECT_EXCEPTION(registration->Update());

  registration->SetFixedImage(image);
  ITK_TEST_SET_GET_VALUE(image, registration->GetFixedImage());

  // Test exception
  ITK_TRY_EXPECT_EXCEPTION(registration->Update());

  registration->SetMovingSpatialObject(group);
  ITK_TEST_SET_GET_VALUE(group, registration->GetMovingSpatialObject());

  // Test exception
  ITK_TRY_EXPECT_EXCEPTION(registration->Update());

  registration->SetMetric(metric);
  ITK_TEST_SET_GET_VALUE(metric, registration->GetMetric());

  // Test exception
  ITK_TRY_EXPECT_EXCEPTION(registration->Update());

  // Setup the optimizer
  TransformType::ParametersType m_ParametersScale;
  m_ParametersScale.set_size(3);

  m_ParametersScale[0] = 100; // angle scale

  for (unsigned int i = 1; i < 3; ++i)
  {
    m_ParametersScale[i] = 1; // offset scale
  }

  optimizer->SetScales(m_ParametersScale);

  TransformType::ParametersType initialParameters;
  initialParameters.set_size(3);

  initialParameters[0] = 0.2; // angle
  initialParameters[1] = 7;   // offset
  initialParameters[2] = 6;   // offset

  std::cout << "Initial Parameters  : " << initialParameters << std::endl;

  registration->SetInitialTransformParameters(initialParameters);
  ITK_TEST_SET_GET_VALUE(initialParameters, registration->GetInitialTransformParameters());

  optimizer->MaximizeOn();

  itk::Statistics::NormalVariateGenerator::Pointer generator = itk::Statistics::NormalVariateGenerator::New();
  generator->Initialize(12345);

  optimizer->SetNormalVariateGenerator(generator);
  optimizer->Initialize(1.02, 1.1);
  optimizer->SetEpsilon(0.01);
  optimizer->SetMaximumIteration(500);

  using IterationCallbackType = itk::IterationCallback<OptimizerType>;
  auto callback = IterationCallbackType::New();
  callback->SetOptimizer(optimizer);

  registration->SetOptimizer(optimizer);
  ITK_TEST_SET_GET_VALUE(optimizer, registration->GetOptimizer());

  // Test exception
  ITK_TRY_EXPECT_EXCEPTION(registration->Update());

  registration->SetTransform(transform);
  ITK_TEST_SET_GET_VALUE(transform, registration->GetTransform());

  // Test exception
  ITK_TRY_EXPECT_EXCEPTION(registration->Update());

  registration->SetInterpolator(interpolator);
  ITK_TEST_SET_GET_VALUE(interpolator, registration->GetInterpolator());

  registration->Update();

  RegistrationType::ParametersType finalParameters = registration->GetLastTransformParameters();

  std::cout << "Final Solution is : " << finalParameters << std::endl;

  for (unsigned int i = 0; i < 3; ++i)
  {
    if (finalParameters[i] > 1) // if we are not within 1 pixel the registration fails
    {
      std::cout << "Test failed!" << std::endl;
      return EXIT_FAILURE;
    }
  }

  std::cout << "Test finished." << std::endl;
  return EXIT_SUCCESS;
}