ITK  4.0.0
Insight Segmentation and Registration Toolkit
itkMattesMutualInformationImageToImageMetric.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkMattesMutualInformationImageToImageMetric_h
00019 #define __itkMattesMutualInformationImageToImageMetric_h
00020 
00021 #include "itkImageToImageMetric.h"
00022 #include "itkPoint.h"
00023 #include "itkIndex.h"
00024 #include "itkBSplineDerivativeKernelFunction.h"
00025 #include "itkArray2D.h"
00026 
00027 namespace itk
00028 {
00110 template <class TFixedImage, class TMovingImage>
00111 class ITK_EXPORT MattesMutualInformationImageToImageMetric:
00112   public ImageToImageMetric<TFixedImage, TMovingImage>
00113 {
00114 public:
00115 
00117   typedef MattesMutualInformationImageToImageMetric     Self;
00118   typedef ImageToImageMetric<TFixedImage, TMovingImage> Superclass;
00119   typedef SmartPointer<Self>                            Pointer;
00120   typedef SmartPointer<const Self>                      ConstPointer;
00121 
00123   itkNewMacro(Self);
00124 
00126   itkTypeMacro(MattesMutualInformationImageToImageMetric,
00127                ImageToImageMetric);
00128 
00130   typedef typename Superclass::TransformType                  TransformType;
00131   typedef typename Superclass::TransformPointer               TransformPointer;
00132   typedef typename Superclass::TransformJacobianType          TransformJacobianType;
00133   typedef typename Superclass::InterpolatorType               InterpolatorType;
00134   typedef typename Superclass::MeasureType                    MeasureType;
00135   typedef typename Superclass::DerivativeType                 DerivativeType;
00136   typedef typename Superclass::ParametersType                 ParametersType;
00137   typedef typename Superclass::FixedImageType                 FixedImageType;
00138   typedef typename Superclass::MovingImageType                MovingImageType;
00139   typedef typename Superclass::MovingImagePointType           MovingImagePointType;
00140   typedef typename Superclass::FixedImageConstPointer         FixedImageConstPointer;
00141   typedef typename Superclass::MovingImageConstPointer        MovingImageConstPointer;
00142   typedef typename Superclass::BSplineTransformWeightsType    BSplineTransformWeightsType;
00143   typedef typename Superclass::BSplineTransformIndexArrayType BSplineTransformIndexArrayType;
00144 
00145   typedef typename Superclass::CoordinateRepresentationType CoordinateRepresentationType;
00146   typedef typename Superclass::FixedImageSampleContainer    FixedImageSampleContainer;
00147   typedef typename Superclass::ImageDerivativesType         ImageDerivativesType;
00148   typedef typename Superclass::WeightsValueType             WeightsValueType;
00149   typedef typename Superclass::IndexValueType               IndexValueType;
00150 
00151   typedef typename FixedImageType::OffsetValueType OffsetValueType;
00152 
00154   itkStaticConstMacro(MovingImageDimension, unsigned int,
00155                       MovingImageType::ImageDimension);
00156 
00164   virtual void Initialize(void)
00165   throw ( ExceptionObject );
00166 
00168   MeasureType GetValue(const ParametersType & parameters) const;
00169 
00171   void GetDerivative(const ParametersType & parameters, DerivativeType & Derivative) const;
00172 
00174   void GetValueAndDerivative(const ParametersType & parameters, MeasureType & Value, DerivativeType & Derivative) const;
00175 
00181   itkSetClampMacro( NumberOfHistogramBins, SizeValueType,
00182                     5, NumericTraits<SizeValueType>::max() );
00183   itkGetConstReferenceMacro(NumberOfHistogramBins, SizeValueType);
00185 
00210   itkSetMacro(UseExplicitPDFDerivatives, bool);
00211   itkGetConstReferenceMacro(UseExplicitPDFDerivatives, bool);
00212   itkBooleanMacro(UseExplicitPDFDerivatives);
00214 
00216   typedef float PDFValueType;
00217 
00219   typedef Image<PDFValueType, 2> JointPDFType;
00220   typedef Image<PDFValueType, 3> JointPDFDerivativesType;
00221 
00226   const typename JointPDFType::Pointer GetJointPDF () const
00227     {
00228     if( this->m_ThreaderJointPDF.size() == 0 )
00229       {
00230       return JointPDFType::Pointer(NULL);
00231       }
00232     return this->m_ThreaderJointPDF[0];
00233     }
00235 
00242   const typename JointPDFDerivativesType::Pointer GetJointPDFDerivatives () const
00243     {
00244     if( this->m_ThreaderJointPDFDerivatives.size() == 0 )
00245       {
00246       return JointPDFDerivativesType::Pointer(NULL);
00247       }
00248     return this->m_ThreaderJointPDFDerivatives[0];
00249     }
00250 protected:
00252 
00253   MattesMutualInformationImageToImageMetric();
00254   virtual ~MattesMutualInformationImageToImageMetric();
00255   void PrintSelf(std::ostream & os, Indent indent) const;
00256 
00257 private:
00258 
00259   // purposely not implemented
00260   MattesMutualInformationImageToImageMetric(const Self &);
00261   // purposely not implemented
00262   void operator=(const Self &);
00263 
00264   typedef JointPDFType::IndexType             JointPDFIndexType;
00265   typedef JointPDFType::PixelType             JointPDFValueType;
00266   typedef JointPDFType::RegionType            JointPDFRegionType;
00267   typedef JointPDFType::SizeType              JointPDFSizeType;
00268   typedef JointPDFDerivativesType::IndexType  JointPDFDerivativesIndexType;
00269   typedef JointPDFDerivativesType::PixelType  JointPDFDerivativesValueType;
00270   typedef JointPDFDerivativesType::RegionType JointPDFDerivativesRegionType;
00271   typedef JointPDFDerivativesType::SizeType   JointPDFDerivativesSizeType;
00272 
00274   typedef BSplineKernelFunction<3>           CubicBSplineFunctionType;
00275   typedef BSplineDerivativeKernelFunction<3> CubicBSplineDerivativeFunctionType;
00276 
00278   void ComputeFixedImageParzenWindowIndices( FixedImageSampleContainer & samples);
00279 
00281   void ComputePDFDerivatives(ThreadIdType threadID, unsigned int sampleNumber, int movingImageParzenWindowIndex,
00282                                      const ImageDerivativesType
00283                                      &  movingImageGradientValue,
00284                                      double cubicBSplineDerivativeValue) const;
00285 
00286   void GetValueThreadPreProcess(ThreadIdType threadID, bool withinSampleThread) const;
00287 
00288   bool GetValueThreadProcessSample(ThreadIdType threadID, SizeValueType fixedImageSample,
00289                                                   const MovingImagePointType & mappedPoint,
00290                                                   double movingImageValue) const;
00291 
00292   void GetValueThreadPostProcess(ThreadIdType threadID, bool withinSampleThread) const;
00293 
00294   void GetValueAndDerivativeThreadPreProcess( ThreadIdType threadID, bool withinSampleThread) const;
00295 
00296   bool GetValueAndDerivativeThreadProcessSample(ThreadIdType threadID, SizeValueType fixedImageSample,
00297                                                                const MovingImagePointType & mappedPoint,
00298                                                                double movingImageValue, const ImageDerivativesType & movingImageGradientValue) const;
00299 
00300   void GetValueAndDerivativeThreadPostProcess( ThreadIdType threadID, bool withinSampleThread) const;
00301 
00303   SizeValueType m_NumberOfHistogramBins;
00304   double        m_MovingImageNormalizedMin;
00305   double        m_FixedImageNormalizedMin;
00306   double        m_FixedImageTrueMin;
00307   double        m_FixedImageTrueMax;
00308   double        m_MovingImageTrueMin;
00309   double        m_MovingImageTrueMax;
00310   double        m_FixedImageBinSize;
00311   double        m_MovingImageBinSize;
00312 
00314   typename CubicBSplineFunctionType::Pointer           m_CubicBSplineKernel;
00315   typename CubicBSplineDerivativeFunctionType::Pointer m_CubicBSplineDerivativeKernel;
00316 
00318   typedef double              PRatioType;
00319   typedef Array2D<PRatioType> PRatioArrayType;
00320 
00321   mutable PRatioArrayType m_PRatioArray;
00322 
00324   mutable std::vector<DerivativeType> m_ThreaderMetricDerivative;
00325 
00327   mutable std::vector<PDFValueType>               m_MovingImageMarginalPDF;
00328   mutable std::vector<std::vector<PDFValueType> > m_ThreaderFixedImageMarginalPDF;
00329 
00331   typename std::vector<JointPDFType::Pointer>            m_ThreaderJointPDF;
00332   typename std::vector<JointPDFDerivativesType::Pointer> m_ThreaderJointPDFDerivatives;
00333 
00334   std::vector<int> m_ThreaderJointPDFStartBin;
00335   std::vector<int> m_ThreaderJointPDFEndBin;
00336 
00337   mutable std::vector<double> m_ThreaderJointPDFSum;
00338 
00339   bool         m_UseExplicitPDFDerivatives;
00340   mutable bool m_ImplicitDerivativesSecondPass;
00341 };
00342 } // end namespace itk
00343 
00344 #ifndef ITK_MANUAL_INSTANTIATION
00345 #include "itkMattesMutualInformationImageToImageMetric.hxx"
00346 #endif
00347 
00348 #endif
00349