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
00109 void SetSamplingFactors(SamplingFactorType factor)
00110 {
00111 for (unsigned int i = 0; i < SpaceDimension; i++)
00112 {
00113 m_SamplingFactor[i] = factor[i];
00114 }
00115 }
00116
00119 double GetEnergy0(double diff)
00120 {
00121 return (*m_InternalEnergyFunction)(diff);
00122 }
00123
00126 MeasureType GetValue(const ParametersType & parameters ) const ;
00127
00130 void GetDerivative( const ParametersType & itkNotUsed(parameters),
00131 DerivativeType & itkNotUsed(derivative) ) const
00132 { }
00133
00138 void InitializeDistributions( Array<double> classMeans,
00139 Array<double> classSigmas );
00140
00141 unsigned int GetNumberOfParameters(void) const;
00142
00143 private:
00144
00146 BiasFieldType * m_BiasField ;
00147
00149 ImagePointer m_Image ;
00150
00152 MaskPointer m_Mask ;
00153
00155 ImageRegionType m_Region ;
00156
00158 InternalEnergyFunction* m_InternalEnergyFunction ;
00159
00161 SamplingFactorType m_SamplingFactor;
00162
00163 protected:
00165 MRIBiasEnergyFunction();
00166
00168 virtual ~MRIBiasEnergyFunction();
00169
00170
00171 private:
00172
00173 MRIBiasEnergyFunction(const Self&);
00174 void operator=(const Self&);
00175
00176 } ;
00177
00178
00179
00229 template <class TInputImage, class TOutputImage, class TMaskImage>
00230 class ITK_EXPORT MRIBiasFieldCorrectionFilter :
00231 public ImageToImageFilter< TInputImage, TOutputImage >
00232 {
00233 public:
00235 typedef MRIBiasFieldCorrectionFilter Self;
00236 typedef ImageToImageFilter< TInputImage, TOutputImage > Superclass;
00237 typedef SmartPointer<Self> Pointer;
00238 typedef SmartPointer<const Self> ConstPointer;
00239
00241 itkNewMacro(Self);
00242
00244 itkTypeMacro(MRIBiasFieldCorrectionFilter, ImageToImageFilter);
00245
00247 itkStaticConstMacro(ImageDimension, unsigned int,
00248 TOutputImage::ImageDimension);
00249
00251 typedef TOutputImage OutputImageType ;
00252 typedef TInputImage InputImageType ;
00253 typedef typename TOutputImage::Pointer OutputImagePointer ;
00254 typedef typename TOutputImage::IndexType OutputImageIndexType ;
00255 typedef typename TOutputImage::PixelType OutputImagePixelType ;
00256 typedef typename TOutputImage::SizeType OutputImageSizeType;
00257 typedef typename TOutputImage::RegionType OutputImageRegionType;
00258 typedef typename TInputImage::Pointer InputImagePointer ;
00259 typedef typename TInputImage::IndexType InputImageIndexType;
00260 typedef typename TInputImage::PixelType InputImagePixelType;
00261 typedef typename TInputImage::SizeType InputImageSizeType;
00262 typedef typename TInputImage::RegionType InputImageRegionType;
00263
00265 typedef TMaskImage ImageMaskType ;
00266 typedef typename ImageMaskType::Pointer ImageMaskPointer ;
00267 typedef typename ImageMaskType::RegionType ImageMaskRegionType ;
00268
00270 typedef Image< float, itkGetStaticConstMacro(ImageDimension) >
00271 InternalImageType ;
00272 typedef typename InternalImageType::PixelType InternalImagePixelType ;
00273 typedef typename InternalImageType::Pointer InternalImagePointer ;
00274 typedef typename InternalImageType::RegionType InternalImageRegionType ;
00275
00277 typedef MRASlabIdentifier<InputImageType> MRASlabIdentifierType ;
00278 typedef typename MRASlabIdentifierType::SlabRegionVectorType
00279 SlabRegionVectorType ;
00280 typedef typename SlabRegionVectorType::iterator SlabRegionVectorIteratorType ;
00281
00283 typedef MultivariateLegendrePolynomial BiasFieldType;
00284
00286 typedef MRIBiasEnergyFunction<InternalImageType,
00287 ImageMaskType,
00288 BiasFieldType> EnergyFunctionType;
00289 typedef typename EnergyFunctionType::Pointer EnergyFunctionPointer;
00290
00292 typedef Statistics::NormalVariateGenerator NormalVariateGeneratorType ;
00293
00295 typedef OnePlusOneEvolutionaryOptimizer OptimizerType ;
00296
00298 typedef Array2D<unsigned int> ScheduleType;
00299
00303 void SetInputMask(ImageMaskType* inputMask);
00304 itkGetObjectMacro( InputMask, ImageMaskType );
00305
00308 void SetOutputMask(ImageMaskType* outputMask) ;
00309
00311 itkGetObjectMacro( OutputMask, ImageMaskType );
00312
00316 void IsBiasFieldMultiplicative(bool flag)
00317 { m_BiasMultiplicative = flag ; }
00318
00320 bool IsBiasFieldMultiplicative()
00321 { return m_BiasMultiplicative ; }
00322
00326 itkSetMacro( UsingInterSliceIntensityCorrection, bool );
00327 itkGetConstReferenceMacro( UsingInterSliceIntensityCorrection, bool );
00328
00334 itkSetMacro( UsingSlabIdentification, bool );
00335 itkGetConstReferenceMacro( UsingSlabIdentification, bool );
00336
00337 itkSetMacro( SlabBackgroundMinimumThreshold, InputImagePixelType );
00338 itkGetConstReferenceMacro( SlabBackgroundMinimumThreshold,
00339 InputImagePixelType );
00340
00341 itkSetMacro( SlabNumberOfSamples, unsigned int );
00342 itkGetConstReferenceMacro( SlabNumberOfSamples, unsigned int );
00343
00344 itkSetMacro( SlabTolerance, double );
00345 itkGetConstReferenceMacro( SlabTolerance, double );
00346
00352 itkSetMacro( UsingBiasFieldCorrection, bool );
00353 itkGetConstReferenceMacro( UsingBiasFieldCorrection, bool );
00354
00357 itkSetMacro( GeneratingOutput, bool );
00358 itkGetConstReferenceMacro( GeneratingOutput, bool );
00359
00362 itkSetMacro( SlicingDirection , int );
00363
00365 itkSetMacro( BiasFieldDegree, int );
00366 itkGetMacro( BiasFieldDegree, int );
00367
00370 void SetInitialBiasFieldCoefficients(const
00371 BiasFieldType::CoefficientArrayType
00372 &coefficients)
00373 { this->Modified() ; m_BiasFieldCoefficients = coefficients ; }
00374
00378 BiasFieldType::CoefficientArrayType GetEstimatedBiasFieldCoefficients()
00379 { return m_EstimatedBiasFieldCoefficients ; }
00380
00384 void SetTissueClassStatistics(const Array<double> & means,
00385 const Array<double> & sigmas)
00386 throw (ExceptionObject) ;
00387
00389 itkSetMacro( VolumeCorrectionMaximumIteration, int );
00390 itkGetMacro( VolumeCorrectionMaximumIteration, int );
00391 itkSetMacro( InterSliceCorrectionMaximumIteration, int );
00392 itkGetMacro( InterSliceCorrectionMaximumIteration, int );
00393
00395 void SetOptimizerInitialRadius(double initRadius)
00396 { m_OptimizerInitialRadius = initRadius ; }
00397 double GetOptimizerInitialRadius()
00398 { return m_OptimizerInitialRadius ; }
00399
00401 itkSetMacro( OptimizerGrowthFactor, double );
00402 itkGetMacro( OptimizerGrowthFactor, double );
00403
00406 itkSetMacro( OptimizerShrinkFactor, double );
00407 itkGetMacro( OptimizerShrinkFactor, double );
00408
00409
00416 void SetNumberOfLevels(unsigned int num);
00417
00419 itkGetMacro(NumberOfLevels, unsigned int);
00420
00427 void SetSchedule( const ScheduleType& schedule );
00428
00430 itkGetConstReferenceMacro(Schedule, ScheduleType);
00431
00436 void SetStartingShrinkFactors( unsigned int factor );
00437 void SetStartingShrinkFactors( unsigned int* factors );
00438
00440 const unsigned int * GetStartingShrinkFactors() const;
00441
00445 static bool IsScheduleDownwardDivisible( const ScheduleType& schedule );
00446
00447
00454 void Initialize() throw (ExceptionObject) ;
00455
00458 BiasFieldType EstimateBiasField(InputImageRegionType region,
00459 unsigned int degree,
00460 int maximumIteration) ;
00461
00465 void CorrectImage(BiasFieldType& bias,
00466 InputImageRegionType region) ;
00467
00470 void CorrectInterSliceIntensityInhomogeneity(InputImageRegionType region) ;
00471
00472 protected:
00473 MRIBiasFieldCorrectionFilter() ;
00474 virtual ~MRIBiasFieldCorrectionFilter() ;
00475 void PrintSelf(std::ostream& os, Indent indent) const;
00476
00479 bool CheckMaskImage(ImageMaskType* mask) ;
00480
00481 protected:
00485 void Log1PImage(InternalImageType* source,
00486 InternalImageType* target) ;
00487
00490 void ExpImage(InternalImageType* source,
00491 InternalImageType* target) ;
00492
00495 template<class TSource, class TTarget>
00496 void CopyAndConvertImage(const TSource * source,
00497 TTarget * target,
00498 typename TTarget::RegionType requestedRegion)
00499 {
00500 typedef ImageRegionConstIterator<TSource> SourceIterator ;
00501 typedef ImageRegionIterator<TTarget> TargetIterator ;
00502 typedef typename TTarget::PixelType TargetPixelType ;
00503
00504 SourceIterator s_iter(source, requestedRegion) ;
00505 TargetIterator t_iter(target, requestedRegion) ;
00506
00507 s_iter.GoToBegin() ;
00508 t_iter.GoToBegin() ;
00509 while (!s_iter.IsAtEnd())
00510 {
00511 t_iter.Set(static_cast<TargetPixelType>( s_iter.Get() ) ) ;
00512 ++s_iter ;
00513 ++t_iter ;
00514 }
00515 }
00516
00521 void GetBiasFieldSize(InputImageRegionType region,
00522 BiasFieldType::DomainSizeType& domainSize) ;
00523
00527 void AdjustSlabRegions(SlabRegionVectorType& slabs,
00528 OutputImageRegionType requestedRegion) ;
00529
00530 void GenerateData() ;
00531
00532 private:
00533 MRIBiasFieldCorrectionFilter(const Self&);
00534 void operator=(const Self&);
00535
00537 EnergyFunctionPointer m_EnergyFunction ;
00538
00540 NormalVariateGeneratorType::Pointer m_NormalVariateGenerator ;
00541
00543 ImageMaskPointer m_InputMask ;
00544
00546 ImageMaskPointer m_OutputMask ;
00547
00549 InternalImagePointer m_InternalInput ;
00550
00552 SlabRegionVectorType m_Slabs ;
00553
00555 int m_SlicingDirection ;
00556
00558 bool m_BiasMultiplicative ;
00559
00561 bool m_UsingInterSliceIntensityCorrection ;
00562 bool m_UsingSlabIdentification ;
00563 bool m_UsingBiasFieldCorrection ;
00564 bool m_GeneratingOutput ;
00565
00566 unsigned int m_SlabNumberOfSamples ;
00567 InputImagePixelType m_SlabBackgroundMinimumThreshold ;
00568 double m_SlabTolerance ;
00570 int m_BiasFieldDegree ;
00571
00573 unsigned int m_NumberOfLevels;
00575 ScheduleType m_Schedule;
00576
00579 BiasFieldType::CoefficientArrayType m_BiasFieldCoefficients ;
00580
00583 BiasFieldType::CoefficientArrayType m_EstimatedBiasFieldCoefficients ;
00584
00586 int m_VolumeCorrectionMaximumIteration ;
00587
00589 int m_InterSliceCorrectionMaximumIteration ;
00590
00592 double m_OptimizerInitialRadius ;
00593
00595 double m_OptimizerGrowthFactor ;
00596
00598 double m_OptimizerShrinkFactor ;
00599
00601 Array<double> m_TissueClassMeans ;
00602
00604 Array<double> m_TissueClassSigmas ;
00605 };
00606
00607
00608
00609
00610
00611 }
00612
00613 #ifndef ITK_MANUAL_INSTANTIATION
00614 #include "itkMRIBiasFieldCorrectionFilter.txx"
00615 #endif
00616
00617 #endif