ITK  5.2.0
Insight Toolkit
itkMattesMutualInformationImageToImageMetricv4.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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 itkMattesMutualInformationImageToImageMetricv4_h
19 #define itkMattesMutualInformationImageToImageMetricv4_h
20 
23 #include "itkPoint.h"
24 #include "itkIndex.h"
26 #include "itkArray2D.h"
28 #include <mutex>
29 
30 namespace itk
31 {
32 
97 template <typename TFixedImage,
98  typename TMovingImage,
99  typename TVirtualImage = TFixedImage,
100  typename TInternalComputationValueType = double,
101  typename TMetricTraits =
102  DefaultImageToImageMetricTraitsv4<TFixedImage, TMovingImage, TVirtualImage, TInternalComputationValueType>>
104  : public ImageToImageMetricv4<TFixedImage, TMovingImage, TVirtualImage, TInternalComputationValueType, TMetricTraits>
105 {
106 public:
107  ITK_DISALLOW_COPY_AND_MOVE(MattesMutualInformationImageToImageMetricv4);
108 
111  using Superclass =
115 
117  itkNewMacro(Self);
118 
121 
123  using MeasureType = typename Superclass::MeasureType;
124  using DerivativeType = typename Superclass::DerivativeType;
125  using DerivativeValueType = typename DerivativeType::ValueType;
126 
127  using FixedImageType = typename Superclass::FixedImageType;
128  using FixedImagePointType = typename Superclass::FixedImagePointType;
129  using FixedImageIndexType = typename Superclass::FixedImageIndexType;
130  using FixedImagePixelType = typename Superclass::FixedImagePixelType;
131  using FixedImageGradientType = typename Superclass::FixedImageGradientType;
132 
133  using MovingImagePointType = typename Superclass::MovingImagePointType;
134  using MovingImagePixelType = typename Superclass::MovingImagePixelType;
135  using MovingImageGradientType = typename Superclass::MovingImageGradientType;
136 
137  using MovingTransformType = typename Superclass::MovingTransformType;
138  using JacobianType = typename Superclass::JacobianType;
139  using VirtualImageType = typename Superclass::VirtualImageType;
140  using VirtualIndexType = typename Superclass::VirtualIndexType;
141  using VirtualPointType = typename Superclass::VirtualPointType;
142  using VirtualPointSetType = typename Superclass::VirtualPointSetType;
143 
145  using FixedSampledPointSetPointer = typename Superclass::FixedSampledPointSetPointer;
146 
147  /* Image dimension accessors */
148  static constexpr typename TVirtualImage::ImageDimensionType VirtualImageDimension = TVirtualImage::ImageDimension;
149  static constexpr typename TFixedImage::ImageDimensionType FixedImageDimension = TFixedImage::ImageDimension;
150  static constexpr typename TMovingImage::ImageDimensionType MovingImageDimension = TMovingImage::ImageDimension;
151 
157  itkSetClampMacro(NumberOfHistogramBins, SizeValueType, 5, NumericTraits<SizeValueType>::max());
158  itkGetConstReferenceMacro(NumberOfHistogramBins, SizeValueType);
160 
161  void
162  Initialize() override;
163 
165  // NOTE: floating point precision is not as stable.
166  // Double precision proves faster and more robust in real-world testing.
167  using PDFValueType = TInternalComputationValueType;
168 
172 
177  const typename JointPDFType::Pointer
178  GetJointPDF() const
179  {
180  if (this->m_ThreaderJointPDF.empty())
181  {
182  return typename JointPDFType::Pointer(nullptr);
183  }
184  return this->m_ThreaderJointPDF[0];
185  }
187 
194  const typename JointPDFDerivativesType::Pointer
196  {
197  return this->m_JointPDFDerivatives;
198  }
199 
200  void
201  FinalizeThread(const ThreadIdType threadId) override;
202 
203 protected:
205  ~MattesMutualInformationImageToImageMetricv4() override = default;
206 
208  ThreadedImageRegionPartitioner<Superclass::VirtualImageDimension>,
209  Superclass,
212  ThreadedIndexedContainerPartitioner,
213  Superclass,
214  Self>;
217  ThreadedImageRegionPartitioner<Superclass::VirtualImageDimension>,
218  Superclass,
219  Self>;
222  Superclass,
223  Self>;
224 
225  void
226  PrintSelf(std::ostream & os, Indent indent) const override;
227 
236 
240 
243  virtual void
244  GetValueCommonAfterThreadedExecution();
245 
247  ComputeSingleFixedImageParzenWindowIndex(const FixedImagePixelType & value) const;
248 
250  SizeValueType m_NumberOfHistogramBins{ 50 };
259 
263 
266  using PRatioArrayType = std::vector<PRatioType>;
267 
269 
272  mutable std::vector<OffsetValueType> m_JointPdfIndex1DArray;
273 
275  mutable std::vector<PDFValueType> m_MovingImageMarginalPDF;
276  mutable std::vector<std::vector<PDFValueType>> m_ThreaderFixedImageMarginalPDF;
277 
279  typename std::vector<typename JointPDFType::Pointer> m_ThreaderJointPDF;
280 
281  /* \class DerivativeBufferManager
282  * A helper class to manage complexities of minimizing memory
283  * needs for mattes mutual information derivative computations
284  * per thread.
285  *
286  * Thread safety note:
287  * A separate object is used locally per each thread. Only the members
288  * m_ParentJointPDFDerivativesLockPtr and m_ParentJointPDFDerivatives
289  * are shared between threads and access to m_ParentJointPDFDerivatives
290  * is controlled with the m_ParentJointPDFDerivativesLockPtr mutex lock.
291  * \ingroup ITKMetricsv4
292  */
294  {
296 
297  public:
298  /* All these methods are thread safe except ReduceBuffer */
299 
300  void
301  Initialize(size_t maxBufferLength,
302  const size_t cachedNumberOfLocalParameters,
303  std::mutex * parentDerivativeLockPtr,
304  typename JointPDFDerivativesType::Pointer parentJointPDFDerivatives);
305 
306  void
307  DoubleBufferSize();
308 
310  : m_MemoryBlock(0)
311  {}
312 
313  ~DerivativeBufferManager() = default;
314 
315  size_t
317  {
318  return this->m_CachedNumberOfLocalParameters;
319  }
320 
325  void
326  CheckAndReduceIfNecessary();
327 
331  void
332  BlockAndReduce();
333 
334  // If offset is same as previous offset, then accumulate with previous
335  PDFValueType *
337  {
338  m_BufferOffsetContainer[m_CurrentFillSize] = offset;
339  PDFValueType * PDFBufferForWriting = m_BufferPDFValuesContainer[m_CurrentFillSize];
340  ++m_CurrentFillSize;
341  return PDFBufferForWriting;
342  }
343 
348  void
349  ReduceBuffer();
350 
351  private:
352  // How many AccumlatorElements used
353  size_t m_CurrentFillSize{ 0 };
354  // Continguous chunk of memory for efficiency
355  std::vector<PDFValueType> m_MemoryBlock;
356  // The (number of lines in the buffer) * (cells per line)
358  std::vector<PDFValueType *> m_BufferPDFValuesContainer;
359  std::vector<OffsetValueType> m_BufferOffsetContainer;
362  // Pointer handle to parent version
364  // Smart pointer handle to parent version
366  };
367 
368  std::vector<DerivativeBufferManager> m_ThreaderDerivativeManager;
371 
373 
376  mutable std::vector<DerivativeType> m_LocalDerivativeByParzenBin;
377 
378 private:
380  virtual void
381  ComputeResults() const;
382 };
383 
384 } // end namespace itk
385 
386 #ifndef ITK_MANUAL_INSTANTIATION
387 # include "itkMattesMutualInformationImageToImageMetricv4.hxx"
388 #endif
389 
390 #endif
itk::MattesMutualInformationImageToImageMetricv4::m_JointPdfIndex1DArray
std::vector< OffsetValueType > m_JointPdfIndex1DArray
Definition: itkMattesMutualInformationImageToImageMetricv4.h:272
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::m_ParentJointPDFDerivatives
JointPDFDerivativesType::Pointer m_ParentJointPDFDerivatives
Definition: itkMattesMutualInformationImageToImageMetricv4.h:365
itk::BSplineKernelFunction
BSpline kernel used for density estimation and nonparametric regression.
Definition: itkBSplineKernelFunction.h:43
itk::MattesMutualInformationImageToImageMetricv4::JointPDFValueType
typename JointPDFType::PixelType JointPDFValueType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:229
itk::ImageToImageMetricv4::FixedImageGradientType
typename MetricTraits::FixedImageGradientType FixedImageGradientType
Definition: itkImageToImageMetricv4.h:297
itkThreadedIndexedContainerPartitioner.h
itk::MattesMutualInformationImageToImageMetricv4::JointPDFIndexType
typename JointPDFType::IndexType JointPDFIndexType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:228
itk::ImageToImageMetricv4::FixedImageType
TFixedImage FixedImageType
Definition: itkImageToImageMetricv4.h:234
itk::ThreadedImageRegionPartitioner
Class for partitioning of an ImageRegion.
Definition: itkThreadedImageRegionPartitioner.h:45
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::m_MaxBufferSize
vcl_size_t m_MaxBufferSize
Definition: itkMattesMutualInformationImageToImageMetricv4.h:361
itkPoint.h
itk::MattesMutualInformationImageToImageMetricv4::m_FixedImageTrueMin
PDFValueType m_FixedImageTrueMin
Definition: itkMattesMutualInformationImageToImageMetricv4.h:253
itk::ImageToImageMetricv4::FixedSampledPointSetPointer
typename FixedSampledPointSetType::Pointer FixedSampledPointSetPointer
Definition: itkImageToImageMetricv4.h:287
itk::MattesMutualInformationImageToImageMetricv4::JointPDFDerivativesSizeType
typename JointPDFDerivativesType::SizeType JointPDFDerivativesSizeType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:235
itk::ImageToImageMetricv4::FixedImageIndexType
typename FixedImageType::IndexType FixedImageIndexType
Definition: itkImageToImageMetricv4.h:240
itk::SmartPointer< Self >
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::GetNextElementAndAddOffset
PDFValueType * GetNextElementAndAddOffset(const OffsetValueType &offset)
Definition: itkMattesMutualInformationImageToImageMetricv4.h:336
itk::MattesMutualInformationImageToImageMetricv4::m_MovingImageTrueMax
PDFValueType m_MovingImageTrueMax
Definition: itkMattesMutualInformationImageToImageMetricv4.h:256
itk::ImageToImageMetricv4::JacobianType
typename Superclass::JacobianType JacobianType
Definition: itkImageToImageMetricv4.h:227
itk::MattesMutualInformationImageToImageMetricv4GetValueAndDerivativeThreader
Processes points for MattesMutualInformationImageToImageMetricv4 GetValueAndDerivative.
Definition: itkMattesMutualInformationImageToImageMetricv4GetValueAndDerivativeThreader.h:35
itk::ImageToImageMetricv4::MovingImagePointType
typename MovingImageType::PointType MovingImagePointType
Definition: itkImageToImageMetricv4.h:246
itk::ObjectToObjectMetricBaseTemplate::DerivativeValueType
typename DerivativeType::ValueType DerivativeValueType
Definition: itkObjectToObjectMetricBase.h:113
itk::MattesMutualInformationImageToImageMetricv4::JointPDFSizeType
typename JointPDFType::SizeType JointPDFSizeType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:231
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::m_CachedNumberOfLocalParameters
vcl_size_t m_CachedNumberOfLocalParameters
Definition: itkMattesMutualInformationImageToImageMetricv4.h:360
itk::MattesMutualInformationImageToImageMetricv4::m_MovingImageMarginalPDF
std::vector< PDFValueType > m_MovingImageMarginalPDF
Definition: itkMattesMutualInformationImageToImageMetricv4.h:275
itk::ImageToImageMetricv4::FixedImagePointType
typename FixedImageType::PointType FixedImagePointType
Definition: itkImageToImageMetricv4.h:239
itk::MattesMutualInformationImageToImageMetricv4::GetJointPDF
const JointPDFType::Pointer GetJointPDF() const
Definition: itkMattesMutualInformationImageToImageMetricv4.h:178
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::m_BufferOffsetContainer
std::vector< OffsetValueType > m_BufferOffsetContainer
Definition: itkMattesMutualInformationImageToImageMetricv4.h:359
itk::MattesMutualInformationImageToImageMetricv4::PRatioType
PDFValueType PRatioType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:265
itk::ThreadIdType
unsigned int ThreadIdType
Definition: itkIntTypes.h:99
itk::MattesMutualInformationImageToImageMetricv4::m_FixedImageBinSize
PDFValueType m_FixedImageBinSize
Definition: itkMattesMutualInformationImageToImageMetricv4.h:257
itk::MattesMutualInformationImageToImageMetricv4::PDFValueType
TInternalComputationValueType PDFValueType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:167
itk::MattesMutualInformationImageToImageMetricv4::m_LocalDerivativeByParzenBin
std::vector< DerivativeType > m_LocalDerivativeByParzenBin
Definition: itkMattesMutualInformationImageToImageMetricv4.h:376
itk::MattesMutualInformationImageToImageMetricv4::JointPDFDerivativesValueType
typename JointPDFDerivativesType::PixelType JointPDFDerivativesValueType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:233
itk::MattesMutualInformationImageToImageMetricv4::JointPDFRegionType
typename JointPDFType::RegionType JointPDFRegionType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:230
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::MattesMutualInformationImageToImageMetricv4::m_MovingImageNormalizedMin
PDFValueType m_MovingImageNormalizedMin
Definition: itkMattesMutualInformationImageToImageMetricv4.h:251
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::m_ParentJointPDFDerivativesLockPtr
std::mutex * m_ParentJointPDFDerivativesLockPtr
Definition: itkMattesMutualInformationImageToImageMetricv4.h:363
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::m_MemoryBlockSize
vcl_size_t m_MemoryBlockSize
Definition: itkMattesMutualInformationImageToImageMetricv4.h:357
itk::MattesMutualInformationImageToImageMetricv4::m_FixedImageNormalizedMin
PDFValueType m_FixedImageNormalizedMin
Definition: itkMattesMutualInformationImageToImageMetricv4.h:252
itk::Image::RegionType
typename Superclass::RegionType RegionType
Definition: itkImage.h:150
itk::MattesMutualInformationImageToImageMetricv4::m_CubicBSplineKernel
CubicBSplineFunctionType::Pointer m_CubicBSplineKernel
Definition: itkMattesMutualInformationImageToImageMetricv4.h:261
itkImageToImageMetricv4.h
itk::MattesMutualInformationImageToImageMetricv4::m_ThreaderFixedImageMarginalPDF
std::vector< std::vector< PDFValueType > > m_ThreaderFixedImageMarginalPDF
Definition: itkMattesMutualInformationImageToImageMetricv4.h:276
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::GetCachedNumberOfLocalParameters
vcl_size_t GetCachedNumberOfLocalParameters() const
Definition: itkMattesMutualInformationImageToImageMetricv4.h:316
itk::MattesMutualInformationImageToImageMetricv4::m_ThreaderDerivativeManager
std::vector< DerivativeBufferManager > m_ThreaderDerivativeManager
Definition: itkMattesMutualInformationImageToImageMetricv4.h:368
itk::ImageToImageMetricv4::VirtualIndexType
typename Superclass::VirtualIndexType VirtualIndexType
Definition: itkImageToImageMetricv4.h:261
itk::MattesMutualInformationImageToImageMetricv4::m_FixedImageTrueMax
PDFValueType m_FixedImageTrueMax
Definition: itkMattesMutualInformationImageToImageMetricv4.h:254
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager
Definition: itkMattesMutualInformationImageToImageMetricv4.h:293
itk::MattesMutualInformationImageToImageMetricv4::m_ThreaderJointPDF
std::vector< typename JointPDFType::Pointer > m_ThreaderJointPDF
Definition: itkMattesMutualInformationImageToImageMetricv4.h:279
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::DerivativeBufferManager
DerivativeBufferManager()
Definition: itkMattesMutualInformationImageToImageMetricv4.h:309
itk::MattesMutualInformationImageToImageMetricv4::m_CubicBSplineDerivativeKernel
CubicBSplineDerivativeFunctionType::Pointer m_CubicBSplineDerivativeKernel
Definition: itkMattesMutualInformationImageToImageMetricv4.h:262
itkIndex.h
itk::Image::PixelType
TPixel PixelType
Definition: itkImage.h:106
itk::MattesMutualInformationImageToImageMetricv4::m_JointPDFDerivativesLock
std::mutex m_JointPDFDerivativesLock
Definition: itkMattesMutualInformationImageToImageMetricv4.h:369
itk::MattesMutualInformationImageToImageMetricv4::m_JointPDFDerivatives
JointPDFDerivativesType::Pointer m_JointPDFDerivatives
Definition: itkMattesMutualInformationImageToImageMetricv4.h:370
itk::NumericTraits
Define additional traits for native types such as int or float.
Definition: itkNumericTraits.h:58
itk::MattesMutualInformationImageToImageMetricv4::GetJointPDFDerivatives
const JointPDFDerivativesType::Pointer GetJointPDFDerivatives() const
Definition: itkMattesMutualInformationImageToImageMetricv4.h:195
itkMattesMutualInformationImageToImageMetricv4GetValueAndDerivativeThreader.h
itk::MattesMutualInformationImageToImageMetricv4::JointPDFDerivativesRegionType
typename JointPDFDerivativesType::RegionType JointPDFDerivativesRegionType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:234
itk::ObjectToObjectMetricBaseTemplate::MeasureType
typename Superclass::MeasureType MeasureType
Definition: itkObjectToObjectMetricBase.h:109
itk::MattesMutualInformationImageToImageMetricv4::m_PRatioArray
PRatioArrayType m_PRatioArray
Definition: itkMattesMutualInformationImageToImageMetricv4.h:268
itkArray2D.h
itk::MattesMutualInformationImageToImageMetricv4::JointPDFDerivativesIndexType
typename JointPDFDerivativesType::IndexType JointPDFDerivativesIndexType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:232
itk::Image::IndexType
typename Superclass::IndexType IndexType
Definition: itkImage.h:132
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ImageToImageMetricv4::MovingImageGradientType
typename MetricTraits::MovingImageGradientType MovingImageGradientType
Definition: itkImageToImageMetricv4.h:298
itk::MattesMutualInformationImageToImageMetricv4::m_MovingImageTrueMin
PDFValueType m_MovingImageTrueMin
Definition: itkMattesMutualInformationImageToImageMetricv4.h:255
itk::BSplineDerivativeKernelFunction
Derivative of a BSpline kernel used for density estimation and nonparametric regression.
Definition: itkBSplineDerivativeKernelFunction.h:43
itk::OffsetValueType
signed long OffsetValueType
Definition: itkIntTypes.h:94
itk::ImageToImageMetricv4::VirtualPointType
typename Superclass::VirtualPointType VirtualPointType
Definition: itkImageToImageMetricv4.h:258
itk::ImageToImageMetricv4
Definition: itkImageToImageMetricv4.h:174
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:86
itk::MattesMutualInformationImageToImageMetricv4::m_JointPDFSum
PDFValueType m_JointPDFSum
Definition: itkMattesMutualInformationImageToImageMetricv4.h:372
itk::ImageToImageMetricv4::FixedImagePixelType
typename FixedImageType::PixelType FixedImagePixelType
Definition: itkImageToImageMetricv4.h:235
itk::ImageToImageMetricv4::MovingImagePixelType
typename MovingImageType::PixelType MovingImagePixelType
Definition: itkImageToImageMetricv4.h:242
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::m_BufferPDFValuesContainer
std::vector< PDFValueType * > m_BufferPDFValuesContainer
Definition: itkMattesMutualInformationImageToImageMetricv4.h:358
itk::ObjectToObjectMetricBaseTemplate::DerivativeType
typename Superclass::DerivativeType DerivativeType
Definition: itkObjectToObjectMetricBase.h:112
itk::ImageToImageMetricv4::MovingTransformType
typename Superclass::MovingTransformType MovingTransformType
Definition: itkImageToImageMetricv4.h:221
itk::ImageToImageMetricv4::VirtualPointSetType
typename Superclass::VirtualPointSetType VirtualPointSetType
Definition: itkImageToImageMetricv4.h:262
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::m_MemoryBlock
std::vector< PDFValueType > m_MemoryBlock
Definition: itkMattesMutualInformationImageToImageMetricv4.h:355
itk::Image::SizeType
typename Superclass::SizeType SizeType
Definition: itkImage.h:139
itk::MattesMutualInformationImageToImageMetricv4::PRatioArrayType
std::vector< PRatioType > PRatioArrayType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:266
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
itk::ImageToImageMetricv4::VirtualImageType
typename Superclass::VirtualImageType VirtualImageType
Definition: itkImageToImageMetricv4.h:251
itkBSplineDerivativeKernelFunction.h
itk::MattesMutualInformationImageToImageMetricv4::m_MovingImageBinSize
PDFValueType m_MovingImageBinSize
Definition: itkMattesMutualInformationImageToImageMetricv4.h:258
itk::MattesMutualInformationImageToImageMetricv4
Computes the mutual information between two images to be registered using the method of Mattes et al.
Definition: itkMattesMutualInformationImageToImageMetricv4.h:103