ITK  4.13.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:
60 
62  typedef typename Superclass::ParametersValueType CoordinateRepresentationType;
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 
105 
106  typedef std::vector< FixedImageIndexType > FixedImageIndexContainer;
107 
110 
119 
121 
127 
133 
135  typedef typename Superclass::MeasureType MeasureType;
136 
138  typedef typename Superclass::DerivativeType DerivativeType;
139 
141  typedef typename Superclass::ParametersType ParametersType;
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();
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 
230  virtual void MultiThreadingInitialize(void);
231 
234  virtual void SetNumberOfFixedImageSamples(SizeValueType numSamples);
235  itkGetConstReferenceMacro(NumberOfFixedImageSamples, SizeValueType);
237 
241  {
242  this->SetNumberOfFixedImageSamples(num);
243  }
244 
246  {
247  return this->GetNumberOfFixedImageSamples();
248  }
249 
252  void SetFixedImageSamplesIntensityThreshold(const FixedImagePixelType & thresh);
253 
254  itkGetConstReferenceMacro(FixedImageSamplesIntensityThreshold, FixedImagePixelType);
255 
256  void SetUseFixedImageSamplesIntensityThreshold(bool useThresh);
257 
258  itkGetConstReferenceMacro(UseFixedImageSamplesIntensityThreshold, bool);
259 
263  void SetUseAllPixels(bool useAllPixels);
264 
265  void UseAllPixelsOn(void)
266  {
267  this->SetUseAllPixels(true);
268  }
269 
270  void UseAllPixelsOff(void)
271  {
272  this->SetUseAllPixels(false);
273  }
274 
275  itkGetConstReferenceMacro(UseAllPixels, bool);
276 
281  void SetUseSequentialSampling(bool sequentialSampling);
282 
283  itkGetConstReferenceMacro(UseSequentialSampling, bool);
284 
294  void ReinitializeSeed();
295  void ReinitializeSeed(int seed);
297 
314  itkSetMacro(UseCachingOfBSplineWeights, bool);
315  itkGetConstReferenceMacro(UseCachingOfBSplineWeights, bool);
316  itkBooleanMacro(UseCachingOfBSplineWeights);
318 
319  typedef MultiThreader MultiThreaderType;
321  itkGetModifiableObjectMacro(Threader, MultiThreaderType);
323  {
324  return m_ThreaderTransform;
325  }
327 
328 protected:
330  virtual ~ImageToImageMetric() ITK_OVERRIDE;
331 
332  virtual void PrintSelf(std::ostream & os, Indent indent) const ITK_OVERRIDE;
333 
340  {
341 public:
343  {
344  point.Fill(0.0);
345  value = 0;
346  valueIndex = 0;
347  }
348 
350 
351 public:
353  double value;
354  unsigned int valueIndex;
355  };
356 
359 
362 
364  typedef std::vector< FixedImageSamplePoint > FixedImageSampleContainer;
365 
367  virtual void SampleFixedImageRegion(FixedImageSampleContainer & samples) const;
368 
369  virtual void SampleFixedImageIndexes(FixedImageSampleContainer & samples) const;
370 
372  virtual void SampleFullFixedImageRegion(FixedImageSampleContainer &
373  samples) const;
374 
377 
379 
381  //m_NumberOfPixelsCounted must be mutable because the const
382  //thread consolidation functions merge each threads valus
383  //onto this accumulator variable.
385 
388 
391 
395 
397 
400 
403 
405 
408 
410 
411  mutable int m_RandomSeed;
412 
421 
425 
426  itkStaticConstMacro(DeformationSplineOrder, unsigned int, 3);
427 
429  FixedImageType ::ImageDimension,
430  itkGetStaticConstMacro(DeformationSplineOrder) > BSplineTransformType;
431 
435 
439 
440  typedef std::vector< MovingImagePointType > MovingImagePointArrayType;
441  typedef std::vector< bool > BooleanArrayType;
451 
458 
460 
465 
467 
468  // Variables needed for optionally caching values when using a BSpline
469  // transform.
473 
476 
477  virtual void PreComputeTransformValues();
478 
481  virtual void TransformPoint(unsigned int sampleNumber,
482  MovingImagePointType & mappedPoint,
483  bool & sampleWithinSupportRegion,
484  double & movingImageValue,
485  ThreadIdType threadId) const;
486 
487  virtual void TransformPointWithDerivatives(unsigned int sampleNumber,
488  MovingImagePointType & mappedPoint,
489  bool & sampleWithinSupportRegion,
490  double & movingImageValue,
491  ImageDerivativesType & gradient,
492  ThreadIdType threadId) const;
493 
496 
499 
502 
504  virtual void ComputeImageDerivatives(const MovingImagePointType & mappedPoint,
505  ImageDerivativesType & gradient,
506  ThreadIdType threadId) const;
507 
514  };
515 
516  MultiThreaderType::Pointer m_Threader;
518  mutable unsigned int * m_ThreaderNumberOfMovingImageSamples;
521 
522  void GetValueMultiThreadedPreProcessInitiate() const;
523 
524  void GetValueMultiThreadedInitiate() const;
525 
526  void GetValueMultiThreadedPostProcessInitiate() const;
527 
528  static ITK_THREAD_RETURN_TYPE GetValueMultiThreadedPreProcess(void *arg);
529 
530  static ITK_THREAD_RETURN_TYPE GetValueMultiThreaded(void *arg);
531 
532  static ITK_THREAD_RETURN_TYPE GetValueMultiThreadedPostProcess(void *arg);
533 
534  virtual inline void GetValueThread(ThreadIdType threadId) const;
535 
536  virtual inline void GetValueThreadPreProcess(
537  ThreadIdType itkNotUsed(threadId),
538  bool itkNotUsed(withinSampleThread) ) const
539  {}
540  virtual inline bool GetValueThreadProcessSample(
541  ThreadIdType itkNotUsed(threadId),
542  SizeValueType itkNotUsed(fixedImageSample),
543  const MovingImagePointType & itkNotUsed(mappedPoint),
544  double itkNotUsed(movingImageValue) ) const
545  { return false; }
546  virtual inline void GetValueThreadPostProcess(
547  ThreadIdType itkNotUsed(threadId),
548  bool itkNotUsed(withinSampleThread) ) const
549  {}
550 
551  void GetValueAndDerivativeMultiThreadedPreProcessInitiate() const;
552 
553  void GetValueAndDerivativeMultiThreadedInitiate() const;
554 
555  void GetValueAndDerivativeMultiThreadedPostProcessInitiate() const;
556 
557  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPreProcess(void *arg);
558 
559  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreaded(void *arg);
560 
561  static ITK_THREAD_RETURN_TYPE GetValueAndDerivativeMultiThreadedPostProcess(void *arg);
562 
563  virtual inline void GetValueAndDerivativeThread(ThreadIdType threadId) const;
564 
566  ThreadIdType itkNotUsed(threadId),
567  bool itkNotUsed(withinSampleThread) ) const
568  {}
570  ThreadIdType itkNotUsed(threadId),
571  SizeValueType itkNotUsed(fixedImageSample),
572  const MovingImagePointType & itkNotUsed(mappedPoint),
573  double itkNotUsed(movingImageValue),
574  const ImageDerivativesType & itkNotUsed(movingImageGradientValue) ) const
575  { return false; }
577  ThreadIdType itkNotUsed(threadId),
578  bool itkNotUsed(withinSampleThread) ) const
579  {}
580 
584  virtual void SynchronizeTransforms() const;
585 
586 private:
587  ITK_DISALLOW_COPY_AND_ASSIGN(ImageToImageMetric);
588 
590 };
591 } // end namespace itk
592 
593 #ifndef ITK_MANUAL_INSTANTIATION
594 #include "itkImageToImageMetric.hxx"
595 #endif
596 
597 #endif
virtual bool GetValueAndDerivativeThreadProcessSample(ThreadIdType, SizeValueType, const MovingImagePointType &, double, const ImageDerivativesType &) const
virtual void GetValueThreadPostProcess(ThreadIdType, bool) const
unsigned int * m_ThreaderNumberOfMovingImageSamples
TransformType::OutputPointType OutputPointType
virtual void GetValueThreadPreProcess(ThreadIdType, bool) const
BSplineTransformIndexArrayType m_BSplineTransformIndices
This class is a base for the CostFunctions returning a single value.
FixedImageMaskType::ConstPointer FixedImageMaskConstPointer
std::vector< FixedImageIndexType > FixedImageIndexContainer
BSplineTransformType::WeightsType BSplineTransformWeightsType
MovingImageMaskConstPointer m_MovingImageMask
Superclass::ParametersValueType CoordinateRepresentationType
TransformType::InputPointType InputPointType
virtual void GetValueAndDerivativeThreadPreProcess(ThreadIdType, bool) const
void SetNumberOfSpatialSamples(SizeValueType num)
signed long IndexValueType
Definition: itkIntTypes.h:150
Array2D< WeightsValueType > BSplineTransformWeightsArrayType
BSplineParametersOffsetType m_BSplineParametersOffset
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
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.
TransformType::OutputPointType MovingImagePointType
MultiThreaderParameterType m_ThreaderParameter
unsigned long SizeValueType
Definition: itkIntTypes.h:143
#define ITK_THREAD_RETURN_TYPE
MovingImageConstPointer m_MovingImage
const TransformPointer * GetThreaderTransform()
Simulate a standard C array with copy semnatics.
Definition: itkFixedArray.h:50
FixedImageSampleContainer m_FixedImageSamples
MovingImagePointArrayType m_BSplinePreTransformPointsArray
TMovingImage::PixelType MovingImagePixelType
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:45
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
A base class with common elements of BSplineTransform and BSplineDeformableTransform.
InterpolatorPointer m_Interpolator
BSplineTransformIndexArrayType::ValueType IndexValueType
GradientImagePointer m_GradientImage
MovingImageMaskType::ConstPointer MovingImageMaskConstPointer
virtual bool GetValueThreadProcessSample(ThreadIdType, SizeValueType, const MovingImagePointType &, double) const
BSplineTransformWeightsType * m_ThreaderBSplineTransformWeights
unsigned int ThreadIdType
Definition: itkIntTypes.h:159
std::vector< FixedImageSamplePoint > FixedImageSampleContainer
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
TransformType::JacobianType TransformJacobianType
FixedImageMaskConstPointer m_FixedImageMask
DerivativeFunctionType::Pointer m_DerivativeCalculator
FixedImageMaskType::Pointer FixedImageMaskPointer
virtual unsigned int GetNumberOfParameters(void) const override
FixedImageRegionType m_FixedImageRegion
MovingImageMaskType::Pointer MovingImageMaskPointer
FixedImagePixelType m_FixedImageSamplesIntensityThreshold
BSplineTransformWeightsType::ValueType WeightsValueType
InterpolateImageFunction< MovingImageType, CoordinateRepresentationType > InterpolatorType
Control indentation during Print() invocation.
Definition: itkIndent.h:49
FixedImageType::ConstPointer FixedImageConstPointer
TFixedImage::PixelType FixedImagePixelType
TransformType::InputPointType FixedImagePointType
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
Superclass::MeasureType MeasureType
CovariantVector< RealType, itkGetStaticConstMacro(MovingImageDimension) > GradientPixelType
Transform< CoordinateRepresentationType, itkGetStaticConstMacro(MovingImageDimension), itkGetStaticConstMacro(FixedImageDimension) > TransformType
Computes similarity between regions of two images.
MovingImageType::IndexType MovingImageIndexType
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:52
A templated class holding a n-Dimensional covariant vector.
TransformType::ParametersType TransformParametersType
GradientRecursiveGaussianImageFilter< MovingImageType, GradientImageType > GradientImageFilterType
Templated n-dimensional image class.
Definition: itkImage.h:75
SmartPointer< Self > Pointer
MultiThreaderType::Pointer m_Threader
FixedImageType::RegionType FixedImageRegionType
std::vector< bool > BooleanArrayType
BSplineTransformType::ParameterIndexArrayType BSplineTransformIndexArrayType
GradientImageFilterType::Pointer GradientImageFilterPointer
SizeValueType m_NumberOfFixedImageSamples