00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00108 void SetSamplingFactors(SamplingFactorType factor)
00109 { for (unsigned int i = 0; i < SpaceDimension; i++) m_SamplingFactor[i] = factor[i]; }
00110
00113 double GetEnergy0(double diff)
00114 { return (*m_InternalEnergyFunction)(diff); }
00115
00118 MeasureType GetValue(const ParametersType & parameters ) const ;
00119
00122 void GetDerivative( const ParametersType & itkNotUsed(parameters),
00123 DerivativeType & itkNotUsed(derivative) ) const
00124 { }
00125
00130 void InitializeDistributions( Array<double> classMeans,
00131 Array<double> classSigmas );
00132
00133 unsigned int GetNumberOfParameters(void) const;
00134
00135 private:
00136
00138 BiasFieldType * m_BiasField ;
00139
00141 ImagePointer m_Image ;
00142
00144 MaskPointer m_Mask ;
00145
00147 ImageRegionType m_Region ;
00148
00150 InternalEnergyFunction* m_InternalEnergyFunction ;
00151
00153 SamplingFactorType m_SamplingFactor;
00154
00155 protected:
00157 MRIBiasEnergyFunction();
00158
00160 virtual ~MRIBiasEnergyFunction();
00161
00162
00163 private:
00164
00165 MRIBiasEnergyFunction(const Self&);
00166 void operator=(const Self&);
00167
00168 } ;
00169
00170
00171
00221 template <class TInputImage, class TOutputImage, class TMaskImage>
00222 class ITK_EXPORT MRIBiasFieldCorrectionFilter :
00223 public ImageToImageFilter< TInputImage, TOutputImage >
00224 {
00225 public:
00227 typedef MRIBiasFieldCorrectionFilter Self;
00228 typedef ImageToImageFilter< TInputImage, TOutputImage > Superclass;
00229 typedef SmartPointer<Self> Pointer;
00230 typedef SmartPointer<const Self> ConstPointer;
00231
00233 itkNewMacro(Self);
00234
00236 itkTypeMacro(MRIBiasFieldCorrectionFilter, ImageToImageFilter);
00237
00239 itkStaticConstMacro(ImageDimension, unsigned int,
00240 TOutputImage::ImageDimension);
00241
00243 typedef TOutputImage OutputImageType ;
00244 typedef TInputImage InputImageType ;
00245 typedef typename TOutputImage::Pointer OutputImagePointer ;
00246 typedef typename TOutputImage::IndexType OutputImageIndexType ;
00247 typedef typename TOutputImage::PixelType OutputImagePixelType ;
00248 typedef typename TOutputImage::SizeType OutputImageSizeType;
00249 typedef typename TOutputImage::RegionType OutputImageRegionType;
00250 typedef typename TInputImage::Pointer InputImagePointer ;
00251 typedef typename TInputImage::IndexType InputImageIndexType;
00252 typedef typename TInputImage::PixelType InputImagePixelType;
00253 typedef typename TInputImage::SizeType InputImageSizeType;
00254 typedef typename TInputImage::RegionType InputImageRegionType;
00255
00257 typedef TMaskImage ImageMaskType ;
00258 typedef typename ImageMaskType::Pointer ImageMaskPointer ;
00259 typedef typename ImageMaskType::RegionType ImageMaskRegionType ;
00260
00262 typedef Image< float, itkGetStaticConstMacro(ImageDimension) > InternalImageType ;
00263 typedef typename InternalImageType::PixelType InternalImagePixelType ;
00264 typedef typename InternalImageType::Pointer InternalImagePointer ;
00265 typedef typename InternalImageType::RegionType InternalImageRegionType ;
00266
00268 typedef MRASlabIdentifier<InputImageType> MRASlabIdentifierType;
00269 typedef typename MRASlabIdentifierType::SlabRegionVectorType
00270 SlabRegionVectorType;
00271 typedef typename SlabRegionVectorType::iterator SlabRegionVectorIteratorType;
00272
00274 typedef MultivariateLegendrePolynomial BiasFieldType;
00275
00277 typedef MRIBiasEnergyFunction<InternalImageType, ImageMaskType, BiasFieldType>
00278 EnergyFunctionType;
00279 typedef typename EnergyFunctionType::Pointer EnergyFunctionPointer;
00280
00282 typedef Statistics::NormalVariateGenerator NormalVariateGeneratorType ;
00283
00285 typedef OnePlusOneEvolutionaryOptimizer OptimizerType ;
00286
00288 typedef Array2D<unsigned int> ScheduleType;
00289
00293 void SetInputMask(ImageMaskType* inputMask);
00294 itkGetObjectMacro( InputMask, ImageMaskType );
00295
00298 void SetOutputMask(ImageMaskType* outputMask) ;
00299
00301 itkGetObjectMacro( OutputMask, ImageMaskType );
00302
00306 void IsBiasFieldMultiplicative(bool flag)
00307 { m_BiasMultiplicative = flag ; }
00308
00310 bool IsBiasFieldMultiplicative()
00311 { return m_BiasMultiplicative ; }
00312
00316 itkSetMacro( UsingInterSliceIntensityCorrection, bool );
00317 itkGetConstReferenceMacro( UsingInterSliceIntensityCorrection, bool );
00318
00324 itkSetMacro( UsingSlabIdentification, bool );
00325 itkGetConstReferenceMacro( UsingSlabIdentification, bool );
00326
00327 itkSetMacro( SlabBackgroundMinimumThreshold, InputImagePixelType );
00328 itkGetConstReferenceMacro( SlabBackgroundMinimumThreshold, InputImagePixelType );
00329
00330 itkSetMacro( SlabNumberOfSamples, unsigned int );
00331 itkGetConstReferenceMacro( SlabNumberOfSamples, unsigned int );
00332
00333 itkSetMacro( SlabTolerance, double );
00334 itkGetConstReferenceMacro( SlabTolerance, double );
00335
00341 itkSetMacro( UsingBiasFieldCorrection, bool );
00342 itkGetConstReferenceMacro( UsingBiasFieldCorrection, bool );
00343
00346 itkSetMacro( GeneratingOutput, bool );
00347 itkGetConstReferenceMacro( GeneratingOutput, bool );
00348
00351 itkSetMacro( SlicingDirection , int );
00352
00354 itkSetMacro( BiasFieldDegree, int );
00355 itkGetMacro( BiasFieldDegree, int );
00356
00359 void SetInitialBiasFieldCoefficients
00360 (const BiasFieldType::CoefficientArrayType &coefficients)
00361 { this->Modified() ; m_BiasFieldCoefficients = coefficients ; }
00362
00366 BiasFieldType::CoefficientArrayType GetEstimatedBiasFieldCoefficients()
00367 { return m_EstimatedBiasFieldCoefficients ; }
00368
00372 void SetTissueClassStatistics(const Array<double> & means,
00373 const Array<double> & sigmas)
00374 throw (ExceptionObject) ;
00375
00377 itkSetMacro( VolumeCorrectionMaximumIteration, int );
00378 itkGetMacro( VolumeCorrectionMaximumIteration, int );
00379 itkSetMacro( InterSliceCorrectionMaximumIteration, int );
00380 itkGetMacro( InterSliceCorrectionMaximumIteration, int );
00381
00383 void SetOptimizerInitialRadius(double initRadius)
00384 { m_OptimizerInitialRadius = initRadius ; }
00385 double GetOptimizerInitialRadius()
00386 { return m_OptimizerInitialRadius ; }
00387
00389 itkSetMacro( OptimizerGrowthFactor, double );
00390 itkGetMacro( OptimizerGrowthFactor, double );
00391
00394 itkSetMacro( OptimizerShrinkFactor, double );
00395 itkGetMacro( OptimizerShrinkFactor, double );
00396
00397
00404 void SetNumberOfLevels(unsigned int num);
00405
00407 itkGetMacro(NumberOfLevels, unsigned int);
00408
00415 void SetSchedule( const ScheduleType& schedule );
00416
00418 itkGetConstReferenceMacro(Schedule, ScheduleType);
00419
00424 void SetStartingShrinkFactors( unsigned int factor );
00425 void SetStartingShrinkFactors( unsigned int* factors );
00426
00428 const unsigned int * GetStartingShrinkFactors() const;
00429
00433 static bool IsScheduleDownwardDivisible( const ScheduleType& schedule );
00434
00435
00442 void Initialize() throw (ExceptionObject) ;
00443
00446 BiasFieldType EstimateBiasField(InputImageRegionType region,
00447 unsigned int degree,
00448 int maximumIteration) ;
00449
00453 void CorrectImage(BiasFieldType& bias,
00454 InputImageRegionType region) ;
00455
00458 void CorrectInterSliceIntensityInhomogeneity(InputImageRegionType region) ;
00459
00460 protected:
00461 MRIBiasFieldCorrectionFilter() ;
00462 virtual ~MRIBiasFieldCorrectionFilter() ;
00463 void PrintSelf(std::ostream& os, Indent indent) const;
00464
00467 bool CheckMaskImage(ImageMaskType* mask) ;
00468
00469 protected:
00473 void Log1PImage(InternalImageType* source,
00474 InternalImageType* target) ;
00475
00478 void ExpImage(InternalImageType* source,
00479 InternalImageType* target) ;
00480
00483 template<class TSource, class TTarget>
00484 void CopyAndConvertImage(const TSource * source,
00485 TTarget * target,
00486 typename TTarget::RegionType requestedRegion)
00487 {
00488 typedef ImageRegionConstIterator<TSource> SourceIterator ;
00489 typedef ImageRegionIterator<TTarget> TargetIterator ;
00490 typedef typename TTarget::PixelType TargetPixelType ;
00491
00492 SourceIterator s_iter(source, requestedRegion) ;
00493 TargetIterator t_iter(target, requestedRegion) ;
00494
00495 s_iter.GoToBegin() ;
00496 t_iter.GoToBegin() ;
00497 while (!s_iter.IsAtEnd())
00498 {
00499 t_iter.Set(static_cast<TargetPixelType>( s_iter.Get() ) ) ;
00500 ++s_iter ;
00501 ++t_iter ;
00502 }
00503 }
00504
00509 void GetBiasFieldSize(InputImageRegionType region,
00510 BiasFieldType::DomainSizeType& domainSize) ;
00511
00515 void AdjustSlabRegions(SlabRegionVectorType& slabs,
00516 OutputImageRegionType requestedRegion) ;
00517
00518 void GenerateData() ;
00519
00520 private:
00521 MRIBiasFieldCorrectionFilter(const Self&);
00522 void operator=(const Self&);
00523
00525 EnergyFunctionPointer m_EnergyFunction ;
00526
00528 NormalVariateGeneratorType::Pointer m_NormalVariateGenerator ;
00529
00531 ImageMaskPointer m_InputMask ;
00532
00534 ImageMaskPointer m_OutputMask ;
00535
00537 InternalImagePointer m_InternalInput ;
00538
00540 SlabRegionVectorType m_Slabs ;
00541
00543 int m_SlicingDirection ;
00544
00546 bool m_BiasMultiplicative ;
00547
00549 bool m_UsingInterSliceIntensityCorrection ;
00550 bool m_UsingSlabIdentification ;
00551 bool m_UsingBiasFieldCorrection ;
00552 bool m_GeneratingOutput ;
00553
00554 unsigned int m_SlabNumberOfSamples ;
00555 InputImagePixelType m_SlabBackgroundMinimumThreshold ;
00556 double m_SlabTolerance ;
00558 int m_BiasFieldDegree ;
00559
00561 unsigned int m_NumberOfLevels;
00563 ScheduleType m_Schedule;
00564
00567 BiasFieldType::CoefficientArrayType m_BiasFieldCoefficients ;
00568
00571 BiasFieldType::CoefficientArrayType m_EstimatedBiasFieldCoefficients ;
00572
00574 int m_VolumeCorrectionMaximumIteration ;
00575
00577 int m_InterSliceCorrectionMaximumIteration ;
00578
00580 double m_OptimizerInitialRadius ;
00581
00583 double m_OptimizerGrowthFactor ;
00584
00586 double m_OptimizerShrinkFactor ;
00587
00589 Array<double> m_TissueClassMeans ;
00590
00592 Array<double> m_TissueClassSigmas ;
00593 };
00594
00595
00596
00597
00598
00599 }
00600
00601 #ifndef ITK_MANUAL_INSTANTIATION
00602 #include "itkMRIBiasFieldCorrectionFilter.txx"
00603 #endif
00604
00605 #endif