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