ITK  4.13.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;
120  typedef typename Superclass::DerivativeType DerivativeType;
122 
123  typedef typename Superclass::FixedImageType FixedImageType;
124  typedef typename Superclass::FixedImagePointType FixedImagePointType;
125  typedef typename Superclass::FixedImageIndexType FixedImageIndexType;
126  typedef typename Superclass::FixedImagePixelType FixedImagePixelType;
127  typedef typename Superclass::FixedImageGradientType FixedImageGradientType;
128 
129  typedef typename Superclass::MovingImagePointType MovingImagePointType;
130  typedef typename Superclass::MovingImagePixelType MovingImagePixelType;
131  typedef typename Superclass::MovingImageGradientType MovingImageGradientType;
132 
133  typedef typename Superclass::MovingTransformType MovingTransformType;
134  typedef typename Superclass::JacobianType JacobianType;
135  typedef typename Superclass::VirtualImageType VirtualImageType;
136  typedef typename Superclass::VirtualIndexType VirtualIndexType;
137  typedef typename Superclass::VirtualPointType VirtualPointType;
138  typedef typename Superclass::VirtualPointSetType VirtualPointSetType;
139 
141  typedef typename Superclass::FixedSampledPointSetPointer FixedSampledPointSetPointer;
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) 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;
166  typedef Image<PDFValueType, 3> JointPDFDerivativesType;
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:
197  virtual ~MattesMutualInformationImageToImageMetricv4() ITK_OVERRIDE;
198 
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 
209  typedef typename JointPDFType::PixelType JointPDFValueType;
210  typedef typename JointPDFType::RegionType JointPDFRegionType;
216 
220 
223  virtual void GetValueCommonAfterThreadedExecution();
224 
225  OffsetValueType ComputeSingleFixedImageParzenWindowIndex( const FixedImagePixelType & value ) const;
226 
228  SizeValueType m_NumberOfHistogramBins;
229  PDFValueType m_MovingImageNormalizedMin;
230  PDFValueType m_FixedImageNormalizedMin;
231  PDFValueType m_FixedImageTrueMin;
232  PDFValueType m_FixedImageTrueMax;
233  PDFValueType m_MovingImageTrueMin;
234  PDFValueType m_MovingImageTrueMax;
235  PDFValueType m_FixedImageBinSize;
236  PDFValueType m_MovingImageBinSize;
237 
239  typename CubicBSplineFunctionType::Pointer m_CubicBSplineKernel;
240  typename CubicBSplineDerivativeFunctionType::Pointer m_CubicBSplineDerivativeKernel;
241 
244  typedef std::vector<PRatioType> PRatioArrayType;
245 
246  mutable PRatioArrayType m_PRatioArray;
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  {
273  typedef DerivativeBufferManager Self;
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  ITK_DISALLOW_COPY_AND_ASSIGN(MattesMutualInformationImageToImageMetricv4);
353 
355  virtual void ComputeResults() const;
356 
357 };
358 
359 } // end namespace itk
360 
361 #ifndef ITK_MANUAL_INSTANTIATION
362 #include "itkMattesMutualInformationImageToImageMetricv4.hxx"
363 #endif
364 
365 #endif
Critical section locking class that can be allocated on the stack.
TInternalComputationValueType ValueType
Definition: itkArray.h:55
Light weight base class for most itk classes.
Represent the size (bounds) of a n-dimensional image.
Definition: itkSize.h:52
signed long OffsetValueType
Definition: itkIntTypes.h:154
ImageToImageMetricv4< TFixedImage, TMovingImage, TVirtualImage, TInternalComputationValueType, TMetricTraits > Superclass
Class for partitioning of an ImageRegion.
unsigned long SizeValueType
Definition: itkIntTypes.h:143
Derivative of a BSpline kernel used for density estimation and nonparameteric regression.
BSpline kernel used for density estimation and nonparameteric regression.
FixedImageType::PixelType FixedImagePixelType
Computes the mutual information between two images to be registered using the method of Mattes et al...
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
Control indentation during Print() invocation.
Definition: itkIndent.h:49
Define additional traits for native types such as int or float.
Templated n-dimensional image class.
Definition: itkImage.h:75