Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkPyramidRegistrationMethodHelper.h
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #ifndef MITKPYRAMIDREGISTRATIONMETHODHELPER_H
18 #define MITKPYRAMIDREGISTRATIONMETHODHELPER_H
19 
21 
22 #include <itkCommand.h>
23 
24 #include <itkRegularStepGradientDescentOptimizer.h>
25 #include <itkMattesMutualInformationImageToImageMetricv4.h>
26 #include <itkCorrelationImageToImageMetricv4.h>
27 #include <itkGradientDescentLineSearchOptimizerv4.h>
28 
29 #include <itkImageRegistrationMethodv4.h>
30 
31 #include <itkAffineTransform.h>
32 #include <itkEuler3DTransform.h>
33 
34 #include <itkMattesMutualInformationImageToImageMetric.h>
35 #include <itkNormalizedCorrelationImageToImageMetric.h>
36 
37 #include <itkMultiResolutionImageRegistrationMethod.h>
38 #include <itkImageMomentsCalculator.h>
39 
40 #include "mitkImageAccessByItk.h"
41 
47 #define AccessTwoImagesFixedDimensionTypeSubsetByItk(mitkImage1, mitkImage2, itkImageTypeFunction, dimension) \
48 { \
49  const mitk::PixelType& pixelType1 = mitkImage1->GetPixelType(); \
50  const mitk::PixelType& pixelType2 = mitkImage2->GetPixelType(); \
51  const mitk::Image* constImage1 = mitkImage1; \
52  const mitk::Image* constImage2 = mitkImage2; \
53  mitk::Image* nonConstImage1 = const_cast<mitk::Image*>(constImage1); \
54  mitk::Image* nonConstImage2 = const_cast<mitk::Image*>(constImage2); \
55  nonConstImage1->Update(); \
56  nonConstImage2->Update(); \
57  _checkSpecificDimension(mitkImage1, (dimension)); \
58  _checkSpecificDimension(mitkImage2, (dimension)); \
59  _accessTwoImagesByItkForEach(itkImageTypeFunction, ((short, dimension))((unsigned short, dimension))((float, dimension))((double, dimension)), ((short, dimension))((unsigned short, dimension))((float, dimension))((double, dimension)) ) \
60  { \
61  std::string msg("Pixel type "); \
62  msg.append(pixelType1.GetComponentTypeAsString() ); \
63  msg.append(" or pixel type "); \
64  msg.append(pixelType2.GetComponentTypeAsString() ); \
65  msg.append(" is not in " MITK_PP_STRINGIZE(MITK_ACCESSBYITK_TYPES_DIMN_SEQ(dimension))); \
66  throw mitk::AccessByItkException(msg); \
67  } \
68 }
69 
70 
75 template <typename RegistrationType >
76 class PyramidOptControlCommand : public itk::Command
77 {
78 public:
79 
80  typedef itk::RegularStepGradientDescentOptimizer OptimizerType;
81 
83  itkFactorylessNewMacro(Self)
84  itkCloneMacro(Self)
85 
86  void Execute(itk::Object *caller, const itk::EventObject & /*event*/) override
87  {
88  RegistrationType* registration = dynamic_cast< RegistrationType* >( caller );
89 
90  if( registration == NULL)
91  return;
92 
93  MITK_DEBUG << "\t - Pyramid level " << registration->GetCurrentLevel();
94  if( registration->GetCurrentLevel() == 0 )
95  { MITK_WARN("OptCommand") << "Cast to registration failed";
96  return;
97  }
98 
99  OptimizerType* optimizer = dynamic_cast< OptimizerType* >(registration->GetOptimizer());
100 
101  if( optimizer == NULL)
102  { MITK_WARN("OptCommand") << "Cast to optimizer failed";
103  return;
104  }
105 
106  MITK_DEBUG /*<< optimizer->GetStopConditionDescription() << "\n"*/
107  << optimizer->GetValue() << " : " << optimizer->GetCurrentPosition();
108 
109  optimizer->SetMaximumStepLength( optimizer->GetMaximumStepLength() * 0.25f );
110  optimizer->SetMinimumStepLength( optimizer->GetMinimumStepLength() * 0.1f );
111  // optimizer->SetNumberOfIterations( optimizer->GetNumberOfIterations() * 1.5f );
112  }
113 
114  void Execute(const itk::Object * /*object*/, const itk::EventObject & /*event*/) override{}
115 };
116 
117 #include <itkGradientDescentLineSearchOptimizerv4.h>
118 
119 template <typename RegistrationType >
120 class PyramidOptControlCommandv4 : public itk::Command
121 {
122 public:
123 
124  typedef itk::GradientDescentLineSearchOptimizerv4 OptimizerType;
125 
127  itkFactorylessNewMacro(Self)
128  itkCloneMacro(Self)
129 
130  void Execute(itk::Object *caller, const itk::EventObject & /*event*/) override
131  {
132  RegistrationType* registration = dynamic_cast< RegistrationType* >( caller );
133 
134  if( registration == NULL)
135  return;
136 
137  MITK_DEBUG << "\t - Pyramid level " << registration->GetCurrentLevel();
138  if( registration->GetCurrentLevel() == 0 )
139  return;
140 
141  OptimizerType* optimizer = dynamic_cast< OptimizerType* >( registration->GetOptimizer() );
142 
143  if( optimizer == NULL)
144  { MITK_WARN("OptCommand4") << "Cast to optimizer failed";
145  return;
146  }
147 
148  optimizer->SetNumberOfIterations( optimizer->GetNumberOfIterations() * 2.5 );
149  optimizer->SetMaximumStepSizeInPhysicalUnits( optimizer->GetMaximumStepSizeInPhysicalUnits() * 0.4);
150 
151  MITK_INFO("Pyramid.Command.Iter") << optimizer->GetNumberOfIterations();
152  MITK_INFO("Pyramid.Command.MaxStep") << optimizer->GetMaximumStepSizeInPhysicalUnits();
153 
154  }
155 
156  void Execute(const itk::Object * /*object*/, const itk::EventObject & /*event*/) override{}
157 };
158 
159 
160 template <typename OptimizerType>
161 class OptimizerIterationCommand : public itk::Command
162 {
163 public:
165  itkFactorylessNewMacro(Self)
166  itkCloneMacro(Self)
167 
168  void Execute(itk::Object *caller, const itk::EventObject & /*event*/) override
169  {
170 
171  OptimizerType* optimizer = dynamic_cast< OptimizerType* >( caller );
172 
173  unsigned int currentIter = optimizer->GetCurrentIteration();
174  MITK_INFO << "[" << currentIter << "] : " << optimizer->GetValue() << " : " << optimizer->GetCurrentPosition();
175 
176  }
177 
178  void Execute(const itk::Object * /*object*/, const itk::EventObject & /*event*/) override
179  {
180 
181  }
182 };
183 
184 template <typename OptimizerType>
185 class OptimizerIterationCommandv4 : public itk::Command
186 {
187 public:
188  itkNewMacro( OptimizerIterationCommandv4 )
189 
190  void Execute(itk::Object *object, const itk::EventObject & event) override
191  {
192  OptimizerType* optimizer = dynamic_cast< OptimizerType* >( object );
193 
194  if( typeid( event ) != typeid( itk::IterationEvent ) )
195  { return; }
196 
197  unsigned int currentIter = optimizer->GetCurrentIteration();
198  MITK_INFO << "[" << currentIter << "] : " << optimizer->GetCurrentMetricValue() << " : "
199  << optimizer->GetMetric()->GetParameters() ;
200  //<< " : " << optimizer->GetScales();
201 
202  }
203 
204  void Execute(const itk::Object * /*object*/, const itk::EventObject & /*event*/) override
205  {
206 
207 
208  }
209 };
210 
211 
212 #endif // MITKPYRAMIDREGISTRATIONMETHODHELPER_H
void Execute(itk::Object *caller, const itk::EventObject &) override
mitkClassMacroItkParent(PyramidOptControlCommandv4< RegistrationType >, itk::Command) static Pointer New()
itk::GradientDescentLineSearchOptimizerv4 OptimizerType
#define MITK_INFO
Definition: mitkLogMacros.h:22
void Execute(const itk::Object *, const itk::EventObject &) override
void Execute(itk::Object *caller, const itk::EventObject &) override
#define MITK_DEBUG
Definition: mitkLogMacros.h:26
void Execute(const itk::Object *, const itk::EventObject &) override
The PyramidOptControlCommand class stears the step lenght of the gradient descent optimizer in multi-...
#define MITK_WARN
Definition: mitkLogMacros.h:23
mitkClassMacroItkParent(PyramidOptControlCommand< RegistrationType >, itk::Command) static Pointer New()
mitkClassMacroItkParent(OptimizerIterationCommand< OptimizerType >, itk::Command) static Pointer New()
void Execute(const itk::Object *, const itk::EventObject &) override
void Execute(itk::Object *caller, const itk::EventObject &) override
::map::core::RegistrationBase RegistrationType
itk::RegularStepGradientDescentOptimizer OptimizerType
void Execute(const itk::Object *, const itk::EventObject &) override
void Execute(itk::Object *object, const itk::EventObject &event) override