ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkMattesMutualInformationImageToImageMetric.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 itkMattesMutualInformationImageToImageMetric_h
19 #define itkMattesMutualInformationImageToImageMetric_h
20 
21 #include "itkImageToImageMetric.h"
22 #include "itkPoint.h"
23 #include "itkIndex.h"
25 #include "itkArray2D.h"
26 
27 #include <mutex>
28 
29 
30 namespace itk
31 {
111 template <typename TFixedImage, typename TMovingImage>
113  public ImageToImageMetric<TFixedImage, TMovingImage>
114 {
115 public:
116  ITK_DISALLOW_COPY_AND_ASSIGN(MattesMutualInformationImageToImageMetric);
117 
123 
125  itkNewMacro(Self);
126 
130 
132  using TransformType = typename Superclass::TransformType;
133  using TransformPointer = typename Superclass::TransformPointer;
134  using TransformJacobianType = typename Superclass::TransformJacobianType;
135  using InterpolatorType = typename Superclass::InterpolatorType;
136  using MeasureType = typename Superclass::MeasureType;
137  using DerivativeType = typename Superclass::DerivativeType;
138  using ParametersType = typename Superclass::ParametersType;
139  using FixedImageType = typename Superclass::FixedImageType;
140  using MovingImageType = typename Superclass::MovingImageType;
141  using MovingImagePointType = typename Superclass::MovingImagePointType;
142  using FixedImageConstPointer = typename Superclass::FixedImageConstPointer;
143  using MovingImageConstPointer = typename Superclass::MovingImageConstPointer;
144  using BSplineTransformWeightsType = typename Superclass::BSplineTransformWeightsType;
145  using BSplineTransformIndexArrayType = typename Superclass::BSplineTransformIndexArrayType;
146 
147  using CoordinateRepresentationType = typename Superclass::CoordinateRepresentationType;
148  using FixedImageSampleContainer = typename Superclass::FixedImageSampleContainer;
149  using ImageDerivativesType = typename Superclass::ImageDerivativesType;
150  using WeightsValueType = typename Superclass::WeightsValueType;
152 
154 
156  static constexpr unsigned int MovingImageDimension = MovingImageType::ImageDimension;
157 
165  void Initialize() override;
166 
168  MeasureType GetValue(const ParametersType & parameters) const override;
169 
171  void GetDerivative(const ParametersType & parameters, DerivativeType & Derivative) const override;
172 
174  void GetValueAndDerivative(const ParametersType & parameters, MeasureType & Value, DerivativeType & Derivative) const override;
175 
182  itkSetClampMacro( NumberOfHistogramBins, SizeValueType,
184  itkGetConstReferenceMacro(NumberOfHistogramBins, SizeValueType);
186 
211  itkSetMacro(UseExplicitPDFDerivatives, bool);
212  itkGetConstReferenceMacro(UseExplicitPDFDerivatives, bool);
213  itkBooleanMacro(UseExplicitPDFDerivatives);
215 
217  using PDFValueType = double; //NOTE: floating point precision is not as stable. Double precision proves faster and more robust in real-world testing.
218 
222 
227  const typename JointPDFType::Pointer GetJointPDF () const
228  {
229  if( this->m_MMIMetricPerThreadVariables == nullptr )
230  {
231  return JointPDFType::Pointer(nullptr);
232  }
233  return this->m_MMIMetricPerThreadVariables[0].JointPDF;
234  }
236 
244  {
245  if( this->m_MMIMetricPerThreadVariables == nullptr )
246  {
247  return JointPDFDerivativesType::Pointer(nullptr);
248  }
249  return this->m_MMIMetricPerThreadVariables[0].JointPDFDerivatives;
250  }
252 
253 protected:
254 
257  void PrintSelf(std::ostream & os, Indent indent) const override;
258 
259 private:
268 
272 
274  void ComputeFixedImageParzenWindowIndices( FixedImageSampleContainer & samples);
275 
277  void ComputePDFDerivatives(ThreadIdType threadId, unsigned int sampleNumber, int movingImageParzenWindowIndex,
279  & movingImageGradientValue,
280  PDFValueType cubicBSplineDerivativeValue) const;
281 
282  void GetValueThreadPreProcess(ThreadIdType threadId, bool withinSampleThread) const override;
283  void GetValueThreadPostProcess(ThreadIdType threadId, bool withinSampleThread) const override;
284  //NOTE: The signature in base class requires that movingImageValue is of type double
285  bool GetValueThreadProcessSample(ThreadIdType threadId, SizeValueType fixedImageSample,
286  const MovingImagePointType & mappedPoint,
287  double movingImageValue) const override;
288 
289  void GetValueAndDerivativeThreadPreProcess( ThreadIdType threadId, bool withinSampleThread) const override;
290  void GetValueAndDerivativeThreadPostProcess( ThreadIdType threadId, bool withinSampleThread) const override;
291  //NOTE: The signature in base class requires that movingImageValue is of type double
292  bool GetValueAndDerivativeThreadProcessSample(ThreadIdType threadId, SizeValueType fixedImageSample,
293  const MovingImagePointType & mappedPoint,
294  double movingImageValue, const ImageDerivativesType &
295  movingImageGradientValue) const override;
296 
298  SizeValueType m_NumberOfHistogramBins{50};
299  PDFValueType m_MovingImageNormalizedMin{0.0};
300  PDFValueType m_FixedImageNormalizedMin{0.0};
301  PDFValueType m_FixedImageTrueMin{0.0};
302  PDFValueType m_FixedImageTrueMax{0.0};
303  PDFValueType m_MovingImageTrueMin{0.0};
304  PDFValueType m_MovingImageTrueMax{0.0};
305  PDFValueType m_FixedImageBinSize{0.0};
306  PDFValueType m_MovingImageBinSize{0.0};
307 
311 
315 
317 
319  using MarginalPDFType = std::vector< PDFValueType >;
321 
323  {
326 
328 
331 
335 
337 
339  };
340 
341 #if !defined(ITK_WRAPPING_PARSER)
342  itkPadStruct( ITK_CACHE_LINE_ALIGNMENT, MMIMetricPerThreadStruct,
343  PaddedMMIMetricPerThreadStruct);
344  itkAlignedTypedef( ITK_CACHE_LINE_ALIGNMENT, PaddedMMIMetricPerThreadStruct,
345  AlignedMMIMetricPerThreadStruct );
346  // Due to a bug in older version of Visual Studio where std::vector resize
347  // uses a value instead of a const reference, this must be a pointer.
348  // See
349  // http://thetweaker.wordpress.com/2010/05/05/stdvector-of-aligned-elements/
350  // http://connect.microsoft.com/VisualStudio/feedback/details/692988
351  mutable AlignedMMIMetricPerThreadStruct * m_MMIMetricPerThreadVariables;
352 #endif
353 
354  bool m_UseExplicitPDFDerivatives{true};
355  mutable bool m_ImplicitDerivativesSecondPass{false};
356 };
357 } // end namespace itk
358 
359 #ifndef ITK_MANUAL_INSTANTIATION
360 #include "itkMattesMutualInformationImageToImageMetric.hxx"
361 #endif
362 
363 #endif
Array class with size defined at construction time.
Definition: itkArray.h:46
typename BSplineTransformWeightsType::ValueType WeightsValueType
TPixel PixelType
Definition: itkImage.h:95
Light weight base class for most itk classes.
const JointPDFDerivativesType::Pointer GetJointPDFDerivatives() const
Define numeric traits for std::vector.
typename BSplineTransformType::WeightsType BSplineTransformWeightsType
typename TransformType::JacobianType TransformJacobianType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
CubicBSplineDerivativeFunctionType::Pointer m_CubicBSplineDerivativeKernel
Derivative of a BSpline kernel used for density estimation and nonparameteric regression.
BSpline kernel used for density estimation and nonparameteric regression.
Transform points and vectors from an input space to an output space.
Definition: itkTransform.h:83
signed long IndexValueType
Definition: itkIntTypes.h:90
typename Superclass::ParametersValueType CoordinateRepresentationType
std::vector< FixedImageSamplePoint > FixedImageSampleContainer
typename FixedImageType::ConstPointer FixedImageConstPointer
unsigned int ThreadIdType
Definition: itkIntTypes.h:99
Base class for all image interpolaters.
typename TransformType::Pointer TransformPointer
typename BSplineTransformType::ParameterIndexArrayType BSplineTransformIndexArrayType
Computes the mutual information between two images to be registered using the method of Mattes et al...
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Computes similarity between regions of two images.
A templated class holding a n-Dimensional covariant vector.
signed long OffsetValueType
Definition: itkIntTypes.h:94
typename MovingImageType::ConstPointer MovingImageConstPointer
typename BSplineTransformIndexArrayType::ValueType IndexValueType
Templated n-dimensional image class.
Definition: itkImage.h:75
typename TransformType::OutputPointType MovingImagePointType