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;
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
63protected:
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.
73template <typename TRegistration>
75 : public SimpleMultiResolutionImageRegistrationUI<TRegistration>
76{
77public:
80
82 : Superclass(ptr)
83 {}
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 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
134private:
135 itk::Array<unsigned int> m_NumberOfIterations{};
136 itk::Array<double> m_LearningRates{};
137};
138
139
140#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