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
29template <typename TRegistrator>
31{
32public:
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
64protected:
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.
74template <typename TRegistration>
76 : public SimpleMultiResolutionImageRegistrationUI<TRegistration>
77{
78public:
81
83 : Superclass(ptr)
84 {}
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
135private:
136 itk::Array<unsigned int> m_NumberOfIterations{};
137 itk::Array<double> m_LearningRates{};
138};
139
140
141#endif
~SimpleMultiResolutionImageRegistrationUI2() override=default
Implement a gradient descent optimizer.
Templated n-dimensional image class.
Definition: itkImage.h:89
Base class for multi-resolution image registration methods.
static Pointer New()
SmartPointer< Self > Pointer