ITK  4.12.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;
66  typedef typename ImageType::IndexType ImageIndexType;
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();
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;
246  typedef typename TOutputImage::IndexType OutputImageIndexType;
247  typedef typename TOutputImage::PixelType OutputImagePixelType;
248  typedef typename TOutputImage::SizeType OutputImageSizeType;
249  typedef typename TOutputImage::RegionType OutputImageRegionType;
250 
251  typedef TInputImage InputImageType;
253  typedef typename TInputImage::IndexType InputImageIndexType;
254  typedef typename TInputImage::PixelType InputImagePixelType;
255  typedef typename TInputImage::SizeType InputImageSizeType;
256  typedef typename TInputImage::RegionType InputImageRegionType;
257 
259  typedef TMaskImage ImageMaskType;
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  throw ( ExceptionObject );
404 
407  itkSetMacro(VolumeCorrectionMaximumIteration, int);
408  itkGetConstMacro(VolumeCorrectionMaximumIteration, int);
410 
413  itkSetMacro(InterSliceCorrectionMaximumIteration, int);
414  itkGetConstMacro(InterSliceCorrectionMaximumIteration, int);
416 
418  itkSetMacro(OptimizerInitialRadius, double);
419  itkGetConstMacro(OptimizerInitialRadius, double);
421 
423  itkSetMacro(OptimizerGrowthFactor, double);
424  itkGetConstMacro(OptimizerGrowthFactor, double);
426 
429  itkSetMacro(OptimizerShrinkFactor, double);
430  itkGetConstMacro(OptimizerShrinkFactor, double);
431 
438  void SetNumberOfLevels(unsigned int num);
439 
441  itkGetConstMacro(NumberOfLevels, unsigned int);
442 
449  void SetSchedule(const ScheduleType & schedule);
450 
452  itkGetConstReferenceMacro(Schedule, ScheduleType);
453 
458  void SetStartingShrinkFactors(unsigned int factor);
459 
460  void SetStartingShrinkFactors(unsigned int *factors);
461 
463  const unsigned int * GetStartingShrinkFactors() const;
464 
468  static bool IsScheduleDownwardDivisible(const ScheduleType & schedule);
469 
476  void Initialize()
477  throw ( ExceptionObject );
478 
481  BiasFieldType EstimateBiasField(InputImageRegionType region,
482  unsigned int degree,
483  int maximumIteration);
484 
488  void CorrectImage(BiasFieldType & bias,
489  InputImageRegionType region);
490 
493  void CorrectInterSliceIntensityInhomogeneity(InputImageRegionType region);
494 
495 protected:
497  virtual ~MRIBiasFieldCorrectionFilter();
498  void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
499 
502  bool CheckMaskImage(ImageMaskType *mask);
503 
504 protected:
508  void Log1PImage(InternalImageType *source,
509  InternalImageType *target);
510 
513  void ExpImage(InternalImageType *source,
514  InternalImageType *target);
515 
518  template< typename TSource, typename TTarget >
519  void CopyAndConvertImage(const TSource *source,
520  TTarget *target,
521  typename TTarget::RegionType requestedRegion)
522  {
523  typedef ImageRegionConstIterator< TSource > SourceIterator;
524  typedef ImageRegionIterator< TTarget > TargetIterator;
525  typedef typename TTarget::PixelType TargetPixelType;
526 
527  SourceIterator s_iter(source, requestedRegion);
528  TargetIterator t_iter(target, requestedRegion);
529 
530  s_iter.GoToBegin();
531  t_iter.GoToBegin();
532  while ( !s_iter.IsAtEnd() )
533  {
534  t_iter.Set( static_cast< TargetPixelType >( s_iter.Get() ) );
535  ++s_iter;
536  ++t_iter;
537  }
538  }
539 
544  void GetBiasFieldSize(InputImageRegionType region,
545  BiasFieldType::DomainSizeType & domainSize);
546 
550  void AdjustSlabRegions(SlabRegionVectorType & slabs,
551  OutputImageRegionType requestedRegion);
552 
553  void GenerateData() ITK_OVERRIDE;
554 
555 private:
556  ITK_DISALLOW_COPY_AND_ASSIGN(MRIBiasFieldCorrectionFilter);
557 
559  EnergyFunctionPointer m_EnergyFunction;
560 
562  NormalVariateGeneratorType::Pointer m_NormalVariateGenerator;
563 
565  ImageMaskPointer m_InputMask;
566 
568  ImageMaskPointer m_OutputMask;
569 
571  InternalImagePointer m_InternalInput;
572 
574  SlabRegionVectorType m_Slabs;
575 
577  int m_SlicingDirection;
578 
580  bool m_BiasFieldMultiplicative;
581 
583  bool m_UsingInterSliceIntensityCorrection;
584  bool m_UsingSlabIdentification;
585  bool m_UsingBiasFieldCorrection;
586  bool m_GeneratingOutput;
587 
588  unsigned int m_SlabNumberOfSamples;
589  InputImagePixelType m_SlabBackgroundMinimumThreshold;
590  double m_SlabTolerance;
591 
593  int m_BiasFieldDegree;
594 
596  unsigned int m_NumberOfLevels;
597 
599  ScheduleType m_Schedule;
600 
603  BiasFieldType::CoefficientArrayType m_BiasFieldCoefficients;
604 
607  BiasFieldType::CoefficientArrayType m_EstimatedBiasFieldCoefficients;
608 
609  int m_VolumeCorrectionMaximumIteration;
610 
611  int m_InterSliceCorrectionMaximumIteration;
612 
614  double m_OptimizerInitialRadius;
615 
617  double m_OptimizerGrowthFactor;
618 
620  double m_OptimizerShrinkFactor;
621 
623  Array< double > m_TissueClassMeans;
624 
626  Array< double > m_TissueClassSigmas;
627 };
628 
629 } // end namespace itk
630 
631 #ifndef ITK_MANUAL_INSTANTIATION
632 #include "itkMRIBiasFieldCorrectionFilter.hxx"
633 #endif
634 
635 #endif
Array class with size defined at construction time.
Definition: itkArray.h:50
virtual void PrintSelf(std::ostream &os, Indent indent) const override
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
virtual void Initialize()
CompositeValleyFunction InternalEnergyFunction
An image region represents a structured region of data.
std::vector< ImageRegionType > SlabRegionVectorType
ImageToImageFilter< TInputImage, TOutputImage > Superclass
TPixel PixelType
Definition: itkImage.h:89
Superclass::ParametersType ParametersType
SmartPointer< Self > Pointer
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
Standard exception handling object.
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.