ITK  5.3.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 
262  using PRatioArrayType = std::vector<PRatioType>;
263 
265 
268  mutable std::vector<OffsetValueType> m_JointPdfIndex1DArray;
269 
271  mutable std::vector<PDFValueType> m_MovingImageMarginalPDF;
272  mutable std::vector<std::vector<PDFValueType>> m_ThreaderFixedImageMarginalPDF;
273 
275  typename std::vector<typename JointPDFType::Pointer> m_ThreaderJointPDF;
276 
277  /* \class DerivativeBufferManager
278  * A helper class to manage complexities of minimizing memory
279  * needs for mattes mutual information derivative computations
280  * per thread.
281  *
282  * Thread safety note:
283  * A separate object is used locally per each thread. Only the members
284  * m_ParentJointPDFDerivativesLockPtr and m_ParentJointPDFDerivatives
285  * are shared between threads and access to m_ParentJointPDFDerivatives
286  * is controlled with the m_ParentJointPDFDerivativesLockPtr mutex lock.
287  * \ingroup ITKMetricsv4
288  */
290  {
292 
293  public:
294  /* All these methods are thread safe except ReduceBuffer */
295 
296  void
297  Initialize(size_t maxBufferLength,
298  const size_t cachedNumberOfLocalParameters,
299  std::mutex * parentDerivativeLockPtr,
300  typename JointPDFDerivativesType::Pointer parentJointPDFDerivatives);
301 
302  void
303  DoubleBufferSize();
304 
306  : m_MemoryBlock(0)
307  {}
308 
309  ~DerivativeBufferManager() = default;
310 
311  size_t
313  {
314  return this->m_CachedNumberOfLocalParameters;
315  }
316 
321  void
322  CheckAndReduceIfNecessary();
323 
327  void
328  BlockAndReduce();
329 
330  // If offset is same as previous offset, then accumulate with previous
331  PDFValueType *
333  {
334  m_BufferOffsetContainer[m_CurrentFillSize] = offset;
335  PDFValueType * PDFBufferForWriting = m_BufferPDFValuesContainer[m_CurrentFillSize];
336  ++m_CurrentFillSize;
337  return PDFBufferForWriting;
338  }
339 
344  void
345  ReduceBuffer();
346 
347  private:
348  // How many AccumlatorElements used
349  size_t m_CurrentFillSize{ 0 };
350  // Continguous chunk of memory for efficiency
351  std::vector<PDFValueType> m_MemoryBlock;
352  // The (number of lines in the buffer) * (cells per line)
354  std::vector<PDFValueType *> m_BufferPDFValuesContainer;
355  std::vector<OffsetValueType> m_BufferOffsetContainer;
358  // Pointer handle to parent version
360  // Smart pointer handle to parent version
362  };
363 
364  std::vector<DerivativeBufferManager> m_ThreaderDerivativeManager;
367 
369 
372  mutable std::vector<DerivativeType> m_LocalDerivativeByParzenBin;
373 
374 private:
376  virtual void
377  ComputeResults() const;
378 };
379 
380 } // end namespace itk
381 
382 #ifndef ITK_MANUAL_INSTANTIATION
383 # include "itkMattesMutualInformationImageToImageMetricv4.hxx"
384 #endif
385 
386 #endif
itk::MattesMutualInformationImageToImageMetricv4::m_JointPdfIndex1DArray
std::vector< OffsetValueType > m_JointPdfIndex1DArray
Definition: itkMattesMutualInformationImageToImageMetricv4.h:268
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::m_ParentJointPDFDerivatives
JointPDFDerivativesType::Pointer m_ParentJointPDFDerivatives
Definition: itkMattesMutualInformationImageToImageMetricv4.h:361
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:357
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:332
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:356
itk::MattesMutualInformationImageToImageMetricv4::m_MovingImageMarginalPDF
std::vector< PDFValueType > m_MovingImageMarginalPDF
Definition: itkMattesMutualInformationImageToImageMetricv4.h:271
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:355
itk::MattesMutualInformationImageToImageMetricv4::PRatioType
PDFValueType PRatioType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:261
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:372
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:359
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::m_MemoryBlockSize
vcl_size_t m_MemoryBlockSize
Definition: itkMattesMutualInformationImageToImageMetricv4.h:353
itk::MattesMutualInformationImageToImageMetricv4::m_FixedImageNormalizedMin
PDFValueType m_FixedImageNormalizedMin
Definition: itkMattesMutualInformationImageToImageMetricv4.h:252
itk::Image::RegionType
typename Superclass::RegionType RegionType
Definition: itkImage.h:152
itkImageToImageMetricv4.h
itk::MattesMutualInformationImageToImageMetricv4::m_ThreaderFixedImageMarginalPDF
std::vector< std::vector< PDFValueType > > m_ThreaderFixedImageMarginalPDF
Definition: itkMattesMutualInformationImageToImageMetricv4.h:272
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::GetCachedNumberOfLocalParameters
vcl_size_t GetCachedNumberOfLocalParameters() const
Definition: itkMattesMutualInformationImageToImageMetricv4.h:312
itk::MattesMutualInformationImageToImageMetricv4::m_ThreaderDerivativeManager
std::vector< DerivativeBufferManager > m_ThreaderDerivativeManager
Definition: itkMattesMutualInformationImageToImageMetricv4.h:364
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:289
itk::MattesMutualInformationImageToImageMetricv4::m_ThreaderJointPDF
std::vector< typename JointPDFType::Pointer > m_ThreaderJointPDF
Definition: itkMattesMutualInformationImageToImageMetricv4.h:275
itk::MattesMutualInformationImageToImageMetricv4::DerivativeBufferManager::DerivativeBufferManager
DerivativeBufferManager()
Definition: itkMattesMutualInformationImageToImageMetricv4.h:305
itkIndex.h
itk::Image::PixelType
TPixel PixelType
Definition: itkImage.h:108
itk::MattesMutualInformationImageToImageMetricv4::m_JointPDFDerivativesLock
std::mutex m_JointPDFDerivativesLock
Definition: itkMattesMutualInformationImageToImageMetricv4.h:365
itk::MattesMutualInformationImageToImageMetricv4::m_JointPDFDerivatives
JointPDFDerivativesType::Pointer m_JointPDFDerivatives
Definition: itkMattesMutualInformationImageToImageMetricv4.h:366
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:264
itkArray2D.h
itk::MattesMutualInformationImageToImageMetricv4::JointPDFDerivativesIndexType
typename JointPDFDerivativesType::IndexType JointPDFDerivativesIndexType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:232
itk::Image::IndexType
typename Superclass::IndexType IndexType
Definition: itkImage.h:134
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:88
itk::MattesMutualInformationImageToImageMetricv4::m_JointPDFSum
PDFValueType m_JointPDFSum
Definition: itkMattesMutualInformationImageToImageMetricv4.h:368
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:354
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:351
itk::Image::SizeType
typename Superclass::SizeType SizeType
Definition: itkImage.h:141
itk::MattesMutualInformationImageToImageMetricv4::PRatioArrayType
std::vector< PRatioType > PRatioArrayType
Definition: itkMattesMutualInformationImageToImageMetricv4.h:262
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