ITK  4.6.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 >
53 {
54 public:
60 
63 
66 
68  typedef TMovingImage MovingImageType;
69  typedef typename TMovingImage::PixelType MovingImagePixelType;
70  typedef typename MovingImageType::ConstPointer MovingImageConstPointer;
71 
73  typedef TFixedImage FixedImageType;
74  typedef typename TFixedImage::PixelType FixedImagePixelType;
75  typedef typename FixedImageType::ConstPointer FixedImageConstPointer;
76  typedef typename FixedImageType::RegionType FixedImageRegionType;
77 
79  itkStaticConstMacro(MovingImageDimension,
80  unsigned int,
81  TMovingImage::ImageDimension);
82  itkStaticConstMacro(FixedImageDimension,
83  unsigned int,
84  TFixedImage::ImageDimension);
86 
89  itkGetStaticConstMacro(MovingImageDimension),
90  itkGetStaticConstMacro(FixedImageDimension) >
92 
98 
100  typedef typename FixedImageType::IndexType FixedImageIndexType;
102  typedef typename MovingImageType::IndexType MovingImageIndexType;
105 
106  typedef std::vector< FixedImageIndexType > FixedImageIndexContainer;
107 
110 
119 
121 
127 
133 
136 
139 
142 
144  itkSetConstObjectMacro( FixedImage, FixedImageType );
145  itkGetConstObjectMacro(FixedImage, FixedImageType );
147 
149  itkSetConstObjectMacro( MovingImage, MovingImageType );
150  itkGetConstObjectMacro(MovingImage, MovingImageType );
152 
154  itkSetObjectMacro( Transform, TransformType );
155 
157  itkGetModifiableObjectMacro(Transform, TransformType);
158 
160  itkSetObjectMacro(Interpolator, InterpolatorType);
161 
163  itkGetModifiableObjectMacro(Interpolator, InterpolatorType);
164 
167  {
168  return this->GetNumberOfPixelsCounted();
169  }
170 
171  itkGetConstReferenceMacro(NumberOfPixelsCounted, SizeValueType);
172 
174  virtual void SetFixedImageRegion(const FixedImageRegionType reg);
175 
177  itkGetConstReferenceMacro(FixedImageRegion, FixedImageRegionType);
178 
180  itkSetObjectMacro(MovingImageMask, MovingImageMaskType);
181  itkSetConstObjectMacro(MovingImageMask, MovingImageMaskType);
182  itkGetConstObjectMacro(MovingImageMask, MovingImageMaskType);
184 
186  itkSetObjectMacro(FixedImageMask, FixedImageMaskType);
187  itkSetConstObjectMacro(FixedImageMask, FixedImageMaskType);
188  itkGetConstObjectMacro(FixedImageMask, FixedImageMaskType);
190 
193  void SetFixedImageIndexes(const FixedImageIndexContainer & indexes);
194 
195  void SetUseFixedImageIndexes(bool useIndex);
196 
197  itkGetConstReferenceMacro(UseFixedImageIndexes, bool);
198 
200  void SetNumberOfThreads(ThreadIdType numberOfThreads);
201  itkGetConstReferenceMacro(NumberOfThreads, ThreadIdType);
203 
205  itkSetMacro(ComputeGradient, bool);
206  itkGetConstReferenceMacro(ComputeGradient, bool);
207  itkBooleanMacro(ComputeGradient);
209 
211  virtual void ComputeGradient(void);
212 
214  itkGetModifiableObjectMacro(GradientImage, GradientImageType);
215 
217  void SetTransformParameters(const ParametersType & parameters) const;
218 
220  virtual unsigned int GetNumberOfParameters(void) const ITK_OVERRIDE
221  {
222  return m_Transform->GetNumberOfParameters();
223  }
224 
227  virtual void Initialize(void)
228  throw ( ExceptionObject );
229 
231  virtual void MultiThreadingInitialize(void) throw ( ExceptionObject );
232 
235  virtual void SetNumberOfFixedImageSamples(SizeValueType numSamples);
236  itkGetConstReferenceMacro(NumberOfFixedImageSamples, SizeValueType);
238 
241  void SetNumberOfSpatialSamples(SizeValueType num)
242  {
243  this->SetNumberOfFixedImageSamples(num);
244  }
245 
247  {
248  return this->GetNumberOfFixedImageSamples();
249  }
250 
254 
255  itkGetConstReferenceMacro(FixedImageSamplesIntensityThreshold, FixedImagePixelType);
256 
257  void SetUseFixedImageSamplesIntensityThreshold(bool useThresh);
258 
259  itkGetConstReferenceMacro(UseFixedImageSamplesIntensityThreshold, bool);
260 
264  void SetUseAllPixels(bool useAllPixels);
265 
266  void UseAllPixelsOn(void)
267  {
268  this->SetUseAllPixels(true);
269  }
270 
271  void UseAllPixelsOff(void)
272  {
273  this->SetUseAllPixels(false);
274  }
275 
276  itkGetConstReferenceMacro(UseAllPixels, bool);
277 
282  void SetUseSequentialSampling(bool sequentialSampling);
283 
284  itkGetConstReferenceMacro(UseSequentialSampling, bool);
285 
295  void ReinitializeSeed();
296  void ReinitializeSeed(int seed);
298 
315  itkSetMacro(UseCachingOfBSplineWeights, bool);
316  itkGetConstReferenceMacro(UseCachingOfBSplineWeights, bool);
317  itkBooleanMacro(UseCachingOfBSplineWeights);
319 
320  typedef MultiThreader MultiThreaderType;
322  itkGetModifiableObjectMacro(Threader, MultiThreaderType);
324  {
325  return m_ThreaderTransform;
326  }
328 
329 protected:
331  virtual ~ImageToImageMetric();
332 
333  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
334 
341  {
342 public:
344  {
345  point.Fill(0.0);
346  value = 0;
347  valueIndex = 0;
348  }
349 
351 
352 public:
354  double value;
355  unsigned int valueIndex;
356  };
357 
360 
363 
365  typedef std::vector< FixedImageSamplePoint > FixedImageSampleContainer;
366 
368  virtual void SampleFixedImageRegion(FixedImageSampleContainer & samples) const;
369 
370  virtual void SampleFixedImageIndexes(FixedImageSampleContainer & samples) const;
371 
374  samples) const;
375 
378 
380 
382  //m_NumberOfPixelsCounted must be mutable because the const
383  //thread consolidation functions merge each threads valus
384  //onto this accumulator variable.
386 
389 
392 
396 
398 
401 
404 
406 
409 
411 
413 
422 
426 
427  itkStaticConstMacro(DeformationSplineOrder, unsigned int, 3);
428 
430  FixedImageType ::ImageDimension,
431  itkGetStaticConstMacro(DeformationSplineOrder) > BSplineTransformType;
432 
436 
440 
441  typedef std::vector< MovingImagePointType > MovingImagePointArrayType;
442  typedef std::vector< bool > BooleanArrayType;
452 
459 
461 
466 
468 
469  // Variables needed for optionally caching values when using a BSpline
470  // transform.
474 
477 
478  virtual void PreComputeTransformValues(void);
479 
482  virtual void TransformPoint(unsigned int sampleNumber,
483  MovingImagePointType & mappedPoint,
484  bool & sampleWithinSupportRegion,
485  double & movingImageValue,
486  ThreadIdType threadId) const;
487 
488  virtual void TransformPointWithDerivatives(unsigned int sampleNumber,
489  MovingImagePointType & mappedPoint,
490  bool & sampleWithinSupportRegion,
491  double & movingImageValue,
492  ImageDerivativesType & gradient,
493  ThreadIdType threadId) const;
494 
497 
500 
503 
505  virtual void ComputeImageDerivatives(const MovingImagePointType & mappedPoint,
506  ImageDerivativesType & gradient,
507  ThreadIdType threadId) const;
508 
515  };
516 
517  MultiThreaderType::Pointer m_Threader;
519  mutable unsigned int * m_ThreaderNumberOfMovingImageSamples;
522 
524 
525  void GetValueMultiThreadedInitiate(void) const;
526 
528 
530 
532 
534 
535  virtual inline void GetValueThread(ThreadIdType threadId) const;
536 
537  virtual inline void GetValueThreadPreProcess(
538  ThreadIdType itkNotUsed(threadId),
539  bool itkNotUsed(withinSampleThread) ) const
540  {}
541  virtual inline bool GetValueThreadProcessSample(
542  ThreadIdType itkNotUsed(threadId),
543  SizeValueType itkNotUsed(fixedImageSample),
544  const MovingImagePointType & itkNotUsed(mappedPoint),
545  double itkNotUsed(movingImageValue) ) const
546  { return false; }
547  virtual inline void GetValueThreadPostProcess(
548  ThreadIdType itkNotUsed(threadId),
549  bool itkNotUsed(withinSampleThread) ) const
550  {}
551 
553 
555 
557 
559 
561 
563 
564  virtual inline void GetValueAndDerivativeThread(ThreadIdType threadId) const;
565 
567  ThreadIdType itkNotUsed(threadId),
568  bool itkNotUsed(withinSampleThread) ) const
569  {}
571  ThreadIdType itkNotUsed(threadId),
572  SizeValueType itkNotUsed(fixedImageSample),
573  const MovingImagePointType & itkNotUsed(mappedPoint),
574  double itkNotUsed(movingImageValue),
575  const ImageDerivativesType & itkNotUsed(movingImageGradientValue) ) const
576  { return false; }
578  ThreadIdType itkNotUsed(threadId),
579  bool itkNotUsed(withinSampleThread) ) const
580  {}
581 
585  virtual void SynchronizeTransforms() const;
586 
587 private:
588  ImageToImageMetric(const Self &); //purposely not implemented
589  void operator=(const Self &); //purposely not implemented
590 
592 };
593 } // end namespace itk
594 
595 #ifndef ITK_MANUAL_INSTANTIATION
596 #include "itkImageToImageMetric.hxx"
597 #endif
598 
599 #endif
virtual bool GetValueAndDerivativeThreadProcessSample(ThreadIdType, SizeValueType, const MovingImagePointType &, double, const ImageDerivativesType &) const
Array class with size defined at construction time.
Definition: itkArray.h:50
virtual void GetValueThreadPostProcess(ThreadIdType, bool) const
unsigned int * m_ThreaderNumberOfMovingImageSamples
TransformType::OutputPointType OutputPointType
virtual void GetValueThreadPreProcess(ThreadIdType, bool) const
BSplineTransformIndexArrayType m_BSplineTransformIndices
static ITK_THREAD_RETURN_TYPE GetValueMultiThreaded(void *arg)
This class is a base for the CostFunctions returning a single value.
FixedImageMaskType::ConstPointer FixedImageMaskConstPointer
static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPreProcess(void *arg)
std::vector< FixedImageIndexType > FixedImageIndexContainer
virtual void MultiThreadingInitialize(void)
static const unsigned int FixedImageDimension
BSplineTransformType::WeightsType BSplineTransformWeightsType
MovingImageMaskConstPointer m_MovingImageMask
Superclass::ParametersValueType CoordinateRepresentationType
void GetValueAndDerivativeMultiThreadedPreProcessInitiate(void) const
static ITK_THREAD_RETURN_TYPE GetValueMultiThreadedPreProcess(void *arg)
TransformType::InputPointType InputPointType
virtual void GetValueAndDerivativeThreadPreProcess(ThreadIdType, bool) const
virtual void GetValueThread(ThreadIdType threadId) const
void SetNumberOfSpatialSamples(SizeValueType num)
signed long IndexValueType
Definition: itkIntTypes.h:150
Array2D< WeightsValueType > BSplineTransformWeightsArrayType
void SetFixedImageIndexes(const FixedImageIndexContainer &indexes)
BSplineParametersOffsetType m_BSplineParametersOffset
virtual void SetFixedImageRegion(const FixedImageRegionType reg)
FixedImageIndexContainer m_FixedImageIndexes
TransformType::Pointer TransformPointer
FixedArray< SizeValueType, FixedImageType::ImageDimension > BSplineParametersOffsetType
CovariantVector< double, itkGetStaticConstMacro(MovingImageDimension) > ImageDerivativesType
TransformPointer * m_ThreaderTransform
SizeValueType GetNumberOfSpatialSamples(void)
BSplineTransformType::Pointer m_BSplineTransform
BSplineTransformWeightsArrayType m_BSplineTransformWeightsArray
NumericTraits< MovingImagePixelType >::RealType RealType
BSplineTransformIndicesArrayType m_BSplineTransformIndicesArray
void SetNumberOfThreads(ThreadIdType numberOfThreads)
std::vector< MovingImagePointType > MovingImagePointArrayType
FixedImageType::IndexType FixedImageIndexType
BSplineTransformWeightsType m_BSplineTransformWeights
Computes the gradient of an image by convolution with the first derivative of a Gaussian.
Superclass::DerivativeType DerivativeType
MovingImageType::ConstPointer MovingImageConstPointer
Calculate the derivative by central differencing.
virtual void Initialize(void)
TransformType::OutputPointType MovingImagePointType
void operator=(const Self &)
MultiThreaderParameterType m_ThreaderParameter
unsigned long SizeValueType
Definition: itkIntTypes.h:143
#define ITK_THREAD_RETURN_TYPE
MovingImageConstPointer m_MovingImage
void Fill(const ValueType &)
const TransformPointer * GetThreaderTransform()
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:50
void GetValueAndDerivativeMultiThreadedInitiate(void) const
FixedImageSampleContainer m_FixedImageSamples
MovingImagePointArrayType m_BSplinePreTransformPointsArray
virtual void TransformPoint(unsigned int sampleNumber, MovingImagePointType &mappedPoint, bool &sampleWithinSupportRegion, double &movingImageValue, ThreadIdType threadId) const
virtual unsigned int GetNumberOfParameters(void) const ITK_OVERRIDE
TMovingImage::PixelType MovingImagePixelType
void GetValueAndDerivativeMultiThreadedPostProcessInitiate(void) const
virtual void ComputeImageDerivatives(const MovingImagePointType &mappedPoint, ImageDerivativesType &gradient, ThreadIdType threadId) const
CentralDifferenceImageFunction< MovingImageType, CoordinateRepresentationType > DerivativeFunctionType
Transform points and vectors from an input space to an output space.
Definition: itkTransform.h:82
FixedImageIndexType::IndexValueType FixedImageIndexValueType
Array2D class representing a 2D array with size defined at construction time.
Definition: itkArray2D.h:44
void SetUseSequentialSampling(bool sequentialSampling)
BooleanArrayType m_WithinBSplineSupportRegionArray
Class to hold and manage different parameter types used during optimization.
SpatialObject< itkGetStaticConstMacro(FixedImageDimension) > FixedImageMaskType
Superclass::ParametersType ParametersType
BSplineBaseTransform< CoordinateRepresentationType, FixedImageType::ImageDimension, itkGetStaticConstMacro(DeformationSplineOrder) > BSplineTransformType
SmartPointer< const Self > ConstPointer
Implementation of the composite pattern.
FixedImageConstPointer m_FixedImage
Standard exception handling object.
virtual void SampleFullFixedImageRegion(FixedImageSampleContainer &samples) const
void GetValueMultiThreadedInitiate(void) const
A base class with common elements of BSplineTransform and BSplineDeformableTransform.
InterpolatorPointer m_Interpolator
BSplineTransformIndexArrayType::ValueType IndexValueType
GradientImagePointer m_GradientImage
void SetFixedImageSamplesIntensityThreshold(const FixedImagePixelType &thresh)
MovingImageMaskType::ConstPointer MovingImageMaskConstPointer
virtual bool GetValueThreadProcessSample(ThreadIdType, SizeValueType, const MovingImagePointType &, double) const
BSplineTransformWeightsType * m_ThreaderBSplineTransformWeights
void SetUseFixedImageIndexes(bool useIndex)
std::vector< FixedImageSamplePoint > FixedImageSampleContainer
virtual void ComputeGradient(void)
Superclass::ParametersType ParametersType
SmartPointer< GradientImageType > GradientImagePointer
BSplineInterpolateImageFunction< MovingImageType, CoordinateRepresentationType > BSplineInterpolatorType
SingleValuedCostFunction Superclass
Image< GradientPixelType, itkGetStaticConstMacro(MovingImageDimension) > GradientImageType
SizeValueType GetNumberOfMovingImageSamples(void)
Base class for all image interpolaters.
SpatialObject< itkGetStaticConstMacro(MovingImageDimension) > MovingImageMaskType
BSplineInterpolatorType::Pointer m_BSplineInterpolator
InterpolatorType::Pointer InterpolatorPointer
virtual void SampleFixedImageRegion(FixedImageSampleContainer &samples) const
TransformType::JacobianType TransformJacobianType
FixedImageMaskConstPointer m_FixedImageMask
DerivativeFunctionType::Pointer m_DerivativeCalculator
FixedImageMaskType::Pointer FixedImageMaskPointer
FixedImageRegionType m_FixedImageRegion
MovingImageMaskType::Pointer MovingImageMaskPointer
virtual void SynchronizeTransforms() const
FixedImagePixelType m_FixedImageSamplesIntensityThreshold
BSplineTransformWeightsType::ValueType WeightsValueType
static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPostProcess(void *arg)
Superclass::ParametersValueType ParametersValueType
InterpolateImageFunction< MovingImageType, CoordinateRepresentationType > InterpolatorType
virtual void PrintSelf(std::ostream &os, Indent indent) const ITK_OVERRIDE
Control indentation during Print() invocation.
Definition: itkIndent.h:49
FixedImageType::ConstPointer FixedImageConstPointer
TFixedImage::PixelType FixedImagePixelType
static ITK_THREAD_RETURN_TYPE GetValueMultiThreadedPostProcess(void *arg)
TransformType::InputPointType FixedImagePointType
virtual const SizeValueType & GetNumberOfFixedImageSamples() const
virtual void SetNumberOfFixedImageSamples(SizeValueType numSamples)
void SetUseFixedImageSamplesIntensityThreshold(bool useThresh)
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
Array2D< IndexValueType > BSplineTransformIndicesArrayType
void SetTransformParameters(const ParametersType &parameters) const
Superclass::MeasureType MeasureType
CovariantVector< RealType, itkGetStaticConstMacro(MovingImageDimension) > GradientPixelType
static const unsigned int MovingImageDimension
Transform< CoordinateRepresentationType, itkGetStaticConstMacro(MovingImageDimension), itkGetStaticConstMacro(FixedImageDimension) > TransformType
void GetValueMultiThreadedPreProcessInitiate(void) const
virtual void SampleFixedImageIndexes(FixedImageSampleContainer &samples) const
Computes similarity between regions of two images.
virtual const SizeValueType & GetNumberOfPixelsCounted() const
MovingImageType::IndexType MovingImageIndexType
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:51
A templated class holding a n-Dimensional covariant vector.
virtual void GetValueAndDerivativeThread(ThreadIdType threadId) const
void GetValueMultiThreadedPostProcessInitiate(void) const
TransformType::ParametersType TransformParametersType
GradientRecursiveGaussianImageFilter< MovingImageType, GradientImageType > GradientImageFilterType
static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreaded(void *arg)
Templated n-dimensional image class.
Definition: itkImage.h:75
SmartPointer< Self > Pointer
MultiThreaderType::Pointer m_Threader
FixedImageType::RegionType FixedImageRegionType
std::vector< bool > BooleanArrayType
virtual void TransformPointWithDerivatives(unsigned int sampleNumber, MovingImagePointType &mappedPoint, bool &sampleWithinSupportRegion, double &movingImageValue, ImageDerivativesType &gradient, ThreadIdType threadId) const
virtual void PreComputeTransformValues(void)
static const unsigned int DeformationSplineOrder
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
BSplineTransformType::ParameterIndexArrayType BSplineTransformIndexArrayType
GradientImageFilterType::Pointer GradientImageFilterPointer
void SetUseAllPixels(bool useAllPixels)
SizeValueType m_NumberOfFixedImageSamples