ITK  4.9.0
Insight Segmentation and Registration Toolkit
itkMattesMutualInformationImageToImageMetricv4.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 itkMattesMutualInformationImageToImageMetricv4_h
19 #define itkMattesMutualInformationImageToImageMetricv4_h
20 
23 #include "itkPoint.h"
24 #include "itkIndex.h"
26 #include "itkArray2D.h"
28 #include "itkMutexLockHolder.h"
29 
30 namespace itk
31 {
32 
97 template <typename TFixedImage, typename TMovingImage, typename TVirtualImage = TFixedImage,
98  typename TInternalComputationValueType = double,
99  typename TMetricTraits = DefaultImageToImageMetricTraitsv4<TFixedImage,TMovingImage,TVirtualImage,TInternalComputationValueType>
100  >
102  public ImageToImageMetricv4<TFixedImage, TMovingImage, TVirtualImage, TInternalComputationValueType, TMetricTraits>
103 {
104 public:
107  typedef ImageToImageMetricv4<TFixedImage, TMovingImage, TVirtualImage,
108  TInternalComputationValueType,TMetricTraits> Superclass;
111 
113  itkNewMacro(Self);
114 
117 
119  typedef typename Superclass::MeasureType MeasureType;
121  typedef typename DerivativeType::ValueType DerivativeValueType;
122 
128 
132 
139 
142 
143  /* Image dimension accessors */
144  itkStaticConstMacro(VirtualImageDimension, typename TVirtualImage::ImageDimensionType, TVirtualImage::ImageDimension);
145  itkStaticConstMacro(FixedImageDimension, typename TFixedImage::ImageDimensionType, TFixedImage::ImageDimension);
146  itkStaticConstMacro(MovingImageDimension, typename TMovingImage::ImageDimensionType, TMovingImage::ImageDimension);
147 
153  itkSetClampMacro( NumberOfHistogramBins, SizeValueType, 5, NumericTraits<SizeValueType>::max() );
154  itkGetConstReferenceMacro(NumberOfHistogramBins, SizeValueType);
156 
157  virtual void Initialize(void) throw ( itk::ExceptionObject ) ITK_OVERRIDE;
158 
160  //NOTE: floating point precision is not as stable.
161  // Double precision proves faster and more robust in real-world testing.
162  typedef TInternalComputationValueType PDFValueType;
163 
165  typedef Image<PDFValueType, 2> JointPDFType;
167 
172  const typename JointPDFType::Pointer GetJointPDF () const
173  {
174  if( this->m_ThreaderJointPDF.size() == 0 )
175  {
176  return typename JointPDFType::Pointer(ITK_NULLPTR);
177  }
178  return this->m_ThreaderJointPDF[0];
179  }
181 
189  {
190  return this->m_JointPDFDerivatives;
191  }
192 
193  virtual void FinalizeThread( const ThreadIdType threadId ) ITK_OVERRIDE;
194 
195 protected:
198 
200  friend class MattesMutualInformationImageToImageMetricv4GetValueAndDerivativeThreader< ThreadedIndexedContainerPartitioner, Superclass, Self >;
201  typedef MattesMutualInformationImageToImageMetricv4GetValueAndDerivativeThreader< ThreadedImageRegionPartitioner< Superclass::VirtualImageDimension >, Superclass, Self >
203  typedef MattesMutualInformationImageToImageMetricv4GetValueAndDerivativeThreader< ThreadedIndexedContainerPartitioner, Superclass, Self >
205 
206  void PrintSelf(std::ostream& os, Indent indent) const ITK_OVERRIDE;
207 
216 
220 
224 
225  OffsetValueType ComputeSingleFixedImageParzenWindowIndex( const FixedImagePixelType & value ) const;
226 
237 
241 
244  typedef std::vector<PRatioType> PRatioArrayType;
245 
247 
250  mutable std::vector<OffsetValueType> m_JointPdfIndex1DArray;
251 
253  mutable std::vector<PDFValueType> m_MovingImageMarginalPDF;
254  mutable std::vector<std::vector<PDFValueType> > m_ThreaderFixedImageMarginalPDF;
255 
257  typename std::vector<typename JointPDFType::Pointer> m_ThreaderJointPDF;
258 
259  /* \class DerivativeBufferManager
260  * A helper class to manage complexities of minimizing memory
261  * needs for mattes mutual information derivative computations
262  * per thread.
263  *
264  * Thread safety note:
265  * A seperate object is used locally per each thread. Only the members
266  * m_ParentJointPDFDerivativesLockPtr and m_ParentJointPDFDerivatives
267  * are shared between threads and access to m_ParentJointPDFDerivatives
268  * is controlled with the m_ParentJointPDFDerivativesLockPtr mutex lock.
269  * \ingroup ITKMetricsv4
270  */
272  {
274 public:
275  /* All these methods are thread safe except ReduceBuffer */
276 
277  void Initialize( size_t maxBufferLength, const size_t cachedNumberOfLocalParameters,
278  SimpleFastMutexLock * parentDerivativeLockPtr,
279  typename JointPDFDerivativesType::Pointer parentJointPDFDerivatives);
280 
281  void DoubleBufferSize();
282 
284  m_CurrentFillSize(0),
285  m_MemoryBlock(0)
286  {
287  }
288 
290  {
291  }
292 
294  {
295  return this->m_CachedNumberOfLocalParameters;
296  }
297 
302  void CheckAndReduceIfNecessary();
303 
307  void BlockAndReduce();
308 
309  // If offset is same as previous offset, then accumulate with previous
311  {
312  m_BufferOffsetContainer[m_CurrentFillSize] = offset;
313  PDFValueType * PDFBufferForWriting = m_BufferPDFValuesContainer[m_CurrentFillSize];
314  ++m_CurrentFillSize;
315  return PDFBufferForWriting;
316  }
317 
322  void ReduceBuffer();
323 
324 private:
325  // How many AccumlatorElements used
327  // Continguous chunk of memory for efficiency
328  std::vector<PDFValueType> m_MemoryBlock;
329  // The (number of lines in the buffer) * (cells per line)
331  std::vector<PDFValueType *> m_BufferPDFValuesContainer;
332  std::vector<OffsetValueType> m_BufferOffsetContainer;
335  // Pointer handle to parent version
337  // Smart pointer handle to parent version
339  };
340 
341  std::vector<DerivativeBufferManager> m_ThreaderDerivativeManager;
344 
346 
349  mutable std::vector<DerivativeType> m_LocalDerivativeByParzenBin;
350 
351 private:
352  MattesMutualInformationImageToImageMetricv4(const Self &) ITK_DELETE_FUNCTION;
353  void operator = (const Self &) ITK_DELETE_FUNCTION;
354 
356  virtual void ComputeResults() const;
357 
358 };
359 
360 } // end namespace itk
361 
362 #ifndef ITK_MANUAL_INSTANTIATION
363 #include "itkMattesMutualInformationImageToImageMetricv4.hxx"
364 #endif
365 
366 #endif
Critical section locking class that can be allocated on the stack.
Superclass::RegionType RegionType
Definition: itkImage.h:137
Superclass::VirtualPointSetType VirtualPointSetType
Light weight base class for most itk classes.
Superclass::JacobianType JacobianType
Superclass::MeasureType MeasureType
Superclass::VirtualIndexType VirtualIndexType
SmartPointer< Self > Pointer
Definition: itkImage.h:81
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
signed long OffsetValueType
Definition: itkIntTypes.h:154
Superclass::MovingTransformType MovingTransformType
ImageToImageMetricv4< TFixedImage, TMovingImage, TVirtualImage, TInternalComputationValueType, TMetricTraits > Superclass
Class for partitioning of an ImageRegion.
typedef(itk::Concept::IsFloatingPoint< FixedImagePixelValueType >) OnlyDefinedForFloatingPointTypes0
MetricTraits::FixedImageGradientType FixedImageGradientType
void PrintSelf(std::ostream &os, Indent indent) const override
unsigned long SizeValueType
Definition: itkIntTypes.h:143
TPixel PixelType
Definition: itkImage.h:89
Derivative of a BSpline kernel used for density estimation and nonparameteric regression.
BSpline kernel used for density estimation and nonparameteric regression.
OffsetValueType ComputeSingleFixedImageParzenWindowIndex(const FixedImagePixelType &value) const
FixedImageType::PixelType FixedImagePixelType
FixedImageType::IndexType FixedImageIndexType
Superclass::VirtualPointType VirtualPointType
Computes the mutual information between two images to be registered using the method of Mattes et al...
Superclass::DerivativeType DerivativeType
Superclass::IndexType IndexType
Definition: itkImage.h:119
Standard exception handling object.
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
MovingImageType::PixelType MovingImagePixelType
MovingImageType::PointType MovingImagePointType
MetricTraits::MovingImageGradientType MovingImageGradientType
FixedSampledPointSetType::Pointer FixedSampledPointSetPointer
virtual void FinalizeThread(const ThreadIdType threadId) override
Define additional traits for native types such as int or float.
FixedImageType::PointType FixedImagePointType
Templated n-dimensional image class.
Definition: itkImage.h:75
Superclass::VirtualImageType VirtualImageType