Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkRegistrationInterfaceCommand.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 namespace mitk
18 {
19  template <class TRegistration, class TPixel>
20  class RegistrationInterfaceCommand : public itk::Command
21  {
22  public:
24  typedef itk::Command Superclass;
26  itkFactorylessNewMacro(Self) itkCloneMacro(Self) protected : RegistrationInterfaceCommand() { m_UseMask = false; }
27  public:
28  // typedef TRegistration RegistrationType;
29 
30  typedef itk::MultiResolutionImageRegistrationMethod<itk::Image<float, 3>, itk::Image<float, 3>> RegistrationType;
31 
32  typedef RegistrationType *RegistrationPointer;
33  typedef itk::SingleValuedNonLinearOptimizer OptimizerType;
34  typedef OptimizerType *OptimizerPointer;
35  typedef itk::ImageMaskSpatialObject<3> MaskType;
36 
38  bool m_UseMask;
39  std::vector<std::string> m_Presets;
41 
42  void Execute(itk::Object *object, const itk::EventObject &event) override
43  {
44  if (!(itk::IterationEvent().CheckEvent(&event)))
45  {
46  return;
47  }
48 
49  RegistrationPointer registration = dynamic_cast<RegistrationPointer>(object);
50 
51  /*OptimizerPointer optimizer = dynamic_cast< OptimizerPointer >(
52  registration->GetOptimizer() );*/
53 
54  std::cout << "-------------------------------------" << std::endl;
55  std::cout << "MultiResolution Level : " << registration->GetCurrentLevel() << std::endl << std::endl;
56 
57  if (registration->GetCurrentLevel() == 0)
58  {
59  // Nothing needs to be changed in the first step
60  }
61  else
62  {
63  // Load presets and make a new optimizer if that succeeds
65  if (preset->LoadPreset())
66  {
68  itk::Array<double> optimizerValues = preset->getOptimizerValues(m_Presets[registration->GetCurrentLevel()]);
69 
70  optimizerParameters->SetOptimizer(optimizerValues[0]);
71  optimizerParameters->SetMaximize(
72  optimizerValues[1]); // should be when used with maximize mutual information for example
73 
75  {
76  optimizerParameters->SetLearningRateGradientDescent(optimizerValues[2]);
77  optimizerParameters->SetNumberOfIterationsGradientDescent(optimizerValues[3]);
78  }
79 
81  {
82  cout << "use regularstepgradientdescent" << endl;
83  optimizerParameters->SetGradientMagnitudeToleranceRegularStepGradientDescent(optimizerValues[2]);
84  optimizerParameters->SetMinimumStepLengthRegularStepGradientDescent(optimizerValues[3]);
85  optimizerParameters->SetMaximumStepLengthRegularStepGradientDescent(optimizerValues[4]);
86  optimizerParameters->SetRelaxationFactorRegularStepGradientDescent(optimizerValues[5]);
87  optimizerParameters->SetNumberOfIterationsRegularStepGradientDescent(optimizerValues[6]);
88  }
89 
90  // Typedef for the OptimizerFactory and initialisation of the optimizer using m_OptimizerParameters
92  optFac->SetOptimizerParameters(optimizerParameters);
93  optFac->SetNumberOfTransformParameters(registration->GetTransform()->GetNumberOfParameters());
94  typename OptimizerType::Pointer optimizer = optFac->GetOptimizer();
95 
96  if (observer.IsNotNull())
97  {
98  optimizer->AddObserver(itk::AnyEvent(), observer);
99  }
100 
101  itk::Array<double> transformValues = preset->getTransformValues(m_Presets[registration->GetCurrentLevel()]);
102 
103  itk::Array<double> scales;
104  if (transformValues[0] == mitk::TransformParameters::AFFINETRANSFORM)
105  scales.SetSize(12);
107  transformParameters->SetTransform(transformValues[0]);
108 
109  for (unsigned int i = 0; i < scales.size(); i++)
110  {
111  scales[i] = transformValues[i + 2];
112  std::cout << "scale " << i << ": " << scales[i] << std::endl;
113  }
114 
115  transformParameters->SetScales(scales);
116  // transformParameters->SetTransformInitializerOn(false);
117 
118  // Use Scales
119  if (transformValues[1] == 1)
120  {
121  transformParameters->SetUseOptimizerScales(true);
122  }
123 
124  if (transformParameters->GetUseOptimizerScales())
125  {
126  itk::Array<double> optimizerScales = transformParameters->GetScales();
127  typename OptimizerType::ScalesType scales(registration->GetTransform()->GetNumberOfParameters());
128  for (unsigned int i = 0; i < scales.Size(); i++)
129  {
130  scales[i] = optimizerScales[i];
131  }
132  optimizer->SetScales(scales);
133  }
134 
135  registration->SetOptimizer(optimizer);
136  }
137  delete preset;
138  }
139 
140  registration->Print(std::cout, 0);
141  std::cout << std::endl;
142  std::cout << "METRIC" << std::endl;
143  registration->GetMetric()->Print(std::cout, 0);
144  std::cout << std::endl;
145  std::cout << "OPTIMIZER" << std::endl;
146  registration->GetOptimizer()->Print(std::cout, 0);
147  std::cout << std::endl;
148  std::cout << "TRANSFORM" << std::endl;
149  registration->GetTransform()->Print(std::cout, 0);
150  }
151 
152  void Execute(const itk::Object *, const itk::EventObject &) override { return; }
153  };
154 }
static Pointer New()
mitk::RigidRegistrationObserver::Pointer observer
Class to load and save parameter presets for rigid registration.
itk::SmartPointer< Self > Pointer
bool LoadPreset()
Tries to find mitkRigidRegistrationPresets.xml in /mitk/Config and loads all presets stored in this f...
DataCollection - Class to facilitate loading/accessing structured data.
void Execute(itk::Object *object, const itk::EventObject &event) override
static Pointer New()
static Pointer New()
itk::SingleValuedNonLinearOptimizer OptimizerType
itk::Array< double > getTransformValues(std::string name)
Returns an array including all all transform values belonging to preset name.
itk::MultiResolutionImageRegistrationMethod< itk::Image< float, 3 >, itk::Image< float, 3 > > RegistrationType
itk::Array< double > getOptimizerValues(std::string name)
Returns an array including all all optimizer values belonging to preset name.
void Execute(const itk::Object *, const itk::EventObject &) override