ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkMRIBiasFieldCorrectionFilter.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkMRIBiasFieldCorrectionFilter_h
19 #define itkMRIBiasFieldCorrectionFilter_h
20 
21 #include <ctime>
22 
23 #include "itkImageToImageFilter.h"
24 #include "itkArray2D.h"
25 #include "itkMRASlabIdentifier.h"
30 #include "itkImageRegionIterator.h"
31 
32 namespace itk
33 {
46 template< typename TImage, typename TImageMask, typename TBiasField >
47 class ITK_TEMPLATE_EXPORT MRIBiasEnergyFunction : public SingleValuedCostFunction
48 {
49 public:
55 
58 
60  itkNewMacro(Self);
61 
63  typedef TImage ImageType;
64  typedef typename ImageType::Pointer ImagePointer;
65  typedef typename ImageType::PixelType ImageElementType;
67  typedef typename ImageType::RegionType ImageRegionType;
68  typedef TImageMask MaskType;
69  typedef typename MaskType::Pointer MaskPointer;
70  typedef typename MaskType::PixelType MaskElementType;
71 
73  typedef TBiasField BiasFieldType;
74 
77  typedef typename Superclass::ParametersType ParametersType;
78 
80  typedef Superclass::DerivativeType DerivativeType;
81 
83  typedef Superclass::MeasureType MeasureType;
84 
85  itkStaticConstMacro(SpaceDimension, unsigned int, 3);
86 
88  typedef CompositeValleyFunction InternalEnergyFunction;
89 
91  typedef unsigned int SamplingFactorType[SpaceDimension];
92 
94  itkSetObjectMacro(Image, ImageType);
95 
97  itkSetObjectMacro(Mask, MaskType);
98 
100  itkSetMacro(Region, ImageRegionType);
101 
104  { m_BiasField = bias; }
105 
108  void SetSamplingFactors(SamplingFactorType factor)
109  {
110  for ( unsigned int i = 0; i < SpaceDimension; i++ )
111  {
112  m_SamplingFactor[i] = factor[i];
113  }
114  }
116 
119  double GetEnergy0(double diff)
120  {
121  return ( *m_InternalEnergyFunction )( diff );
122  }
123 
126  virtual MeasureType GetValue(const ParametersType & parameters) const ITK_OVERRIDE;
127 
130  void GetDerivative( const ParametersType & itkNotUsed(parameters),
131  DerivativeType & itkNotUsed(derivative) ) const ITK_OVERRIDE
132  {}
133 
138  void InitializeDistributions(Array< double > classMeans,
139  Array< double > classSigmas);
140 
141  virtual unsigned int GetNumberOfParameters(void) const ITK_OVERRIDE;
142 
143 protected:
146 
148  virtual ~MRIBiasEnergyFunction() ITK_OVERRIDE;
149 
150 private:
151  ITK_DISALLOW_COPY_AND_ASSIGN(MRIBiasEnergyFunction);
152 
154  BiasFieldType *m_BiasField;
155 
157  ImagePointer m_Image;
158 
160  MaskPointer m_Mask;
161 
163  ImageRegionType m_Region;
164 
166  InternalEnergyFunction *m_InternalEnergyFunction;
167 
169  SamplingFactorType m_SamplingFactor;
170 }; // end of class
171 
222 template< typename TInputImage, typename TOutputImage, typename TMaskImage >
223 class ITK_TEMPLATE_EXPORT MRIBiasFieldCorrectionFilter :
224  public ImageToImageFilter< TInputImage, TOutputImage >
225 {
226 public:
232 
234  itkNewMacro(Self);
235 
238 
240  itkStaticConstMacro(ImageDimension, unsigned int,
241  TOutputImage::ImageDimension);
242 
244  typedef TOutputImage OutputImageType;
245  typedef typename TOutputImage::Pointer OutputImagePointer;
247  typedef typename TOutputImage::PixelType OutputImagePixelType;
249  typedef typename TOutputImage::RegionType OutputImageRegionType;
250 
251  typedef TInputImage InputImageType;
252  typedef typename TInputImage::Pointer InputImagePointer;
254  typedef typename TInputImage::PixelType InputImagePixelType;
256  typedef typename TInputImage::RegionType InputImageRegionType;
257 
259  typedef TMaskImage ImageMaskType;
260  typedef typename ImageMaskType::Pointer ImageMaskPointer;
261  typedef typename ImageMaskType::RegionType ImageMaskRegionType;
262 
269 
273  typedef typename SlabRegionVectorType::iterator SlabRegionVectorIteratorType;
274 
277 
279  typedef MRIBiasEnergyFunction< InternalImageType,
280  ImageMaskType,
281  BiasFieldType > EnergyFunctionType;
283 
286 
289 
292 
296  void SetInputMask(ImageMaskType *inputMask);
297  itkGetModifiableObjectMacro(InputMask, ImageMaskType);
299 
302  void SetOutputMask(ImageMaskType *outputMask);
303  itkGetModifiableObjectMacro(OutputMask, ImageMaskType);
305 
306 #if defined ( ITK_FUTURE_LEGACY_REMOVE )
307 
310  void IsBiasFieldMultiplicative(bool flag)
311  { m_BiasFieldMultiplicative = flag; }
312 #endif // defined ( ITK_FUTURE_LEGACY_REMOVE )
313 
317  itkSetMacro(BiasFieldMultiplicative, bool);
318  itkGetConstMacro(BiasFieldMultiplicative, bool);
319  itkBooleanMacro(BiasFieldMultiplicative);
321 
322 #if defined ( ITK_FUTURE_LEGACY_REMOVE )
323 
324  bool IsBiasFieldMultiplicative()
325  { return m_BiasFieldMultiplicative; }
326 #endif // defined ( ITK_FUTURE_LEGACY_REMOVE )
327 
332  itkSetMacro(UsingInterSliceIntensityCorrection, bool);
333  itkGetConstMacro(UsingInterSliceIntensityCorrection, bool);
334  itkBooleanMacro(UsingInterSliceIntensityCorrection);
336 
342  itkSetMacro(UsingSlabIdentification, bool);
343  itkGetConstMacro(UsingSlabIdentification, bool);
344  itkBooleanMacro(UsingSlabIdentification);
346 
347  itkSetMacro(SlabBackgroundMinimumThreshold, InputImagePixelType);
348  itkGetConstReferenceMacro(SlabBackgroundMinimumThreshold,
349  InputImagePixelType);
350 
351  itkSetMacro(SlabNumberOfSamples, unsigned int);
352  itkGetConstReferenceMacro(SlabNumberOfSamples, unsigned int);
353 
354  itkSetMacro(SlabTolerance, double);
355  itkGetConstReferenceMacro(SlabTolerance, double);
356 
362  itkSetMacro(UsingBiasFieldCorrection, bool);
363  itkGetConstMacro(UsingBiasFieldCorrection, bool);
364  itkBooleanMacro(UsingBiasFieldCorrection);
366 
369  itkSetMacro(GeneratingOutput, bool);
370  itkGetConstMacro(GeneratingOutput, bool);
371  itkBooleanMacro(GeneratingOutput);
373 
376  itkSetMacro(SlicingDirection, int);
377  itkGetConstMacro(SlicingDirection, int);
379 
381  itkSetMacro(BiasFieldDegree, int);
382  itkGetConstMacro(BiasFieldDegree, int);
384 
389  & coefficients)
390  { this->Modified(); m_BiasFieldCoefficients = coefficients; }
391 
396  { return m_EstimatedBiasFieldCoefficients; }
397 
401  void SetTissueClassStatistics(const Array< double > & means,
402  const Array< double > & sigmas);
403 
406  itkSetMacro(VolumeCorrectionMaximumIteration, int);
407  itkGetConstMacro(VolumeCorrectionMaximumIteration, int);
409 
412  itkSetMacro(InterSliceCorrectionMaximumIteration, int);
413  itkGetConstMacro(InterSliceCorrectionMaximumIteration, int);
415 
417  itkSetMacro(OptimizerInitialRadius, double);
418  itkGetConstMacro(OptimizerInitialRadius, double);
420 
422  itkSetMacro(OptimizerGrowthFactor, double);
423  itkGetConstMacro(OptimizerGrowthFactor, double);
425 
428  itkSetMacro(OptimizerShrinkFactor, double);
429  itkGetConstMacro(OptimizerShrinkFactor, double);
430 
437  void SetNumberOfLevels(unsigned int num);
438 
440  itkGetConstMacro(NumberOfLevels, unsigned int);
441 
448  void SetSchedule(const ScheduleType & schedule);
449 
451  itkGetConstReferenceMacro(Schedule, ScheduleType);
452 
457  void SetStartingShrinkFactors(unsigned int factor);
458 
459  void SetStartingShrinkFactors(unsigned int *factors);
460 
462  const unsigned int * GetStartingShrinkFactors() const;
463 
467  static bool IsScheduleDownwardDivisible(const ScheduleType & schedule);
468 
475  void Initialize();
476 
479  BiasFieldType EstimateBiasField(InputImageRegionType region,
480  unsigned int degree,
481  int maximumIteration);
482 
486  void CorrectImage(BiasFieldType & bias,
487  InputImageRegionType region);
488 
491  void CorrectInterSliceIntensityInhomogeneity(InputImageRegionType region);
492 
493 protected:
495  virtual ~MRIBiasFieldCorrectionFilter() ITK_OVERRIDE;
496  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
497 
500  bool CheckMaskImage(ImageMaskType *mask);
501 
502 protected:
506  void Log1PImage(InternalImageType *source,
507  InternalImageType *target);
508 
511  void ExpImage(InternalImageType *source,
512  InternalImageType *target);
513 
516  template< typename TSource, typename TTarget >
517  void CopyAndConvertImage(const TSource *source,
518  TTarget *target,
519  typename TTarget::RegionType requestedRegion)
520  {
521  typedef ImageRegionConstIterator< TSource > SourceIterator;
522  typedef ImageRegionIterator< TTarget > TargetIterator;
523  typedef typename TTarget::PixelType TargetPixelType;
524 
525  SourceIterator s_iter(source, requestedRegion);
526  TargetIterator t_iter(target, requestedRegion);
527 
528  s_iter.GoToBegin();
529  t_iter.GoToBegin();
530  while ( !s_iter.IsAtEnd() )
531  {
532  t_iter.Set( static_cast< TargetPixelType >( s_iter.Get() ) );
533  ++s_iter;
534  ++t_iter;
535  }
536  }
537 
542  void GetBiasFieldSize(InputImageRegionType region,
543  BiasFieldType::DomainSizeType & domainSize);
544 
548  void AdjustSlabRegions(SlabRegionVectorType & slabs,
549  OutputImageRegionType requestedRegion);
550 
551  void GenerateData() ITK_OVERRIDE;
552 
553 private:
554  ITK_DISALLOW_COPY_AND_ASSIGN(MRIBiasFieldCorrectionFilter);
555 
557  EnergyFunctionPointer m_EnergyFunction;
558 
560  NormalVariateGeneratorType::Pointer m_NormalVariateGenerator;
561 
563  ImageMaskPointer m_InputMask;
564 
566  ImageMaskPointer m_OutputMask;
567 
569  InternalImagePointer m_InternalInput;
570 
572  SlabRegionVectorType m_Slabs;
573 
575  int m_SlicingDirection;
576 
578  bool m_BiasFieldMultiplicative;
579 
581  bool m_UsingInterSliceIntensityCorrection;
582  bool m_UsingSlabIdentification;
583  bool m_UsingBiasFieldCorrection;
584  bool m_GeneratingOutput;
585 
586  unsigned int m_SlabNumberOfSamples;
587  InputImagePixelType m_SlabBackgroundMinimumThreshold;
588  double m_SlabTolerance;
589 
591  int m_BiasFieldDegree;
592 
594  unsigned int m_NumberOfLevels;
595 
597  ScheduleType m_Schedule;
598 
601  BiasFieldType::CoefficientArrayType m_BiasFieldCoefficients;
602 
605  BiasFieldType::CoefficientArrayType m_EstimatedBiasFieldCoefficients;
606 
607  int m_VolumeCorrectionMaximumIteration;
608 
609  int m_InterSliceCorrectionMaximumIteration;
610 
612  double m_OptimizerInitialRadius;
613 
615  double m_OptimizerGrowthFactor;
616 
618  double m_OptimizerShrinkFactor;
619 
621  Array< double > m_TissueClassMeans;
622 
624  Array< double > m_TissueClassSigmas;
625 };
626 
627 } // end namespace itk
628 
629 #ifndef ITK_MANUAL_INSTANTIATION
630 #include "itkMRIBiasFieldCorrectionFilter.hxx"
631 #endif
632 
633 #endif
Array class with size defined at construction time.
Definition: itkArray.h:50
Superclass::RegionType RegionType
Definition: itkImage.h:137
Superclass::DerivativeType DerivativeType
SmartPointer< const Self > ConstPointer
InternalImageType::PixelType InternalImagePixelType
This class is a base for the CostFunctions returning a single value.
Light weight base class for most itk classes.
InternalImageType::RegionType InternalImageRegionType
CompositeValleyFunction InternalEnergyFunction
std::vector< ImageRegionType > SlabRegionVectorType
ImageToImageFilter< TInputImage, TOutputImage > Superclass
TPixel PixelType
Definition: itkImage.h:89
Superclass::ParametersType ParametersType
MRIBiasEnergyFunction< InternalImageType, ImageMaskType, BiasFieldType > EnergyFunctionType
BiasFieldType::CoefficientArrayType GetEstimatedBiasFieldCoefficients()
Image< float, itkGetStaticConstMacro(ImageDimension) > InternalImageType
A multi-dimensional iterator templated over image type that walks a region of pixels.
Represents a cost function for MRI bias field correction optimization.
2D and 3D multivariate Legendre Polynomial
A region represents some portion or piece of data.
Definition: itkRegion.h:64
identifies slab in MR images comparing minimum intensity averages
void GetDerivative(const ParametersType &, DerivativeType &) const override
OnePlusOneEvolutionaryOptimizer OptimizerType
Base class for filters that take an image as input and produce an image as output.
Statistics::NormalVariateGenerator NormalVariateGeneratorType
void SetSamplingFactors(SamplingFactorType factor)
Control indentation during Print() invocation.
Definition: itkIndent.h:49
MRASlabIdentifierType::SlabRegionVectorType SlabRegionVectorType
SlabRegionVectorType::iterator SlabRegionVectorIteratorType
MRASlabIdentifier< InputImageType > MRASlabIdentifierType
void SetInitialBiasFieldCoefficients(const BiasFieldType::CoefficientArrayType &coefficients)
Templated n-dimensional image class.
Definition: itkImage.h:75
A multi-dimensional iterator templated over image type that walks a region of pixels.