ITK  4.2.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  {
35 
36  if ( !ptr ) return;
37  m_Registrator = ptr;
39  iterationCommand =
41 
42  iterationCommand->SetCallbackFunction( this,
44 
45  m_Tag = m_Registrator->AddObserver( itk::IterationEvent(), iterationCommand );
46 
47  }
48 
50  {
51  if( m_Registrator ) { m_Registrator->RemoveObserver( m_Tag ); }
52  }
53 
54  virtual void StartNewLevel()
55  {
56  std::cout << "--- Starting level " << m_Registrator->GetCurrentLevel()
57  << std::endl;
58  }
59 
60 protected:
61  typename TRegistrator::Pointer m_Registrator;
62  unsigned long m_Tag;
63 
64 };
65 
66 
67 // This UI supports registration methods with gradient descent
68 // type optimizers.
69 // This UI allows the number of iterations and learning rate
70 // to be changes at each resolution level.
71 template <typename TRegistration>
73  public SimpleMultiResolutionImageRegistrationUI<TRegistration>
74 {
75 public:
76 
79 
81  Superclass(ptr) {};
83 
85  {
86  m_NumberOfIterations = iter;
87  }
88 
90  {
91  m_LearningRates = rates;
92  }
93 
94  virtual void StartNewLevel()
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  optimizer = dynamic_cast< itk::GradientDescentOptimizer * >(
106  this->m_Registrator->GetOptimizer() );
107  if ( !optimizer ) return;
108 
109  unsigned int level = this->m_Registrator->GetCurrentLevel();
110  if ( m_NumberOfIterations.Size() >= level + 1 )
111  {
112  optimizer->SetNumberOfIterations( m_NumberOfIterations[level] );
113  }
114 
115  if ( m_LearningRates.Size() >= level + 1 )
116  {
117  optimizer->SetLearningRate( m_LearningRates[level] );
118  }
119 
120  std::cout << " No. Iterations: "
121  << optimizer->GetNumberOfIterations()
122  << " Learning rate: "
123  << optimizer->GetLearningRate()
124  << std::endl;
125  }
126 
127 private:
130 
131 };
132 
133 
134 #endif
135