ITK  6.0.0
Insight Toolkit
itkSimpleMultiResolutionImageRegistrationUI.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * https://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkSimpleMultiResolutionImageRegistrationUI_h
19 #define itkSimpleMultiResolutionImageRegistrationUI_h
20 
22 #include "itkCommand.h"
23 #include "itkArray.h"
25 
26 // The following classes are examples of simple user interface
27 // that controls a MultiResolutionImageRegistrationMethod process
28 
29 template <typename TRegistrator>
31 {
32 public:
34  : m_Tag(0)
35  {
36 
37  if (!ptr)
38  {
39  return;
40  }
41  m_Registrator = ptr;
44 
45  iterationCommand->SetCallbackFunction(this, &SimpleMultiResolutionImageRegistrationUI::StartNewLevel);
46 
47  m_Tag = m_Registrator->AddObserver(itk::IterationEvent(), iterationCommand);
48  }
49 
51  {
52  if (m_Registrator)
53  {
54  m_Registrator->RemoveObserver(m_Tag);
55  }
56  }
57 
58  virtual void
60  {
61  std::cout << "--- Starting level " << m_Registrator->GetCurrentLevel() << std::endl;
62  }
63 
64 protected:
66  unsigned long m_Tag{};
67 };
68 
69 
70 // This UI supports registration methods with gradient descent
71 // type optimizers.
72 // This UI allows the number of iterations and learning rate
73 // to be changes at each resolution level.
74 template <typename TRegistration>
76  : public SimpleMultiResolutionImageRegistrationUI<TRegistration>
77 {
78 public:
81 
83  : Superclass(ptr)
84  {}
85  ~SimpleMultiResolutionImageRegistrationUI2() override = default;
86 
87  void
89  {
90  m_NumberOfIterations = iter;
91  }
92 
93  void
95  {
96  m_LearningRates = rates;
97  }
98 
99  void
100  StartNewLevel() override
101  {
102 
103  // call the superclass's implementation
104  this->Superclass::StartNewLevel();
105 
106  if (!this->m_Registrator)
107  {
108  return;
109  }
110 
111  // Try to cast the optimizer to a gradient descent type,
112  // return if casting didn't work.
114  dynamic_cast<itk::GradientDescentOptimizer *>(this->m_Registrator->GetModifiableOptimizer());
115  if (!optimizer)
116  {
117  return;
118  }
119 
120  unsigned int level = this->m_Registrator->GetCurrentLevel();
121  if (m_NumberOfIterations.Size() >= level + 1)
122  {
123  optimizer->SetNumberOfIterations(m_NumberOfIterations[level]);
124  }
125 
126  if (m_LearningRates.Size() >= level + 1)
127  {
128  optimizer->SetLearningRate(m_LearningRates[level]);
129  }
130 
131  std::cout << " No. Iterations: " << optimizer->GetNumberOfIterations()
132  << " Learning rate: " << optimizer->GetLearningRate() << std::endl;
133  }
134 
135 private:
136  itk::Array<unsigned int> m_NumberOfIterations{};
137  itk::Array<double> m_LearningRates{};
138 };
139 
140 
141 #endif
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itkMultiResolutionImageRegistrationMethod.h
SimpleMultiResolutionImageRegistrationUI::StartNewLevel
virtual void StartNewLevel()
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:59
itk::MultiResolutionImageRegistrationMethod
Base class for multi-resolution image registration methods.
Definition: itkMultiResolutionImageRegistrationMethod.h:72
SimpleMultiResolutionImageRegistrationUI2::SetNumberOfIterations
void SetNumberOfIterations(itk::Array< unsigned int > &iter)
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:88
SimpleMultiResolutionImageRegistrationUI2::SimpleMultiResolutionImageRegistrationUI2
SimpleMultiResolutionImageRegistrationUI2(TRegistration *ptr)
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:82
SimpleMultiResolutionImageRegistrationUI
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:30
itk::SmartPointer< Self >
SimpleMultiResolutionImageRegistrationUI2::SetLearningRates
void SetLearningRates(itk::Array< double > &rates)
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:94
itk::SimpleMemberCommand::New
static Pointer New()
SimpleMultiResolutionImageRegistrationUI::SimpleMultiResolutionImageRegistrationUI
SimpleMultiResolutionImageRegistrationUI(TRegistrator *ptr)
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:33
SimpleMultiResolutionImageRegistrationUI::m_Registrator
TRegistrator::Pointer m_Registrator
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:65
SimpleMultiResolutionImageRegistrationUI::m_Tag
unsigned long m_Tag
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:66
itkArray.h
SimpleMultiResolutionImageRegistrationUI::~SimpleMultiResolutionImageRegistrationUI
virtual ~SimpleMultiResolutionImageRegistrationUI()
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:50
SimpleMultiResolutionImageRegistrationUI2
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:75
SimpleMultiResolutionImageRegistrationUI2::StartNewLevel
void StartNewLevel() override
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:100
itk::Array< unsigned int >
itkGradientDescentOptimizer.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itkCommand.h