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

itkMRIBiasFieldCorrectionFilter.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkMRIBiasFieldCorrectionFilter.h,v $
00005   Language:  C++
00006   Date:      $Date: 2006/03/19 04:36:54 $
00007   Version:   $Revision: 1.36 $
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 __itkMRIBiasFieldCorrectionFilter_h
00018 #define __itkMRIBiasFieldCorrectionFilter_h
00019 
00020 #include <time.h>
00021 
00022 #include "itkImageToImageFilter.h"
00023 #include "itkImage.h"
00024 #include "itkArray2D.h"
00025 #include "itkMRASlabIdentifier.h"
00026 #include "itkCompositeValleyFunction.h"
00027 #include "itkMultivariateLegendrePolynomial.h"
00028 #include "Statistics/itkNormalVariateGenerator.h"
00029 #include "itkOnePlusOneEvolutionaryOptimizer.h"
00030 #include "itkArray.h"
00031 #include "itkImageRegionConstIterator.h"
00032 #include "itkImageRegionIterator.h"
00033 
00034 namespace itk
00035 {
00047 template<class TImage, class TImageMask, class TBiasField>
00048 class MRIBiasEnergyFunction : public SingleValuedCostFunction
00049 {
00050 public:
00052   typedef MRIBiasEnergyFunction        Self;
00053   typedef SingleValuedCostFunction     Superclass;
00054   typedef SmartPointer<Self>           Pointer;
00055   typedef SmartPointer<const Self>     ConstPointer;
00056 
00058   itkTypeMacro( SingleValuedCostFunction, CostFunction );
00059 
00061   itkNewMacro(Self);
00062 
00064   typedef TImage ImageType ;
00065   typedef TImageMask MaskType ;
00066   typedef typename ImageType::Pointer ImagePointer ;
00067   typedef typename MaskType::Pointer MaskPointer ;
00068   typedef typename ImageType::PixelType ImageElementType ;
00069   typedef typename MaskType::PixelType MaskElementType ;
00070   typedef typename ImageType::IndexType ImageIndexType ;
00071   typedef typename ImageType::RegionType ImageRegionType ;
00072 
00074   typedef TBiasField                        BiasFieldType;
00075 
00078   typedef typename Superclass::ParametersType    ParametersType ;
00079 
00081   typedef Superclass::DerivativeType    DerivativeType;
00082 
00084   typedef Superclass::MeasureType       MeasureType;
00085 
00086   itkStaticConstMacro(SpaceDimension, unsigned int, 3);
00087 
00089   typedef CompositeValleyFunction InternalEnergyFunction ;
00090 
00092   typedef unsigned int SamplingFactorType[SpaceDimension];
00093 
00095   itkSetObjectMacro( Image, ImageType );
00096 
00098   itkSetObjectMacro( Mask, MaskType );
00099 
00101   itkSetMacro( Region, ImageRegionType );
00102 
00104   void SetBiasField(BiasFieldType* bias)
00105   { m_BiasField = bias ; }
00106 
00109   void SetSamplingFactors(SamplingFactorType factor)
00110     { 
00111     for (unsigned int i = 0; i < SpaceDimension; i++)
00112       {
00113       m_SamplingFactor[i] = factor[i]; 
00114       }
00115     }
00117 
00120   double GetEnergy0(double diff) 
00121     {
00122     return (*m_InternalEnergyFunction)(diff); 
00123     } 
00124 
00127   MeasureType GetValue(const ParametersType & parameters ) const ;
00128 
00131   void GetDerivative( const ParametersType & itkNotUsed(parameters),
00132                       DerivativeType & itkNotUsed(derivative) ) const 
00133     {  }
00134 
00139   void InitializeDistributions( Array<double> classMeans, 
00140                                 Array<double> classSigmas );
00141 
00142   unsigned int GetNumberOfParameters(void) const;
00143 
00144 private:
00145   
00147   BiasFieldType        * m_BiasField ;
00148 
00150   ImagePointer           m_Image ;
00151 
00153   MaskPointer            m_Mask ;
00154 
00156   ImageRegionType        m_Region ;
00157 
00159   InternalEnergyFunction* m_InternalEnergyFunction ;
00160 
00162   SamplingFactorType m_SamplingFactor;
00163 
00164 protected:
00166   MRIBiasEnergyFunction();
00167 
00169   virtual ~MRIBiasEnergyFunction();
00170 
00171 
00172 private:
00173   
00174   MRIBiasEnergyFunction(const Self&); //purposely not implemented
00175   void operator=(const Self&); //purposely not implemented
00176   
00177 } ; // end of class
00178 
00179 
00180 
00230 template <class TInputImage, class TOutputImage, class TMaskImage>
00231 class ITK_EXPORT MRIBiasFieldCorrectionFilter :
00232     public ImageToImageFilter< TInputImage, TOutputImage > 
00233 {
00234 public:
00236   typedef MRIBiasFieldCorrectionFilter Self;
00237   typedef ImageToImageFilter< TInputImage, TOutputImage > Superclass;
00238   typedef SmartPointer<Self> Pointer;
00239   typedef SmartPointer<const Self>  ConstPointer;
00240 
00242   itkNewMacro(Self);
00243 
00245   itkTypeMacro(MRIBiasFieldCorrectionFilter, ImageToImageFilter);
00246 
00248   itkStaticConstMacro(ImageDimension, unsigned int,
00249                       TOutputImage::ImageDimension);
00250 
00252   typedef TOutputImage OutputImageType ;
00253   typedef TInputImage InputImageType ;
00254   typedef typename TOutputImage::Pointer OutputImagePointer ;
00255   typedef typename TOutputImage::IndexType OutputImageIndexType ;
00256   typedef typename TOutputImage::PixelType OutputImagePixelType ;
00257   typedef typename TOutputImage::SizeType OutputImageSizeType;
00258   typedef typename TOutputImage::RegionType OutputImageRegionType;
00259   typedef typename TInputImage::Pointer InputImagePointer ;
00260   typedef typename TInputImage::IndexType InputImageIndexType;
00261   typedef typename TInputImage::PixelType InputImagePixelType;
00262   typedef typename TInputImage::SizeType InputImageSizeType;
00263   typedef typename TInputImage::RegionType InputImageRegionType;
00264 
00266   typedef TMaskImage ImageMaskType ;
00267   typedef typename ImageMaskType::Pointer ImageMaskPointer ;
00268   typedef typename ImageMaskType::RegionType ImageMaskRegionType ;
00269 
00271   typedef Image< float, itkGetStaticConstMacro(ImageDimension) >
00272                                                            InternalImageType ;
00273   typedef typename InternalImageType::PixelType       InternalImagePixelType ;
00274   typedef typename InternalImageType::Pointer           InternalImagePointer ;
00275   typedef typename InternalImageType::RegionType     InternalImageRegionType ;
00277 
00279   typedef MRASlabIdentifier<InputImageType>            MRASlabIdentifierType ;
00280   typedef typename MRASlabIdentifierType::SlabRegionVectorType 
00281                                                         SlabRegionVectorType ;
00282   typedef typename SlabRegionVectorType::iterator SlabRegionVectorIteratorType ;
00283 
00285   typedef MultivariateLegendrePolynomial                  BiasFieldType;
00286 
00288   typedef MRIBiasEnergyFunction<InternalImageType,
00289                                 ImageMaskType,
00290                                 BiasFieldType>            EnergyFunctionType;
00291   typedef typename EnergyFunctionType::Pointer            EnergyFunctionPointer;
00292 
00294   typedef Statistics::NormalVariateGenerator NormalVariateGeneratorType ;
00295 
00297   typedef OnePlusOneEvolutionaryOptimizer OptimizerType ;
00298 
00300   typedef Array2D<unsigned int>  ScheduleType;
00301 
00305   void SetInputMask(ImageMaskType* inputMask);
00306   itkGetObjectMacro( InputMask, ImageMaskType );
00308 
00311   void SetOutputMask(ImageMaskType* outputMask) ;
00312 
00314   itkGetObjectMacro( OutputMask, ImageMaskType );
00315 
00319   void IsBiasFieldMultiplicative(bool flag) 
00320     { m_BiasMultiplicative = flag ; }
00321 
00323   bool IsBiasFieldMultiplicative() 
00324     { return m_BiasMultiplicative ; }
00325 
00329   itkSetMacro( UsingInterSliceIntensityCorrection, bool );
00330   itkGetConstReferenceMacro( UsingInterSliceIntensityCorrection, bool );
00332 
00338   itkSetMacro( UsingSlabIdentification, bool );
00339   itkGetConstReferenceMacro( UsingSlabIdentification, bool );
00341 
00342   itkSetMacro( SlabBackgroundMinimumThreshold, InputImagePixelType );
00343   itkGetConstReferenceMacro( SlabBackgroundMinimumThreshold,
00344                              InputImagePixelType );
00345 
00346   itkSetMacro( SlabNumberOfSamples, unsigned int );
00347   itkGetConstReferenceMacro( SlabNumberOfSamples, unsigned int );
00348 
00349   itkSetMacro( SlabTolerance, double );
00350   itkGetConstReferenceMacro( SlabTolerance, double );
00351 
00357   itkSetMacro( UsingBiasFieldCorrection, bool );
00358   itkGetConstReferenceMacro( UsingBiasFieldCorrection, bool );
00360 
00363   itkSetMacro( GeneratingOutput, bool );
00364   itkGetConstReferenceMacro( GeneratingOutput, bool );
00366 
00369   itkSetMacro( SlicingDirection , int );
00370 
00372   itkSetMacro( BiasFieldDegree, int );
00373   itkGetMacro( BiasFieldDegree, int );
00375 
00378   void SetInitialBiasFieldCoefficients(const 
00379                                        BiasFieldType::CoefficientArrayType
00380                                           &coefficients)
00381     { this->Modified() ; m_BiasFieldCoefficients = coefficients ; }
00382 
00386   BiasFieldType::CoefficientArrayType GetEstimatedBiasFieldCoefficients()
00387     { return m_EstimatedBiasFieldCoefficients ; } 
00388 
00392   void SetTissueClassStatistics(const Array<double> & means, 
00393                                 const Array<double> & sigmas) 
00394     throw (ExceptionObject) ;
00395 
00397   itkSetMacro( VolumeCorrectionMaximumIteration, int );
00398   itkGetMacro( VolumeCorrectionMaximumIteration, int );
00399   itkSetMacro( InterSliceCorrectionMaximumIteration, int );
00400   itkGetMacro( InterSliceCorrectionMaximumIteration, int );
00402 
00404   void SetOptimizerInitialRadius(double initRadius) 
00405     { m_OptimizerInitialRadius = initRadius ; }
00406   double GetOptimizerInitialRadius()
00407   { return m_OptimizerInitialRadius ; }
00409 
00411   itkSetMacro( OptimizerGrowthFactor, double );
00412   itkGetMacro( OptimizerGrowthFactor, double );
00414 
00417   itkSetMacro( OptimizerShrinkFactor, double );
00418   itkGetMacro( OptimizerShrinkFactor, double );
00419 
00420 
00427   void SetNumberOfLevels(unsigned int num);
00428 
00430   itkGetMacro(NumberOfLevels, unsigned int);
00431 
00438   void SetSchedule( const ScheduleType& schedule );
00439 
00441   itkGetConstReferenceMacro(Schedule, ScheduleType);
00442 
00447   void SetStartingShrinkFactors( unsigned int factor );
00448   void SetStartingShrinkFactors( unsigned int* factors );
00450 
00452   const unsigned int * GetStartingShrinkFactors() const;
00453 
00457   static bool IsScheduleDownwardDivisible( const ScheduleType& schedule );
00458 
00459   
00466   void Initialize() throw (ExceptionObject) ;
00467 
00470   BiasFieldType EstimateBiasField(InputImageRegionType region,
00471                                   unsigned int degree,
00472                                   int maximumIteration) ;
00473 
00477   void CorrectImage(BiasFieldType& bias, 
00478                     InputImageRegionType region) ;
00479 
00482   void CorrectInterSliceIntensityInhomogeneity(InputImageRegionType region) ;
00483 
00484 protected:
00485   MRIBiasFieldCorrectionFilter() ;
00486   virtual ~MRIBiasFieldCorrectionFilter() ;
00487   void PrintSelf(std::ostream& os, Indent indent) const;
00488 
00491   bool CheckMaskImage(ImageMaskType* mask) ;
00492 
00493 protected:
00497   void Log1PImage(InternalImageType* source, 
00498                   InternalImageType* target) ;
00499 
00502   void ExpImage(InternalImageType* source, 
00503                 InternalImageType* target) ;
00504 
00507   template<class TSource, class TTarget>
00508   void CopyAndConvertImage(const TSource * source,
00509                            TTarget * target,
00510                            typename TTarget::RegionType requestedRegion)
00511   {
00512     typedef ImageRegionConstIterator<TSource> SourceIterator ;
00513     typedef ImageRegionIterator<TTarget> TargetIterator ;
00514     typedef typename TTarget::PixelType  TargetPixelType ;
00515 
00516     SourceIterator s_iter(source, requestedRegion) ;
00517     TargetIterator t_iter(target, requestedRegion) ;
00518     
00519     s_iter.GoToBegin() ;
00520     t_iter.GoToBegin() ;
00521     while (!s_iter.IsAtEnd())
00522       {
00523       t_iter.Set(static_cast<TargetPixelType>( s_iter.Get() ) ) ;
00524       ++s_iter ;
00525       ++t_iter ;
00526       }
00527   }
00528   
00533   void GetBiasFieldSize(InputImageRegionType region,
00534                         BiasFieldType::DomainSizeType& domainSize) ;
00535 
00539   void AdjustSlabRegions(SlabRegionVectorType& slabs, 
00540                          OutputImageRegionType requestedRegion) ;
00541 
00542   void GenerateData() ;
00543 
00544 private:
00545   MRIBiasFieldCorrectionFilter(const Self&); //purposely not implemented
00546   void operator=(const Self&); //purposely not implemented
00547   
00549   EnergyFunctionPointer  m_EnergyFunction ;
00550 
00552   NormalVariateGeneratorType::Pointer m_NormalVariateGenerator ;
00553 
00555   ImageMaskPointer m_InputMask ;
00556 
00558   ImageMaskPointer m_OutputMask ;
00559 
00561   InternalImagePointer m_InternalInput ;
00562 
00564   SlabRegionVectorType m_Slabs ;
00565 
00567   int m_SlicingDirection ;
00568 
00570   bool m_BiasMultiplicative ;
00571 
00573   bool m_UsingInterSliceIntensityCorrection ;
00574   bool m_UsingSlabIdentification ;
00575   bool m_UsingBiasFieldCorrection ;
00576   bool m_GeneratingOutput ;
00577 
00578   unsigned int m_SlabNumberOfSamples ;
00579   InputImagePixelType m_SlabBackgroundMinimumThreshold ;
00580   double m_SlabTolerance ;
00582   int m_BiasFieldDegree ;
00583 
00585   unsigned int    m_NumberOfLevels;
00586 
00588   ScheduleType    m_Schedule;
00589 
00592   BiasFieldType::CoefficientArrayType m_BiasFieldCoefficients ;
00593 
00596   BiasFieldType::CoefficientArrayType m_EstimatedBiasFieldCoefficients ;
00597 
00599   int m_VolumeCorrectionMaximumIteration ;
00600 
00602   int m_InterSliceCorrectionMaximumIteration ;
00603 
00605   double m_OptimizerInitialRadius ;
00606 
00608   double m_OptimizerGrowthFactor ;
00609 
00611   double m_OptimizerShrinkFactor ;
00612 
00614   Array<double> m_TissueClassMeans ;
00615 
00617   Array<double> m_TissueClassSigmas ;
00618 };
00619 
00620   
00621 // ==================================
00622 
00623 
00624 } // end namespace itk
00625 
00626 #ifndef ITK_MANUAL_INSTANTIATION
00627 #include "itkMRIBiasFieldCorrectionFilter.txx"
00628 #endif
00629 
00630 #endif
00631 

Generated at Tue Jul 29 21:20:22 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000