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 
35  {
36 
37  if (!ptr)
38  {
39  return;
40  }
41  m_Registrator = ptr;
43 
44  iterationCommand->SetCallbackFunction(this, &SimpleMultiResolutionImageRegistrationUI::StartNewLevel);
45 
46  m_Tag = m_Registrator->AddObserver(itk::IterationEvent(), iterationCommand);
47  }
48 
50  {
51  if (m_Registrator)
52  {
53  m_Registrator->RemoveObserver(m_Tag);
54  }
55  }
56 
57  virtual void
59  {
60  std::cout << "--- Starting level " << m_Registrator->GetCurrentLevel() << std::endl;
61  }
62 
63 protected:
65  unsigned long m_Tag{};
66 };
67 
68 
69 // This UI supports registration methods with gradient descent
70 // type optimizers.
71 // This UI allows the number of iterations and learning rate
72 // to be changes at each resolution level.
73 template <typename TRegistration>
75  : public SimpleMultiResolutionImageRegistrationUI<TRegistration>
76 {
77 public:
80 
82  : Superclass(ptr)
83  {}
84  ~SimpleMultiResolutionImageRegistrationUI2() override = default;
85 
86  void
88  {
89  m_NumberOfIterations = iter;
90  }
91 
92  void
94  {
95  m_LearningRates = rates;
96  }
97 
98  void
99  StartNewLevel() override
100  {
101 
102  // call the superclass's implementation
103  this->Superclass::StartNewLevel();
104 
105  if (!this->m_Registrator)
106  {
107  return;
108  }
109 
110  // Try to cast the optimizer to a gradient descent type,
111  // return if casting didn't work.
113  dynamic_cast<itk::GradientDescentOptimizer *>(this->m_Registrator->GetModifiableOptimizer());
114  if (!optimizer)
115  {
116  return;
117  }
118 
119  const unsigned int level = this->m_Registrator->GetCurrentLevel();
120  if (m_NumberOfIterations.Size() >= level + 1)
121  {
122  optimizer->SetNumberOfIterations(m_NumberOfIterations[level]);
123  }
124 
125  if (m_LearningRates.Size() >= level + 1)
126  {
127  optimizer->SetLearningRate(m_LearningRates[level]);
128  }
129 
130  std::cout << " No. Iterations: " << optimizer->GetNumberOfIterations()
131  << " Learning rate: " << optimizer->GetLearningRate() << std::endl;
132  }
133 
134 private:
135  itk::Array<unsigned int> m_NumberOfIterations{};
136  itk::Array<double> m_LearningRates{};
137 };
138 
139 
140 #endif
Pointer
SmartPointer< Self > Pointer
Definition: itkAddImageFilter.h:93
itkMultiResolutionImageRegistrationMethod.h
SimpleMultiResolutionImageRegistrationUI::StartNewLevel
virtual void StartNewLevel()
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:58
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:87
SimpleMultiResolutionImageRegistrationUI2::SimpleMultiResolutionImageRegistrationUI2
SimpleMultiResolutionImageRegistrationUI2(TRegistration *ptr)
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:81
SimpleMultiResolutionImageRegistrationUI
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:30
itk::SmartPointer< Self >
SimpleMultiResolutionImageRegistrationUI2::SetLearningRates
void SetLearningRates(itk::Array< double > &rates)
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:93
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:64
SimpleMultiResolutionImageRegistrationUI::m_Tag
unsigned long m_Tag
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:65
itkArray.h
SimpleMultiResolutionImageRegistrationUI::~SimpleMultiResolutionImageRegistrationUI
virtual ~SimpleMultiResolutionImageRegistrationUI()
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:49
SimpleMultiResolutionImageRegistrationUI2
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:74
SimpleMultiResolutionImageRegistrationUI2::StartNewLevel
void StartNewLevel() override
Definition: itkSimpleMultiResolutionImageRegistrationUI.h:99
itk::Array< unsigned int >
itkGradientDescentOptimizer.h
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
itkCommand.h