ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkSimpleMultiResolutionImageRegistrationUI.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkSimpleMultiResolutionImageRegistrationUI_h
00019 #define __itkSimpleMultiResolutionImageRegistrationUI_h
00020 
00021 #include "itkMultiResolutionImageRegistrationMethod.h"
00022 #include "itkCommand.h"
00023 #include "itkArray.h"
00024 #include "itkGradientDescentOptimizer.h"
00025 
00026 // The following classes are examples of simple user interface
00027 // that controls a MultiResolutionImageRegistrationMethod process
00028 
00029 template <typename TRegistrator>
00030 class SimpleMultiResolutionImageRegistrationUI
00031 {
00032 public:
00033   SimpleMultiResolutionImageRegistrationUI( TRegistrator * ptr )
00034     {
00035 
00036     if ( !ptr ) return;
00037     m_Registrator = ptr;
00038     typename itk::SimpleMemberCommand<SimpleMultiResolutionImageRegistrationUI>::Pointer
00039       iterationCommand =
00040     itk::SimpleMemberCommand<SimpleMultiResolutionImageRegistrationUI>::New();
00041 
00042     iterationCommand->SetCallbackFunction( this,
00043       &SimpleMultiResolutionImageRegistrationUI::StartNewLevel );
00044 
00045     m_Tag = m_Registrator->AddObserver( itk::IterationEvent(), iterationCommand );
00046 
00047     }
00048 
00049   virtual ~SimpleMultiResolutionImageRegistrationUI()
00050     {
00051     if( m_Registrator ) { m_Registrator->RemoveObserver( m_Tag ); }
00052     }
00053 
00054    virtual void StartNewLevel()
00055     {
00056     std::cout << "--- Starting level " << m_Registrator->GetCurrentLevel()
00057               << std::endl;
00058     }
00059 
00060 protected:
00061   typename TRegistrator::Pointer  m_Registrator;
00062   unsigned long                   m_Tag;
00063 
00064 };
00065 
00066 
00067 // This UI supports registration methods with gradient descent
00068 // type optimizers.
00069 // This UI allows the number of iterations and learning rate
00070 // to be changes at each resolution level.
00071 template <typename TRegistration>
00072 class SimpleMultiResolutionImageRegistrationUI2 :
00073   public SimpleMultiResolutionImageRegistrationUI<TRegistration>
00074 {
00075 public:
00076 
00077   typedef SimpleMultiResolutionImageRegistrationUI<TRegistration>
00078     Superclass;
00079 
00080   SimpleMultiResolutionImageRegistrationUI2( TRegistration * ptr ) :
00081     Superclass(ptr) {};
00082   virtual ~SimpleMultiResolutionImageRegistrationUI2(){}
00083 
00084   void SetNumberOfIterations( itk::Array<unsigned int> & iter )
00085     {
00086     m_NumberOfIterations = iter;
00087     }
00088 
00089   void SetLearningRates( itk::Array<double> & rates )
00090     {
00091     m_LearningRates = rates;
00092     }
00093 
00094   virtual void StartNewLevel()
00095     {
00096 
00097     // call the superclass's implementation
00098     this->Superclass::StartNewLevel();
00099 
00100     if ( !this->m_Registrator ) return;
00101 
00102     // Try to cast the optimizer to a gradient descent type,
00103     // return if casting didn't work.
00104     itk::GradientDescentOptimizer::Pointer optimizer;
00105     optimizer = dynamic_cast< itk::GradientDescentOptimizer * >(
00106       this->m_Registrator->GetOptimizer() );
00107     if ( !optimizer ) return;
00108 
00109     unsigned int level = this->m_Registrator->GetCurrentLevel();
00110     if ( m_NumberOfIterations.Size() >= level + 1 )
00111       {
00112       optimizer->SetNumberOfIterations( m_NumberOfIterations[level] );
00113       }
00114 
00115     if ( m_LearningRates.Size() >= level + 1 )
00116       {
00117       optimizer->SetLearningRate( m_LearningRates[level] );
00118       }
00119 
00120     std::cout << " No. Iterations: "
00121               << optimizer->GetNumberOfIterations()
00122               << " Learning rate: "
00123               << optimizer->GetLearningRate()
00124               << std::endl;
00125     }
00126 
00127 private:
00128    itk::Array<unsigned int> m_NumberOfIterations;
00129    itk::Array<double>       m_LearningRates;
00130 
00131 };
00132 
00133 
00134 #endif
00135