ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkSimpleMultiResolutionImageRegistrationUI.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
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 ) return;
38  m_Registrator = ptr;
40  iterationCommand =
42 
43  iterationCommand->SetCallbackFunction( this,
45 
46  m_Tag = m_Registrator->AddObserver( itk::IterationEvent(), iterationCommand );
47 
48  }
49 
51  {
52  if( m_Registrator ) { m_Registrator->RemoveObserver( m_Tag ); }
53  }
54 
55  virtual void StartNewLevel()
56  {
57  std::cout << "--- Starting level " << m_Registrator->GetCurrentLevel()
58  << std::endl;
59  }
60 
61 protected:
62  typename TRegistrator::Pointer m_Registrator;
63  unsigned long m_Tag;
64 
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  public SimpleMultiResolutionImageRegistrationUI<TRegistration>
75 {
76 public:
77 
79 
81  Superclass(ptr) {};
82  ~SimpleMultiResolutionImageRegistrationUI2() override = default;
83 
85  {
86  m_NumberOfIterations = iter;
87  }
88 
90  {
91  m_LearningRates = rates;
92  }
93 
94  void StartNewLevel() override
95  {
96 
97  // call the superclass's implementation
99 
100  if ( !this->m_Registrator ) return;
101 
102  // Try to cast the optimizer to a gradient descent type,
103  // return if casting didn't work.
105  this->m_Registrator->GetModifiableOptimizer() );
106  if ( !optimizer ) return;
107 
108  unsigned int level = this->m_Registrator->GetCurrentLevel();
109  if ( m_NumberOfIterations.Size() >= level + 1 )
110  {
111  optimizer->SetNumberOfIterations( m_NumberOfIterations[level] );
112  }
113 
114  if ( m_LearningRates.Size() >= level + 1 )
115  {
116  optimizer->SetLearningRate( m_LearningRates[level] );
117  }
118 
119  std::cout << " No. Iterations: "
120  << optimizer->GetNumberOfIterations()
121  << " Learning rate: "
122  << optimizer->GetLearningRate()
123  << std::endl;
124  }
125 
126 private:
129 
130 };
131 
132 
133 #endif
Implement a gradient descent optimizer.
~SimpleMultiResolutionImageRegistrationUI2() override=default
static Pointer New()
SizeValueType Size() const
Definition: itkArray.h:120
Templated n-dimensional image class.
Definition: itkImage.h:75