ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkImageToImageMetric.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 itkImageToImageMetric_h
19 #define itkImageToImageMetric_h
20 
25 #include "itkSpatialObject.h"
27 
28 namespace itk
29 {
50 template< typename TFixedImage, typename TMovingImage >
51 class ITK_TEMPLATE_EXPORT ImageToImageMetric:
53 {
54 public:
55  ITK_DISALLOW_COPY_AND_ASSIGN(ImageToImageMetric);
56 
62 
64  using CoordinateRepresentationType = typename Superclass::ParametersValueType;
65 
68 
70  using MovingImageType = TMovingImage;
71  using MovingImagePixelType = typename TMovingImage::PixelType;
72  using MovingImageConstPointer = typename MovingImageType::ConstPointer;
73 
75  using FixedImageType = TFixedImage;
76  using FixedImagePixelType = typename TFixedImage::PixelType;
77  using FixedImageConstPointer = typename FixedImageType::ConstPointer;
79 
81  static constexpr unsigned int MovingImageDimension = TMovingImage::ImageDimension;
82  static constexpr unsigned int FixedImageDimension = TFixedImage::ImageDimension;
83 
86  Self::MovingImageDimension,
87  Self::FixedImageDimension >;
88 
94 
101 
102  using FixedImageIndexContainer = std::vector< FixedImageIndexType >;
103 
106 
114 
116 
122 
128 
130  using MeasureType = typename Superclass::MeasureType;
131 
133  using DerivativeType = typename Superclass::DerivativeType;
134 
136  using ParametersType = typename Superclass::ParametersType;
137 
139  itkSetConstObjectMacro( FixedImage, FixedImageType );
140  itkGetConstObjectMacro(FixedImage, FixedImageType );
142 
144  itkSetConstObjectMacro( MovingImage, MovingImageType );
145  itkGetConstObjectMacro(MovingImage, MovingImageType );
147 
149  itkSetObjectMacro( Transform, TransformType );
150 
152  itkGetModifiableObjectMacro(Transform, TransformType);
153 
155  itkSetObjectMacro(Interpolator, InterpolatorType);
156 
158  itkGetModifiableObjectMacro(Interpolator, InterpolatorType);
159 
162  {
163  return this->GetNumberOfPixelsCounted();
164  }
165 
166  itkGetConstReferenceMacro(NumberOfPixelsCounted, SizeValueType);
167 
169  virtual void SetFixedImageRegion(const FixedImageRegionType reg);
170 
172  itkGetConstReferenceMacro(FixedImageRegion, FixedImageRegionType);
173 
175  itkSetObjectMacro(MovingImageMask, MovingImageMaskType);
176  itkSetConstObjectMacro(MovingImageMask, MovingImageMaskType);
177  itkGetConstObjectMacro(MovingImageMask, MovingImageMaskType);
179 
181  itkSetObjectMacro(FixedImageMask, FixedImageMaskType);
182  itkSetConstObjectMacro(FixedImageMask, FixedImageMaskType);
183  itkGetConstObjectMacro(FixedImageMask, FixedImageMaskType);
185 
188  void SetFixedImageIndexes(const FixedImageIndexContainer & indexes);
189 
190  void SetUseFixedImageIndexes(bool useIndex);
191 
192  itkGetConstReferenceMacro(UseFixedImageIndexes, bool);
193 
195  void SetNumberOfWorkUnits(ThreadIdType numberOfThreads);
196  itkGetConstReferenceMacro(NumberOfWorkUnits, ThreadIdType);
198 
200  itkSetMacro(ComputeGradient, bool);
201  itkGetConstReferenceMacro(ComputeGradient, bool);
202  itkBooleanMacro(ComputeGradient);
204 
206  virtual void ComputeGradient();
207 
209  itkGetModifiableObjectMacro(GradientImage, GradientImageType);
210 
212  void SetTransformParameters(const ParametersType & parameters) const;
213 
215  unsigned int GetNumberOfParameters() const override
216  {
217  return m_Transform->GetNumberOfParameters();
218  }
219 
222  virtual void Initialize();
223 
225  virtual void MultiThreadingInitialize();
226 
229  virtual void SetNumberOfFixedImageSamples(SizeValueType numSamples);
230  itkGetConstReferenceMacro(NumberOfFixedImageSamples, SizeValueType);
232 
236  {
237  this->SetNumberOfFixedImageSamples(num);
238  }
239 
241  {
242  return this->GetNumberOfFixedImageSamples();
243  }
244 
247  void SetFixedImageSamplesIntensityThreshold(const FixedImagePixelType & thresh);
248 
249  itkGetConstReferenceMacro(FixedImageSamplesIntensityThreshold, FixedImagePixelType);
250 
251  void SetUseFixedImageSamplesIntensityThreshold(bool useThresh);
252 
253  itkGetConstReferenceMacro(UseFixedImageSamplesIntensityThreshold, bool);
254 
258  void SetUseAllPixels(bool useAllPixels);
259 
261  {
262  this->SetUseAllPixels(true);
263  }
264 
266  {
267  this->SetUseAllPixels(false);
268  }
269 
270  itkGetConstReferenceMacro(UseAllPixels, bool);
271 
276  void SetUseSequentialSampling(bool sequentialSampling);
277 
278  itkGetConstReferenceMacro(UseSequentialSampling, bool);
279 
289  void ReinitializeSeed();
290  void ReinitializeSeed(int seed);
292 
309  itkSetMacro(UseCachingOfBSplineWeights, bool);
310  itkGetConstReferenceMacro(UseCachingOfBSplineWeights, bool);
311  itkBooleanMacro(UseCachingOfBSplineWeights);
313 
316  itkGetModifiableObjectMacro(Threader, MultiThreaderType);
318  {
319  return m_ThreaderTransform;
320  }
322 
323 protected:
325  ~ImageToImageMetric() override;
326 
327  void PrintSelf(std::ostream & os, Indent indent) const override;
328 
335  {
336 public:
338  {
339  point.Fill(0.0);
340  value = 0;
341  valueIndex = 0;
342  }
343 
344  ~FixedImageSamplePoint() = default;
345 
346 public:
348  double value;
349  unsigned int valueIndex;
350  };
351 
352  bool m_UseFixedImageIndexes{false};
354 
355  bool m_UseFixedImageSamplesIntensityThreshold{false};
357 
359  using FixedImageSampleContainer = std::vector< FixedImageSamplePoint >;
360 
362  virtual void SampleFixedImageRegion(FixedImageSampleContainer & samples) const;
363 
364  virtual void SampleFixedImageIndexes(FixedImageSampleContainer & samples) const;
365 
367  virtual void SampleFullFixedImageRegion(FixedImageSampleContainer &
368  samples) const;
369 
372 
373  SizeValueType m_NumberOfParameters{0};
374 
375  SizeValueType m_NumberOfFixedImageSamples{50000};
376  //m_NumberOfPixelsCounted must be mutable because the const
377  //thread consolidation functions merge each work unit's values
378  //onto this accumulator variable.
379  mutable SizeValueType m_NumberOfPixelsCounted{0};
380 
383 
386 
390 
392 
393  bool m_ComputeGradient{true};
395 
398 
399  ThreadIdType m_NumberOfWorkUnits{1};
400 
401  bool m_UseAllPixels{false};
402  bool m_UseSequentialSampling{false};
403 
404  bool m_ReseedIterator{false};
405 
406  mutable int m_RandomSeed;
407 
415  bool m_TransformIsBSpline{false};
416 
419  SizeValueType m_NumBSplineWeights{0};
420 
421  static constexpr unsigned int DeformationSplineOrder = 3;
422 
424  FixedImageType ::ImageDimension,
425  Self::DeformationSplineOrder >;
426 
428  using WeightsValueType = typename BSplineTransformWeightsType::ValueType;
430 
432  using IndexValueType = typename BSplineTransformIndexArrayType::ValueType;
434 
435  using MovingImagePointArrayType = std::vector< MovingImagePointType >;
436  using BooleanArrayType = std::vector< bool >;
445 
449  using ImageDerivativesType =
451 
453 
458 
460 
461  // Variables needed for optionally caching values when using a BSpline
462  // transform.
463  bool m_UseCachingOfBSplineWeights{true};
466 
469 
470  virtual void PreComputeTransformValues();
471 
474  virtual void TransformPoint(unsigned int sampleNumber,
475  MovingImagePointType & mappedPoint,
476  bool & sampleWithinSupportRegion,
477  double & movingImageValue,
478  ThreadIdType threadId) const;
479 
480  virtual void TransformPointWithDerivatives(unsigned int sampleNumber,
481  MovingImagePointType & mappedPoint,
482  bool & sampleWithinSupportRegion,
483  double & movingImageValue,
484  ImageDerivativesType & gradient,
485  ThreadIdType threadId) const;
486 
488  bool m_InterpolatorIsBSpline{false};
489 
492 
495 
497  virtual void ComputeImageDerivatives(const MovingImagePointType & mappedPoint,
498  ImageDerivativesType & gradient,
499  ThreadIdType threadId) const;
500 
514  {
515  public:
517  : m_ConstMetricPointer{ i2i_metricPointer }
518  {}
519  const ImageToImageMetric * GetConstMetricPointer() const { return m_ConstMetricPointer; }
520  private:
522  };
524 
533  {
534  public:
535  MultiThreaderWorkUnitInfoImageToImageMetricWrapper(const void * workunitInfoAsVoid)
536  : m_WorkUnitInfo{ static_cast<const typename MultiThreaderType::WorkUnitInfo *>( workunitInfoAsVoid )}
537  { }
538  ThreadIdType GetThreadId() const { return m_WorkUnitInfo->WorkUnitID; }
540  {
541  return (static_cast<ConstantPointerWrapper *>( m_WorkUnitInfo ->UserData))->GetConstMetricPointer();
542  }
543  private:
545  };
547 
550  mutable unsigned int * m_ThreaderNumberOfMovingImageSamples{nullptr};
551  bool m_WithinThreadPreProcess{false};
552  bool m_WithinThreadPostProcess{false};
553 
554  void GetValueMultiThreadedInitiate() const;
555 
556  void GetValueMultiThreadedPostProcessInitiate() const;
557 
558  static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION GetValueMultiThreaded(void *arg);
559 
560  static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION GetValueMultiThreadedPostProcess(void *arg);
561 
562  virtual inline void GetValueThread(ThreadIdType threadId) const;
563 
564  virtual inline void GetValueThreadPreProcess(
565  ThreadIdType itkNotUsed(threadId),
566  bool itkNotUsed(withinSampleThread) ) const
567  {}
568  virtual inline bool GetValueThreadProcessSample(
569  ThreadIdType itkNotUsed(threadId),
570  SizeValueType itkNotUsed(fixedImageSample),
571  const MovingImagePointType & itkNotUsed(mappedPoint),
572  double itkNotUsed(movingImageValue) ) const
573  { return false; }
574  virtual inline void GetValueThreadPostProcess(
575  ThreadIdType itkNotUsed(threadId),
576  bool itkNotUsed(withinSampleThread) ) const
577  {}
578 
579  void GetValueAndDerivativeMultiThreadedInitiate() const;
580 
581  void GetValueAndDerivativeMultiThreadedPostProcessInitiate() const;
582 
583  static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION GetValueAndDerivativeMultiThreaded(void *arg);
584 
585  static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION GetValueAndDerivativeMultiThreadedPostProcess(void *arg);
586 
587  virtual inline void GetValueAndDerivativeThread(ThreadIdType threadId) const;
588 
590  ThreadIdType itkNotUsed(threadId),
591  bool itkNotUsed(withinSampleThread) ) const
592  {}
594  ThreadIdType itkNotUsed(threadId),
595  SizeValueType itkNotUsed(fixedImageSample),
596  const MovingImagePointType & itkNotUsed(mappedPoint),
597  double itkNotUsed(movingImageValue),
598  const ImageDerivativesType & itkNotUsed(movingImageGradientValue) ) const
599  { return false; }
601  ThreadIdType itkNotUsed(threadId),
602  bool itkNotUsed(withinSampleThread) ) const
603  {}
604 
608  virtual void SynchronizeTransforms() const;
609 
610 private:
612 };
613 } // end namespace itk
614 
615 #ifndef ITK_MANUAL_INSTANTIATION
616 #include "itkImageToImageMetric.hxx"
617 #endif
618 
619 #endif
typename FixedImageType::RegionType FixedImageRegionType
virtual bool GetValueAndDerivativeThreadProcessSample(ThreadIdType, SizeValueType, const MovingImagePointType &, double, const ImageDerivativesType &) const
Array class with size defined at construction time.
Definition: itkArray.h:46
virtual void GetValueThreadPostProcess(ThreadIdType, bool) const
typename BSplineTransformWeightsType::ValueType WeightsValueType
virtual void GetValueThreadPreProcess(ThreadIdType, bool) const
BSplineTransformIndexArrayType m_BSplineTransformIndices
typename Superclass::ParametersType ParametersType
Definition: itkTransform.h:119
This class is a base for the CostFunctions returning a single value.
Light weight base class for most itk classes.
SizeValueType GetNumberOfSpatialSamples()
MovingImageMaskConstPointer m_MovingImageMask
typename TransformType::InputPointType FixedImagePointType
unsigned int GetNumberOfParameters() const override
typename TransformType::InputPointType InputPointType
Define numeric traits for std::vector.
typename BSplineTransformType::WeightsType BSplineTransformWeightsType
std::vector< FixedImageIndexType > FixedImageIndexContainer
typename FixedImageIndexType::IndexValueType FixedImageIndexValueType
typename TransformType::JacobianType TransformJacobianType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
virtual void GetValueAndDerivativeThreadPreProcess(ThreadIdType, bool) const
void SetNumberOfSpatialSamples(SizeValueType num)
BSplineParametersOffsetType m_BSplineParametersOffset
FixedImageIndexContainer m_FixedImageIndexes
TransformPointer * m_ThreaderTransform
typename GradientImageFilterType::Pointer GradientImageFilterPointer
BSplineTransformType::Pointer m_BSplineTransform
BSplineTransformWeightsArrayType m_BSplineTransformWeightsArray
BSplineTransformIndicesArrayType m_BSplineTransformIndicesArray
BSplineTransformWeightsType m_BSplineTransformWeights
itk::ITK_THREAD_RETURN_TYPE ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
Computes the gradient of an image by convolution with the first derivative of a Gaussian.
Calculate the derivative by central differencing.
ConstantPointerWrapper(ImageToImageMetric *i2i_metricPointer)
MovingImageConstPointer m_MovingImage
const TransformPointer * GetThreaderTransform()
typename TMovingImage::PixelType MovingImagePixelType
FixedImageSampleContainer m_FixedImageSamples
typename FixedImageType::IndexType FixedImageIndexType
MovingImagePointArrayType m_BSplinePreTransformPointsArray
std::vector< bool > BooleanArrayType
Transform points and vectors from an input space to an output space.
Definition: itkTransform.h:83
Array2D class representing a 2D array with size defined at construction time.
Definition: itkArray2D.h:45
BooleanArrayType m_WithinBSplineSupportRegionArray
ConstantPointerWrapper * m_ConstSelfWrapper
signed long IndexValueType
Definition: itkIntTypes.h:90
typename TransformType::ParametersType TransformParametersType
Implementation of the composite pattern.
typename Superclass::ParametersValueType CoordinateRepresentationType
typename MovingImageMaskType::ConstPointer MovingImageMaskConstPointer
FixedImageConstPointer m_FixedImage
std::vector< FixedImageSamplePoint > FixedImageSampleContainer
A class for performing multithreaded execution.
A base class with common elements of BSplineTransform and BSplineDeformableTransform.
InterpolatorPointer m_Interpolator
typename WeightsFunctionType::WeightsType WeightsType
typename FixedImageMaskType::ConstPointer FixedImageMaskConstPointer
typename TransformType::OutputPointType OutputPointType
GradientImagePointer m_GradientImage
virtual bool GetValueThreadProcessSample(ThreadIdType, SizeValueType, const MovingImagePointType &, double) const
typename FixedImageType::ConstPointer FixedImageConstPointer
BSplineTransformWeightsType * m_ThreaderBSplineTransformWeights
unsigned int ThreadIdType
Definition: itkIntTypes.h:99
typename InterpolatorType::Pointer InterpolatorPointer
Base class for all image interpolaters.
BSplineInterpolatorType::Pointer m_BSplineInterpolator
FixedImageMaskConstPointer m_FixedImageMask
DerivativeFunctionType::Pointer m_DerivativeCalculator
typename TransformType::Pointer TransformPointer
typename TFixedImage::PixelType FixedImagePixelType
typename BSplineTransformType::ParameterIndexArrayType BSplineTransformIndexArrayType
FixedImageRegionType m_FixedImageRegion
typename NumericTraits< MovingImagePixelType >::RealType RealType
FixedImagePixelType m_FixedImageSamplesIntensityThreshold
typename FixedImageMaskType::Pointer FixedImageMaskPointer
typename MovingImageType::IndexType MovingImageIndexType
typename MovingImageMaskType::Pointer MovingImageMaskPointer
Control indentation during Print() invocation.
Definition: itkIndent.h:49
virtual void GetValueAndDerivativeThreadPostProcess(ThreadIdType, bool) const
Evaluates the B-Spline interpolation of an image. Spline order may be from 0 to 5.
BSplineTransformIndexArrayType * m_ThreaderBSplineTransformIndices
const ImageToImageMetric * GetConstMetricPointer() const
Computes similarity between regions of two images.
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:52
A templated class holding a n-Dimensional covariant vector.
std::vector< MovingImagePointType > MovingImagePointArrayType
SizeValueType GetNumberOfMovingImageSamples()
typename MovingImageType::ConstPointer MovingImageConstPointer
typename BSplineTransformIndexArrayType::ValueType IndexValueType
Templated n-dimensional image class.
Definition: itkImage.h:75
MultiThreaderType::Pointer m_Threader
typename TransformType::OutputPointType MovingImagePointType