Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkOptImageToImageMetric.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkOptImageToImageMetric.h,v $
00005   Language:  C++
00006   Date:      $Date: 2010-03-05 16:15:37 $
00007   Version:   $Revision: 1.34 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 #ifndef __itkOptImageToImageMetric_h
00018 #define __itkOptImageToImageMetric_h
00019 
00020 #include "itkSingleValuedCostFunction.h"
00021 #include "itkImageBase.h"
00022 #include "itkTransform.h"
00023 #include "itkInterpolateImageFunction.h"
00024 #include "itkExceptionObject.h"
00025 #include "itkGradientRecursiveGaussianImageFilter.h"
00026 #include "itkSpatialObject.h"
00027 #include "itkBSplineDeformableTransform.h"
00028 #include "itkCentralDifferenceImageFunction.h"
00029 #include "itkCovariantVector.h"
00030 
00031 #include "itkMultiThreader.h"
00032 
00033 #include "itkOptBSplineInterpolateImageFunction.h"
00034 
00035 namespace itk
00036 {
00037   
00057 template <class TFixedImage,  class TMovingImage> 
00058 class ITK_EXPORT ImageToImageMetric 
00059 : public SingleValuedCostFunction 
00060 {
00061 public:
00063   typedef ImageToImageMetric           Self;
00064   typedef SingleValuedCostFunction     Superclass;
00065   typedef SmartPointer<Self>           Pointer;
00066   typedef SmartPointer<const Self>     ConstPointer;
00067 
00069   typedef typename Superclass::ParametersValueType CoordinateRepresentationType;
00070 
00072   itkTypeMacro(ImageToImageMetric, SingleValuedCostFunction);
00073 
00075   typedef TMovingImage                               MovingImageType;
00076   typedef typename TMovingImage::PixelType           MovingImagePixelType;
00077   typedef typename MovingImageType::ConstPointer     MovingImageConstPointer;
00078 
00080   typedef TFixedImage                                FixedImageType;
00081   typedef typename TFixedImage::PixelType            FixedImagePixelType;
00082   typedef typename FixedImageType::ConstPointer      FixedImageConstPointer;
00083   typedef typename FixedImageType::RegionType        FixedImageRegionType;
00084 
00086   itkStaticConstMacro(MovingImageDimension, 
00087                       unsigned int,
00088                       TMovingImage::ImageDimension);
00089   itkStaticConstMacro(FixedImageDimension, 
00090                       unsigned int,
00091                       TFixedImage::ImageDimension);
00093 
00095   typedef Transform<CoordinateRepresentationType, 
00096                     itkGetStaticConstMacro(MovingImageDimension),
00097                     itkGetStaticConstMacro(FixedImageDimension)> 
00098                                                      TransformType;
00099 
00100   typedef typename TransformType::Pointer            TransformPointer;
00101   typedef typename TransformType::InputPointType     InputPointType;
00102   typedef typename TransformType::OutputPointType    OutputPointType;
00103   typedef typename TransformType::ParametersType     TransformParametersType;
00104   typedef typename TransformType::JacobianType       TransformJacobianType;
00105 
00107   typedef typename FixedImageType::IndexType           FixedImageIndexType;
00108   typedef typename FixedImageIndexType::IndexValueType FixedImageIndexValueType;
00109   typedef typename MovingImageType::IndexType          MovingImageIndexType;
00110   typedef typename TransformType::InputPointType       FixedImagePointType;
00111   typedef typename TransformType::OutputPointType      MovingImagePointType;
00112 
00113   typedef std::vector<FixedImageIndexType>             FixedImageIndexContainer;
00114 
00116   typedef InterpolateImageFunction< MovingImageType, 
00117                                     CoordinateRepresentationType >
00118                                                      InterpolatorType;
00119 
00121   typedef typename NumericTraits<MovingImagePixelType>::RealType 
00122                                                      RealType;
00123   typedef CovariantVector<RealType,
00124                           itkGetStaticConstMacro(MovingImageDimension)>
00125                                                      GradientPixelType;
00126   typedef Image<GradientPixelType,
00127                 itkGetStaticConstMacro(MovingImageDimension)> 
00128                                                      GradientImageType;
00129   typedef SmartPointer<GradientImageType>            GradientImagePointer;
00130   typedef GradientRecursiveGaussianImageFilter< MovingImageType,
00131                                                 GradientImageType >
00132                                                      GradientImageFilterType;  
00133   typedef typename GradientImageFilterType::Pointer  GradientImageFilterPointer;
00135 
00136 
00137   typedef typename InterpolatorType::Pointer         InterpolatorPointer;
00138 
00139 
00142   typedef SpatialObject< itkGetStaticConstMacro(FixedImageDimension) >
00143                                                      FixedImageMaskType;
00144   typedef typename FixedImageMaskType::Pointer       FixedImageMaskPointer;
00145   typedef typename FixedImageMaskType::ConstPointer  FixedImageMaskConstPointer;
00146 
00149   typedef SpatialObject< itkGetStaticConstMacro(MovingImageDimension) >
00150                                                      MovingImageMaskType;
00151   typedef typename MovingImageMaskType::Pointer      MovingImageMaskPointer;
00152   typedef typename MovingImageMaskType::ConstPointer MovingImageMaskConstPointer;
00153 
00154 
00156   typedef typename Superclass::MeasureType                    MeasureType;
00157 
00159   typedef typename Superclass::DerivativeType                 DerivativeType;
00160 
00162   typedef typename Superclass::ParametersType                 ParametersType;
00163 
00165   itkSetConstObjectMacro( FixedImage, FixedImageType );
00166 
00168   itkGetConstObjectMacro( FixedImage, FixedImageType );
00169 
00171   itkSetConstObjectMacro( MovingImage, MovingImageType );
00172 
00174   itkGetConstObjectMacro( MovingImage, MovingImageType );
00175 
00177   itkSetObjectMacro( Transform, TransformType );
00178 
00180   itkGetConstObjectMacro( Transform, TransformType );
00181 
00183   itkSetObjectMacro( Interpolator, InterpolatorType );
00184 
00186   itkGetConstObjectMacro( Interpolator, InterpolatorType );
00187 
00189   unsigned long GetNumberOfMovingImageSamples( void )
00190     {
00191     return this->GetNumberOfPixelsCounted();
00192     }
00193   itkGetConstReferenceMacro( NumberOfPixelsCounted, unsigned long );
00195 
00197   void SetFixedImageRegion( const FixedImageRegionType reg );
00198 
00200   itkGetConstReferenceMacro( FixedImageRegion, FixedImageRegionType );
00201 
00203   itkSetObjectMacro( MovingImageMask, MovingImageMaskType );
00204   itkSetConstObjectMacro( MovingImageMask, MovingImageMaskType );
00205   itkGetConstObjectMacro( MovingImageMask, MovingImageMaskType );
00207 
00209   itkSetObjectMacro( FixedImageMask, FixedImageMaskType );
00210   itkSetConstObjectMacro( FixedImageMask, FixedImageMaskType );
00211   itkGetConstObjectMacro( FixedImageMask, FixedImageMaskType );
00213 
00216   void SetFixedImageIndexes( const FixedImageIndexContainer & indexes );
00217   void SetUseFixedImageIndexes( bool useIndex );
00218   itkGetConstReferenceMacro( UseFixedImageIndexes, bool );
00220 
00222   void SetNumberOfThreads( unsigned int numberOfThreads );
00223   itkGetConstReferenceMacro( NumberOfThreads, unsigned int );
00225 
00227   itkSetMacro( ComputeGradient, bool );
00228   itkGetConstReferenceMacro( ComputeGradient, bool );
00229   itkBooleanMacro(ComputeGradient );
00231 
00233   virtual void ComputeGradient( void );
00234 
00236   itkGetConstObjectMacro( GradientImage, GradientImageType );
00237 
00239   void SetTransformParameters( const ParametersType & parameters ) const;
00240 
00242   unsigned int GetNumberOfParameters( void ) const 
00243     {
00244     return m_Transform->GetNumberOfParameters(); 
00245     }
00246 
00249   virtual void Initialize( void ) throw ( ExceptionObject );
00250 
00252   virtual void MultiThreadingInitialize( void ) throw ( ExceptionObject );
00253 
00256   virtual void SetNumberOfFixedImageSamples( unsigned long numSamples );
00257   itkGetConstReferenceMacro( NumberOfFixedImageSamples, unsigned long ); 
00259 
00262   void SetNumberOfSpatialSamples( unsigned long num )
00263     {
00264     this->SetNumberOfFixedImageSamples( num ); 
00265     }
00266   unsigned long GetNumberOfSpatialSamples( void )
00267     {
00268     return this->GetNumberOfFixedImageSamples(); 
00269     }
00271 
00274   void SetFixedImageSamplesIntensityThreshold( const FixedImagePixelType & thresh );
00275   itkGetConstReferenceMacro( FixedImageSamplesIntensityThreshold, FixedImagePixelType );
00277 
00278   void SetUseFixedImageSamplesIntensityThreshold( bool useThresh );
00279   itkGetConstReferenceMacro( UseFixedImageSamplesIntensityThreshold, bool );
00280 
00284   void SetUseAllPixels( bool useAllPixels );
00285   void UseAllPixelsOn( void )
00286     {
00287     this->SetUseAllPixels( true );
00288     }
00289   void UseAllPixelsOff( void )
00290     {
00291     this->SetUseAllPixels( false );
00292     }
00293   itkGetConstReferenceMacro( UseAllPixels, bool );
00295 
00300   void SetUseSequentialSampling( bool sequentialSampling );
00301   itkGetConstReferenceMacro( UseSequentialSampling, bool );
00303 
00313   void ReinitializeSeed();
00314   void ReinitializeSeed( int seed );  
00316 
00333   itkSetMacro(UseCachingOfBSplineWeights,bool);
00334   itkGetConstReferenceMacro(UseCachingOfBSplineWeights,bool);
00335   itkBooleanMacro(UseCachingOfBSplineWeights);
00337 
00338   typedef MultiThreader               MultiThreaderType;
00340   itkGetConstObjectMacro( Threader, MultiThreaderType );
00341   const TransformPointer* GetThreaderTransform()
00342     {
00343     return m_ThreaderTransform;
00344     }
00346 
00347 protected:
00348   ImageToImageMetric();
00349   virtual ~ImageToImageMetric();
00350 
00351   void PrintSelf(std::ostream& os, Indent indent) const;
00352 
00356 
00357   class FixedImageSamplePoint
00358     {
00359     public:
00360     FixedImageSamplePoint()
00361       { 
00362       point.Fill(0.0);
00363       value = 0;
00364       valueIndex = 0;
00365       }
00366     ~FixedImageSamplePoint() {};
00367 
00368     public:
00369       FixedImagePointType           point;
00370       double                        value;
00371       unsigned int                  valueIndex;
00372     };
00374   
00375   bool                      m_UseFixedImageIndexes;
00376   FixedImageIndexContainer  m_FixedImageIndexes;
00377 
00378   bool                      m_UseFixedImageSamplesIntensityThreshold;
00379   FixedImagePixelType       m_FixedImageSamplesIntensityThreshold;
00380 
00382   typedef std::vector<FixedImageSamplePoint> FixedImageSampleContainer;
00383 
00385   virtual void SampleFixedImageRegion( FixedImageSampleContainer & samples) const;
00386 
00387   virtual void SampleFixedImageIndexes( FixedImageSampleContainer & 
00388                                            samples) const;
00389 
00391   virtual void SampleFullFixedImageRegion( FixedImageSampleContainer & 
00392                                            samples) const;
00393 
00395   FixedImageSampleContainer   m_FixedImageSamples;
00396 
00397   unsigned long               m_NumberOfParameters;
00398   mutable ParametersType      m_Parameters;
00399 
00400   unsigned long               m_NumberOfFixedImageSamples;
00401   //m_NumberOfPixelsCounted must be mutable because the const
00402   //thread consolidation functions merge each threads valus
00403   //onto this accumulator variable.
00404   mutable unsigned long       m_NumberOfPixelsCounted;
00405 
00406   FixedImageConstPointer      m_FixedImage;
00407   MovingImageConstPointer     m_MovingImage;
00408 
00410   TransformPointer            m_Transform;
00411 
00414   TransformPointer          * m_ThreaderTransform; 
00415 
00416   InterpolatorPointer         m_Interpolator;
00417 
00418   bool                        m_ComputeGradient;
00419   GradientImagePointer        m_GradientImage;
00420 
00421   FixedImageMaskConstPointer  m_FixedImageMask;
00422   MovingImageMaskConstPointer m_MovingImageMask;
00423 
00424   unsigned int                m_NumberOfThreads;
00425 
00426   bool                        m_UseAllPixels;
00427   bool                        m_UseSequentialSampling;
00428 
00429   bool                        m_ReseedIterator;
00430 
00431   int                         m_RandomSeed;
00432 
00440   bool                        m_TransformIsBSpline;
00441 
00444   unsigned long               m_NumBSplineWeights;
00445 
00446   itkStaticConstMacro(DeformationSplineOrder, unsigned int, 3 );
00447 
00448   typedef BSplineDeformableTransform< CoordinateRepresentationType,
00449                       ::itk::GetImageDimension<FixedImageType>::ImageDimension,
00450                                       itkGetStaticConstMacro(DeformationSplineOrder) >             BSplineTransformType;
00451 
00452   typedef typename BSplineTransformType::WeightsType       BSplineTransformWeightsType;
00453   typedef typename BSplineTransformWeightsType::ValueType  WeightsValueType;
00454   typedef          Array2D<WeightsValueType>               BSplineTransformWeightsArrayType;
00455 
00456   typedef typename BSplineTransformType::ParameterIndexArrayType 
00457                                                               BSplineTransformIndexArrayType;
00458   typedef typename BSplineTransformIndexArrayType::ValueType  IndexValueType;
00459   typedef          Array2D<IndexValueType>                    BSplineTransformIndicesArrayType;
00460 
00461   typedef          std::vector<MovingImagePointType>          MovingImagePointArrayType;
00462   typedef          std::vector<bool>                          BooleanArrayType;
00463   typedef          FixedArray< unsigned long, 
00464                                ::itk::GetImageDimension<FixedImageType>
00465                                 ::ImageDimension >            BSplineParametersOffsetType;
00471   typedef BSplineInterpolateImageFunction<MovingImageType,
00472                                              CoordinateRepresentationType> 
00473                                                        BSplineInterpolatorType;
00474 
00476   typedef CentralDifferenceImageFunction<MovingImageType,
00477                                          CoordinateRepresentationType> 
00478                                                        DerivativeFunctionType;
00479   typedef          CovariantVector< double,
00480                            itkGetStaticConstMacro(MovingImageDimension) >
00481                                                           ImageDerivativesType;
00482 
00483 
00484   typename BSplineTransformType::Pointer   m_BSplineTransform;
00485 
00486   BSplineTransformWeightsArrayType         m_BSplineTransformWeightsArray;
00487   BSplineTransformIndicesArrayType         m_BSplineTransformIndicesArray;
00488   MovingImagePointArrayType                m_BSplinePreTransformPointsArray;
00489   BooleanArrayType                         m_WithinBSplineSupportRegionArray;
00490   
00491   BSplineParametersOffsetType              m_BSplineParametersOffset;
00492 
00493   // Variables needed for optionally caching values when using a BSpline transform.
00494   bool                                    m_UseCachingOfBSplineWeights;
00495   mutable BSplineTransformWeightsType     m_BSplineTransformWeights;
00496   mutable BSplineTransformIndexArrayType  m_BSplineTransformIndices;
00497 
00498   mutable BSplineTransformWeightsType     * m_ThreaderBSplineTransformWeights;
00499   mutable BSplineTransformIndexArrayType  * m_ThreaderBSplineTransformIndices;
00500 
00501   virtual void PreComputeTransformValues( void );
00502 
00505   virtual void TransformPoint( unsigned int sampleNumber,
00506                                MovingImagePointType& mappedPoint,
00507                                bool& sampleWithinSupportRegion,
00508                                double& movingImageValue,
00509                                unsigned int threadID ) const;
00510 
00511   virtual void TransformPointWithDerivatives( unsigned int sampleNumber,
00512                                MovingImagePointType& mappedPoint,
00513                                bool& sampleWithinSupportRegion,
00514                                double& movingImageValue,
00515                                ImageDerivativesType & gradient,
00516                                unsigned int threadID ) const;
00517 
00519   bool                                       m_InterpolatorIsBSpline;
00520 
00522   typename BSplineInterpolatorType::Pointer  m_BSplineInterpolator;
00523 
00525   typename DerivativeFunctionType::Pointer   m_DerivativeCalculator;
00526 
00528   virtual void ComputeImageDerivatives( const MovingImagePointType & mappedPoint,
00529                                         ImageDerivativesType & gradient,
00530                                         unsigned int threadID ) const;
00531 
00532 
00537   struct MultiThreaderParameterType
00538     {
00539     ImageToImageMetric                   * metric;
00540     };
00541 
00542   MultiThreaderType::Pointer               m_Threader;
00543   MultiThreaderParameterType               m_ThreaderParameter;
00544   mutable unsigned int                   * m_ThreaderNumberOfMovingImageSamples;
00545   bool                                     m_WithinThreadPreProcess;
00546   bool                                     m_WithinThreadPostProcess;
00547 
00548   void                           GetValueMultiThreadedPreProcessInitiate( void ) const;
00549   void                           GetValueMultiThreadedInitiate( void ) const;
00550   void                           GetValueMultiThreadedPostProcessInitiate( void ) const;
00551   static ITK_THREAD_RETURN_TYPE  GetValueMultiThreadedPreProcess( void * arg );
00552   static ITK_THREAD_RETURN_TYPE  GetValueMultiThreaded( void * arg );
00553   static ITK_THREAD_RETURN_TYPE  GetValueMultiThreadedPostProcess( void * arg );
00554 
00555   virtual inline void       GetValueThread( unsigned int threadID ) const;
00556   virtual inline void       GetValueThreadPreProcess( 
00557                                  unsigned int itkNotUsed(threadID),
00558                                  bool itkNotUsed(withinSampleThread) ) const
00559     { };
00560   virtual inline bool       GetValueThreadProcessSample( 
00561                                  unsigned int itkNotUsed(threadID),
00562                                  unsigned long itkNotUsed(fixedImageSample),
00563                                  const MovingImagePointType & itkNotUsed(mappedPoint),
00564                                  double itkNotUsed(movingImageValue)) const
00565     { return false; };
00566   virtual inline void       GetValueThreadPostProcess( 
00567                                  unsigned int itkNotUsed(threadID),
00568                                  bool itkNotUsed(withinSampleThread) ) const
00569     { };
00570       
00571   void                          GetValueAndDerivativeMultiThreadedPreProcessInitiate( void ) const;
00572   void                          GetValueAndDerivativeMultiThreadedInitiate( void ) const;
00573   void                          GetValueAndDerivativeMultiThreadedPostProcessInitiate( void ) const;
00574   static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPreProcess( void * arg );
00575   static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreaded(void * arg);
00576   static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPostProcess(void * arg);
00577 
00578   virtual inline void  GetValueAndDerivativeThread(unsigned int threadID) const;
00579   virtual inline void  GetValueAndDerivativeThreadPreProcess(
00580                                 unsigned int itkNotUsed(threadID),
00581                                 bool itkNotUsed(withinSampleThread)) const 
00582     { };
00583   virtual inline bool  GetValueAndDerivativeThreadProcessSample( 
00584                                 unsigned int itkNotUsed(threadID),
00585                                 unsigned long itkNotUsed(fixedImageSample),
00586                                 const MovingImagePointType & itkNotUsed(mappedPoint),
00587                                 double itkNotUsed(movingImageValue),
00588                                 const ImageDerivativesType & itkNotUsed(movingImageGradientValue) ) const
00589     { return false; }
00590   virtual inline void  GetValueAndDerivativeThreadPostProcess( 
00591                                 unsigned int itkNotUsed(threadID),
00592                                 bool itkNotUsed(withinSampleThread) ) const 
00593     { };
00594 
00598   virtual void SynchronizeTransforms() const;
00599 
00600 private:
00601   ImageToImageMetric(const Self&); //purposely not implemented
00602   void operator=(const Self&); //purposely not implemented
00603   
00604   FixedImageRegionType        m_FixedImageRegion;  
00605 
00606 };
00607 
00608 } // end namespace itk
00609 
00610 #ifndef ITK_MANUAL_INSTANTIATION
00611 #include "itkOptImageToImageMetric.txx"
00612 #endif
00613 
00614 #endif
00615 

Generated at Mon Jul 12 2010 19:21:28 for ITK by doxygen 1.7.1 written by Dimitri van Heesch, © 1997-2000