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: 2009-08-20 09:08:41 $
00007   Version:   $Revision: 1.31 $
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 protected:
00339   ImageToImageMetric();
00340   virtual ~ImageToImageMetric();
00341 
00342   void PrintSelf(std::ostream& os, Indent indent) const;
00343 
00347 
00348   class FixedImageSamplePoint
00349     {
00350     public:
00351     FixedImageSamplePoint()
00352       { 
00353       point.Fill(0.0);
00354       value = 0;
00355       valueIndex = 0;
00356       }
00357     ~FixedImageSamplePoint() {};
00358 
00359     public:
00360       FixedImagePointType           point;
00361       double                        value;
00362       unsigned int                  valueIndex;
00363     };
00365   
00366   bool                      m_UseFixedImageIndexes;
00367   FixedImageIndexContainer  m_FixedImageIndexes;
00368 
00369   bool                      m_UseFixedImageSamplesIntensityThreshold;
00370   FixedImagePixelType       m_FixedImageSamplesIntensityThreshold;
00371 
00373   typedef std::vector<FixedImageSamplePoint> FixedImageSampleContainer;
00374 
00376   virtual void SampleFixedImageDomain( FixedImageSampleContainer & samples) const;
00377 
00378   virtual void SampleFixedImageIndexes( FixedImageSampleContainer & 
00379                                            samples) const;
00380 
00382   virtual void SampleFullFixedImageDomain( FixedImageSampleContainer & 
00383                                            samples) const;
00384 
00386   FixedImageSampleContainer   m_FixedImageSamples;
00387 
00388   unsigned long               m_NumberOfParameters;
00389   mutable ParametersType      m_Parameters;
00390 
00391   unsigned long               m_NumberOfFixedImageSamples;
00392   //m_NumberOfPixelsCounted must be mutable because the const
00393   //thread consolidation functions merge each threads valus
00394   //onto this accumulator variable.
00395   mutable unsigned long       m_NumberOfPixelsCounted;
00396 
00397   FixedImageConstPointer      m_FixedImage;
00398   MovingImageConstPointer     m_MovingImage;
00399 
00401   TransformPointer            m_Transform;
00402 
00405   TransformPointer          * m_ThreaderTransform; 
00406 
00407   InterpolatorPointer         m_Interpolator;
00408 
00409   bool                        m_ComputeGradient;
00410   GradientImagePointer        m_GradientImage;
00411 
00412   FixedImageMaskConstPointer  m_FixedImageMask;
00413   MovingImageMaskConstPointer m_MovingImageMask;
00414 
00415   unsigned int                m_NumberOfThreads;
00416 
00417   bool                        m_UseAllPixels;
00418   bool                        m_UseSequentialSampling;
00419 
00420   bool                        m_ReseedIterator;
00421 
00422   int                         m_RandomSeed;
00423 
00431   bool                        m_TransformIsBSpline;
00432 
00435   unsigned long               m_NumBSplineWeights;
00436 
00437   itkStaticConstMacro(DeformationSplineOrder, unsigned int, 3 );
00438 
00439   typedef BSplineDeformableTransform< CoordinateRepresentationType,
00440                       ::itk::GetImageDimension<FixedImageType>::ImageDimension,
00441                                       itkGetStaticConstMacro(DeformationSplineOrder) >             BSplineTransformType;
00442 
00443   typedef typename BSplineTransformType::WeightsType       BSplineTransformWeightsType;
00444   typedef typename BSplineTransformWeightsType::ValueType  WeightsValueType;
00445   typedef          Array2D<WeightsValueType>               BSplineTransformWeightsArrayType;
00446 
00447   typedef typename BSplineTransformType::ParameterIndexArrayType 
00448                                                               BSplineTransformIndexArrayType;
00449   typedef typename BSplineTransformIndexArrayType::ValueType  IndexValueType;
00450   typedef          Array2D<IndexValueType>                    BSplineTransformIndicesArrayType;
00451 
00452   typedef          std::vector<MovingImagePointType>          MovingImagePointArrayType;
00453   typedef          std::vector<bool>                          BooleanArrayType;
00454   typedef          FixedArray< unsigned long, 
00455                                ::itk::GetImageDimension<FixedImageType>
00456                                 ::ImageDimension >            BSplineParametersOffsetType;
00462   typedef BSplineInterpolateImageFunction<MovingImageType,
00463                                              CoordinateRepresentationType> 
00464                                                        BSplineInterpolatorType;
00465 
00467   typedef CentralDifferenceImageFunction<MovingImageType,
00468                                          CoordinateRepresentationType> 
00469                                                        DerivativeFunctionType;
00470   typedef          CovariantVector< double,
00471                            itkGetStaticConstMacro(MovingImageDimension) >
00472                                                           ImageDerivativesType;
00473 
00474 
00475   typename BSplineTransformType::Pointer   m_BSplineTransform;
00476 
00477   BSplineTransformWeightsArrayType         m_BSplineTransformWeightsArray;
00478   BSplineTransformIndicesArrayType         m_BSplineTransformIndicesArray;
00479   MovingImagePointArrayType                m_BSplinePreTransformPointsArray;
00480   BooleanArrayType                         m_WithinBSplineSupportRegionArray;
00481   
00482   BSplineParametersOffsetType              m_BSplineParametersOffset;
00483 
00484   // Variables needed for optionally caching values when using a BSpline transform.
00485   bool                                    m_UseCachingOfBSplineWeights;
00486   mutable BSplineTransformWeightsType     m_BSplineTransformWeights;
00487   mutable BSplineTransformIndexArrayType  m_BSplineTransformIndices;
00488 
00489   mutable BSplineTransformWeightsType     * m_ThreaderBSplineTransformWeights;
00490   mutable BSplineTransformIndexArrayType  * m_ThreaderBSplineTransformIndices;
00491 
00492   virtual void PreComputeTransformValues( void );
00493 
00496   virtual void TransformPoint( unsigned int sampleNumber,
00497                                MovingImagePointType& mappedPoint,
00498                                bool& sampleWithinSupportRegion,
00499                                double& movingImageValue,
00500                                unsigned int threadID ) const;
00501 
00502   virtual void TransformPointWithDerivatives( unsigned int sampleNumber,
00503                                MovingImagePointType& mappedPoint,
00504                                bool& sampleWithinSupportRegion,
00505                                double& movingImageValue,
00506                                ImageDerivativesType & gradient,
00507                                unsigned int threadID ) const;
00508 
00510   bool                                       m_InterpolatorIsBSpline;
00511 
00513   typename BSplineInterpolatorType::Pointer  m_BSplineInterpolator;
00514 
00516   typename DerivativeFunctionType::Pointer   m_DerivativeCalculator;
00517 
00519   virtual void ComputeImageDerivatives( const MovingImagePointType & mappedPoint,
00520                                         ImageDerivativesType & gradient,
00521                                         unsigned int threadID ) const;
00522 
00523 
00528   typedef MultiThreader               MultiThreaderType;
00529 
00530   struct MultiThreaderParameterType
00531     {
00532     ImageToImageMetric                   * metric;
00533     };
00534 
00535   MultiThreaderType::Pointer               m_Threader;
00536   MultiThreaderParameterType               m_ThreaderParameter;
00537   mutable unsigned int                   * m_ThreaderNumberOfMovingImageSamples;
00538   bool                                     m_WithinThreadPreProcess;
00539   bool                                     m_WithinThreadPostProcess;
00540 
00541   void                           GetValueMultiThreadedPreProcessInitiate( void ) const;
00542   void                           GetValueMultiThreadedInitiate( void ) const;
00543   void                           GetValueMultiThreadedPostProcessInitiate( void ) const;
00544   static ITK_THREAD_RETURN_TYPE  GetValueMultiThreadedPreProcess( void * arg );
00545   static ITK_THREAD_RETURN_TYPE  GetValueMultiThreaded( void * arg );
00546   static ITK_THREAD_RETURN_TYPE  GetValueMultiThreadedPostProcess( void * arg );
00547 
00548   virtual inline void       GetValueThread( unsigned int threadID ) const;
00549   virtual inline void       GetValueThreadPreProcess( 
00550                                  unsigned int itkNotUsed(threadID),
00551                                  bool itkNotUsed(withinSampleThread) ) const
00552     { };
00553   virtual inline bool       GetValueThreadProcessSample( 
00554                                  unsigned int itkNotUsed(threadID),
00555                                  unsigned long itkNotUsed(fixedImageSample),
00556                                  const MovingImagePointType & itkNotUsed(mappedPoint),
00557                                  double itkNotUsed(movingImageValue)) const
00558     { return false; };
00559   virtual inline void       GetValueThreadPostProcess( 
00560                                  unsigned int itkNotUsed(threadID),
00561                                  bool itkNotUsed(withinSampleThread) ) const
00562     { };
00563       
00564   void                          GetValueAndDerivativeMultiThreadedPreProcessInitiate( void ) const;
00565   void                          GetValueAndDerivativeMultiThreadedInitiate( void ) const;
00566   void                          GetValueAndDerivativeMultiThreadedPostProcessInitiate( void ) const;
00567   static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPreProcess( void * arg );
00568   static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreaded(void * arg);
00569   static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPostProcess(void * arg);
00570 
00571   virtual inline void  GetValueAndDerivativeThread(unsigned int threadID) const;
00572   virtual inline void  GetValueAndDerivativeThreadPreProcess(
00573                                 unsigned int itkNotUsed(threadID),
00574                                 bool itkNotUsed(withinSampleThread)) const 
00575     { };
00576   virtual inline bool  GetValueAndDerivativeThreadProcessSample( 
00577                                 unsigned int itkNotUsed(threadID),
00578                                 unsigned long itkNotUsed(fixedImageSample),
00579                                 const MovingImagePointType & itkNotUsed(mappedPoint),
00580                                 double itkNotUsed(movingImageValue),
00581                                 const ImageDerivativesType & itkNotUsed(movingImageGradientValue) ) const
00582     { return false; }
00583   virtual inline void  GetValueAndDerivativeThreadPostProcess( 
00584                                 unsigned int itkNotUsed(threadID),
00585                                 bool itkNotUsed(withinSampleThread) ) const 
00586     { };
00587 
00591   void SynchronizeTransforms() const;
00592 
00593 private:
00594   ImageToImageMetric(const Self&); //purposely not implemented
00595   void operator=(const Self&); //purposely not implemented
00596   
00597   FixedImageRegionType        m_FixedImageRegion;  
00598 
00599 };
00600 
00601 } // end namespace itk
00602 
00603 #ifndef ITK_MANUAL_INSTANTIATION
00604 #include "itkOptImageToImageMetric.txx"
00605 #endif
00606 
00607 #endif
00608 

Generated at Tue Sep 15 04:14:17 2009 for ITK by doxygen 1.5.8 written by Dimitri van Heesch, © 1997-2000