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