ITK  6.0.0
Insight Toolkit
itkImageToImageMetric.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
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 * https://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"
28
29#include <memory> // For unique_ptr.
30
31namespace itk
32{
53template <typename TFixedImage, typename TMovingImage>
54class ITK_TEMPLATE_EXPORT ImageToImageMetric : public SingleValuedCostFunction
55{
56public:
57 ITK_DISALLOW_COPY_AND_MOVE(ImageToImageMetric);
58
64
66 using CoordinateRepresentationType = typename Superclass::ParametersValueType;
67
69 itkOverrideGetNameOfClassMacro(ImageToImageMetric);
70
72 using MovingImageType = TMovingImage;
73 using MovingImagePixelType = typename TMovingImage::PixelType;
75
77 using FixedImageType = TFixedImage;
78 using FixedImagePixelType = typename TFixedImage::PixelType;
81
83 static constexpr unsigned int MovingImageDimension = TMovingImage::ImageDimension;
84 static constexpr unsigned int FixedImageDimension = TFixedImage::ImageDimension;
85
88
94
101
102 using FixedImageIndexContainer = std::vector<FixedImageIndexType>;
103
106
114
116
122
128
130 using typename Superclass::MeasureType;
131
133 using typename Superclass::DerivativeType;
134
136 using typename Superclass::ParametersType;
137
139 itkSetConstObjectMacro(FixedImage, FixedImageType);
140 itkGetConstObjectMacro(FixedImage, FixedImageType);
144 itkSetConstObjectMacro(MovingImage, MovingImageType);
145 itkGetConstObjectMacro(MovingImage, MovingImageType);
149 itkSetObjectMacro(Transform, TransformType);
150
152 itkGetModifiableObjectMacro(Transform, TransformType);
153
155 itkSetObjectMacro(Interpolator, InterpolatorType);
156
158 itkGetModifiableObjectMacro(Interpolator, InterpolatorType);
159
163 {
164 return this->GetNumberOfPixelsCounted();
165 }
166
167 itkGetConstReferenceMacro(NumberOfPixelsCounted, SizeValueType);
168
170 virtual void
172
174 itkGetConstReferenceMacro(FixedImageRegion, FixedImageRegionType);
175
177#ifndef ITK_FUTURE_LEGACY_REMOVE
178 virtual void
179 SetMovingImageMask(MovingImageMaskType * const arg)
180 {
181 const auto * const constArg = arg;
182 // Call the overload defined by itkSetConstObjectMacro, or an override.
183 this->SetMovingImageMask(constArg);
184 }
185#endif
186 itkSetConstObjectMacro(MovingImageMask, MovingImageMaskType);
187 itkGetConstObjectMacro(MovingImageMask, MovingImageMaskType);
191#ifndef ITK_FUTURE_LEGACY_REMOVE
192 virtual void
193 SetFixedImageMask(FixedImageMaskType * const arg)
194 {
195 const auto * const constArg = arg;
196 // Call the overload defined by itkSetConstObjectMacro, or an override.
197 this->SetFixedImageMask(constArg);
198 }
199#endif
200 itkSetConstObjectMacro(FixedImageMask, FixedImageMaskType);
201 itkGetConstObjectMacro(FixedImageMask, FixedImageMaskType);
206 void
208
209 void
210 SetUseFixedImageIndexes(bool useIndexes);
211
212 itkGetConstReferenceMacro(UseFixedImageIndexes, bool);
213
215 void
217 itkGetConstReferenceMacro(NumberOfWorkUnits, ThreadIdType);
221 itkSetMacro(ComputeGradient, bool);
222 itkGetConstReferenceMacro(ComputeGradient, bool);
223 itkBooleanMacro(ComputeGradient);
227 virtual void
229
231 itkGetModifiableObjectMacro(GradientImage, GradientImageType);
232
234 void
235 SetTransformParameters(const ParametersType & parameters) const;
236
238 unsigned int
239 GetNumberOfParameters() const override
240 {
241 return m_Transform->GetNumberOfParameters();
242 }
243
245 virtual void
247
249 virtual void
251
256 virtual void
258 itkGetConstReferenceMacro(NumberOfFixedImageSamples, SizeValueType);
263 void
265 {
266 this->SetNumberOfFixedImageSamples(num);
267 }
268
271 {
272 return this->GetNumberOfFixedImageSamples();
273 }
274
277 void
279
280 itkGetConstReferenceMacro(FixedImageSamplesIntensityThreshold, FixedImagePixelType);
281
282 void
284
285 itkGetConstReferenceMacro(UseFixedImageSamplesIntensityThreshold, bool);
286
290 void
291 SetUseAllPixels(bool useAllPixels);
292
293 void
295 {
296 this->SetUseAllPixels(true);
297 }
298
299 void
301 {
302 this->SetUseAllPixels(false);
303 }
304
305 itkGetConstReferenceMacro(UseAllPixels, bool);
306
311 void
312 SetUseSequentialSampling(bool useSequential);
313
314 itkGetConstReferenceMacro(UseSequentialSampling, bool);
315
325 void
327 void
347 itkSetMacro(UseCachingOfBSplineWeights, bool);
348 itkGetConstReferenceMacro(UseCachingOfBSplineWeights, bool);
349 itkBooleanMacro(UseCachingOfBSplineWeights);
354 itkGetModifiableObjectMacro(Threader, MultiThreaderType);
355 const TransformPointer *
357 {
358 return m_ThreaderTransform.get();
359 }
362protected:
364 ~ImageToImageMetric() override = default;
365
366 void
367 PrintSelf(std::ostream & os, Indent indent) const override;
368
375 {
376 public:
378 {
379 point.Fill(0.0);
380 value = 0;
381 valueIndex = 0;
382 }
383
385
386 inline friend std::ostream &
387 operator<<(std::ostream & os, const FixedImageSamplePoint & val)
388 {
389 os << "point: " << static_cast<typename NumericTraits<FixedImagePointType>::PrintType>(val.point) << std::endl;
390 os << "value: " << val.value << std::endl;
391 os << "valueIndex: " << val.valueIndex << std::endl;
392
393 return os;
394 }
395
396 public:
398 double value;
399 unsigned int valueIndex;
400 };
401
402 bool m_UseFixedImageIndexes{ false };
403 FixedImageIndexContainer m_FixedImageIndexes{};
404
405 bool m_UseFixedImageSamplesIntensityThreshold{ false };
406 FixedImagePixelType m_FixedImageSamplesIntensityThreshold{};
407
409 using FixedImageSampleContainer = std::vector<FixedImageSamplePoint>;
410
415 virtual void
417
419 virtual void
421
426 virtual void
428
430 FixedImageSampleContainer m_FixedImageSamples{};
431
432 SizeValueType m_NumberOfParameters{ 0 };
433
434 SizeValueType m_NumberOfFixedImageSamples{ 50000 };
435 // m_NumberOfPixelsCounted must be mutable because the const
436 // thread consolidation functions merge each work unit's values
437 // onto this accumulator variable.
438 mutable SizeValueType m_NumberOfPixelsCounted{ 0 };
439
441 MovingImageConstPointer m_MovingImage{};
442
444 TransformPointer m_Transform{};
445
448 std::unique_ptr<TransformPointer[]> m_ThreaderTransform;
449
450 InterpolatorPointer m_Interpolator{};
451
452 bool m_ComputeGradient{ true };
453 GradientImagePointer m_GradientImage{};
454
455 FixedImageMaskConstPointer m_FixedImageMask{};
456 MovingImageMaskConstPointer m_MovingImageMask{};
457
458 ThreadIdType m_NumberOfWorkUnits{ 1 };
459
460 bool m_UseAllPixels{ false };
461 bool m_UseSequentialSampling{ false };
462
463 bool m_ReseedIterator{ false };
464
465 mutable int m_RandomSeed{};
466
473#ifndef ITK_FUTURE_LEGACY_REMOVE
477 bool m_TransformIsBSpline{ false };
478#endif
479
482 SizeValueType m_NumBSplineWeights{ 0 };
483
484 static constexpr unsigned int DeformationSplineOrder = 3;
485
488
490 using WeightsValueType = typename BSplineTransformWeightsType::ValueType;
492
494 using IndexValueType = typename BSplineTransformIndexArrayType::ValueType;
496
497 using MovingImagePointArrayType = std::vector<MovingImagePointType>;
498 using BooleanArrayType = std::vector<bool>;
506
510
511 typename BSplineTransformType::Pointer m_BSplineTransform{};
512
513 BSplineTransformWeightsArrayType m_BSplineTransformWeightsArray{};
514 BSplineTransformIndicesArrayType m_BSplineTransformIndicesArray{};
515 MovingImagePointArrayType m_BSplinePreTransformPointsArray{};
516 BooleanArrayType m_WithinBSplineSupportRegionArray{};
517
518 BSplineParametersOffsetType m_BSplineParametersOffset{};
519
520 // Variables needed for optionally caching values when using a BSpline
521 // transform.
522 bool m_UseCachingOfBSplineWeights{ true };
523 mutable BSplineTransformWeightsType m_BSplineTransformWeights{};
524 mutable BSplineTransformIndexArrayType m_BSplineTransformIndices{};
525
526 mutable std::unique_ptr<BSplineTransformWeightsType[]> m_ThreaderBSplineTransformWeights;
527 mutable std::unique_ptr<BSplineTransformIndexArrayType[]> m_ThreaderBSplineTransformIndices;
528
530 virtual void
532
537 virtual void
538 TransformPoint(unsigned int sampleNumber,
539 MovingImagePointType & mappedPoint,
540 bool & sampleOk,
541 double & movingImageValue,
542 ThreadIdType threadId) const;
543
548 virtual void
549 TransformPointWithDerivatives(unsigned int sampleNumber,
550 MovingImagePointType & mappedPoint,
551 bool & sampleOk,
552 double & movingImageValue,
553 ImageDerivativesType & movingImageGradient,
554 ThreadIdType threadId) const;
555
556#ifndef ITK_FUTURE_LEGACY_REMOVE
560 bool m_InterpolatorIsBSpline{ false };
561#endif
562
564 typename BSplineInterpolatorType::Pointer m_BSplineInterpolator{};
565
567 typename DerivativeFunctionType::Pointer m_DerivativeCalculator{};
568
572 virtual void
574 ImageDerivativesType & gradient,
575 ThreadIdType threadId) const;
576
590 {
591 public:
593 : m_ConstMetricPointer{ i2i_metricPointer }
594 {}
595 const ImageToImageMetric *
597 {
598 return m_ConstMetricPointer;
599 }
602 private:
604 };
605
614 {
615 public:
617 : m_WorkUnitInfo(static_cast<const typename MultiThreaderType::WorkUnitInfo *>(workunitInfoAsVoid))
618 {}
621 {
622 return m_WorkUnitInfo->WorkUnitID;
623 }
624 const ImageToImageMetric *
626 {
627 return (static_cast<ConstantPointerWrapper *>(m_WorkUnitInfo->UserData))->GetConstMetricPointer();
628 }
631 private:
633 };
634
636 std::unique_ptr<ConstantPointerWrapper> m_ConstSelfWrapper;
637 mutable std::unique_ptr<unsigned int[]> m_ThreaderNumberOfMovingImageSamples;
638 bool m_WithinThreadPreProcess{ false };
639 bool m_WithinThreadPostProcess{ false };
640
641 void
643
644 void
646
649 GetValueMultiThreaded(void * workunitInfoAsVoid);
650
653 GetValueMultiThreadedPostProcess(void * workunitInfoAsVoid);
654
656 virtual inline void
658
660 virtual inline void
661 GetValueThreadPreProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
662 {}
663 virtual inline bool
665 SizeValueType itkNotUsed(fixedImageSample),
666 const MovingImagePointType & itkNotUsed(mappedPoint),
667 double itkNotUsed(movingImageValue)) const
668 {
669 return false;
670 }
671 virtual inline void
672 GetValueThreadPostProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
673 {}
676 void
678
679 void
681
683 GetValueAndDerivativeMultiThreaded(void * workunitInfoAsVoid);
684
687
688 virtual inline void
690
691 virtual inline void
692 GetValueAndDerivativeThreadPreProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
693 {}
694 virtual inline bool
696 SizeValueType itkNotUsed(fixedImageSample),
697 const MovingImagePointType & itkNotUsed(mappedPoint),
698 double itkNotUsed(movingImageValue),
699 const ImageDerivativesType & itkNotUsed(movingImageGradientValue)) const
700 {
701 return false;
702 }
703 virtual inline void
704 GetValueAndDerivativeThreadPostProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
705 {}
706
712 virtual void
714
715private:
716 FixedImageRegionType m_FixedImageRegion{};
717};
718} // end namespace itk
719
720#ifndef ITK_MANUAL_INSTANTIATION
721# include "itkImageToImageMetric.hxx"
722#endif
723
724#endif
Array2D class representing a 2D array.
Definition: itkArray2D.h:43
A base class with common elements of BSplineTransform and BSplineDeformableTransform.
typename WeightsFunctionType::WeightsType WeightsType
Evaluates the B-Spline interpolation of an image. Spline order may be from 0 to 5.
Calculate the derivative by central differencing.
A templated class holding a n-Dimensional covariant vector.
Simulate a standard C array with copy semantics.
Definition: itkFixedArray.h:54
Computes the gradient of an image by convolution with the first derivative of a Gaussian.
const ImageToImageMetric * GetConstMetricPointer() const
ConstantPointerWrapper(ImageToImageMetric *i2i_metricPointer)
Computes similarity between regions of two images.
typename FixedImageType::ConstPointer FixedImageConstPointer
typename NumericTraits< MovingImagePixelType >::RealType RealType
typename TransformType::Pointer TransformPointer
typename TFixedImage::PixelType FixedImagePixelType
void ReinitializeSeed(int seed)
virtual void SetNumberOfFixedImageSamples(SizeValueType numSamples)
static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION GetValueAndDerivativeMultiThreaded(void *workunitInfoAsVoid)
typename FixedImageType::RegionType FixedImageRegionType
void SetTransformParameters(const ParametersType &parameters) const
std::vector< FixedImageIndexType > FixedImageIndexContainer
typename BSplineTransformWeightsType::ValueType WeightsValueType
typename FixedImageIndexType::IndexValueType FixedImageIndexValueType
virtual void GetValueAndDerivativeThreadPostProcess(ThreadIdType, bool) const
typename MovingImageType::ConstPointer MovingImageConstPointer
void GetValueMultiThreadedPostProcessInitiate() const
std::unique_ptr< ConstantPointerWrapper > m_ConstSelfWrapper
typename MovingImageMaskType::ConstPointer MovingImageMaskConstPointer
typename FixedImageType::IndexType FixedImageIndexType
virtual void TransformPointWithDerivatives(unsigned int sampleNumber, MovingImagePointType &mappedPoint, bool &sampleOk, double &movingImageValue, ImageDerivativesType &movingImageGradient, ThreadIdType threadId) const
static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION GetValueMultiThreadedPostProcess(void *workunitInfoAsVoid)
typename GradientImageFilterType::Pointer GradientImageFilterPointer
typename BSplineTransformType::ParameterIndexArrayType BSplineTransformIndexArrayType
typename MovingImageMaskType::Pointer MovingImageMaskPointer
const TransformPointer * GetThreaderTransform()
virtual bool GetValueAndDerivativeThreadProcessSample(ThreadIdType, SizeValueType, const MovingImagePointType &, double, const ImageDerivativesType &) const
std::vector< MovingImagePointType > MovingImagePointArrayType
void SetNumberOfSpatialSamples(SizeValueType num)
virtual void ComputeImageDerivatives(const MovingImagePointType &mappedPoint, ImageDerivativesType &gradient, ThreadIdType threadId) const
void GetValueMultiThreadedInitiate() const
virtual void SampleFixedImageRegion(FixedImageSampleContainer &samples) const
void PrintSelf(std::ostream &os, Indent indent) const override
std::vector< bool > BooleanArrayType
virtual bool GetValueThreadProcessSample(ThreadIdType, SizeValueType, const MovingImagePointType &, double) const
SizeValueType GetNumberOfMovingImageSamples()
typename BSplineTransformIndexArrayType::ValueType IndexValueType
virtual void GetValueThread(ThreadIdType threadId) const
virtual void GetValueThreadPostProcess(ThreadIdType, bool) const
typename BSplineTransformType::WeightsType BSplineTransformWeightsType
typename TransformType::ParametersType TransformParametersType
typename TransformType::OutputPointType MovingImagePointType
void GetValueAndDerivativeMultiThreadedPostProcessInitiate() const
typename TransformType::InputPointType FixedImagePointType
std::unique_ptr< BSplineTransformWeightsType[]> m_ThreaderBSplineTransformWeights
std::unique_ptr< BSplineTransformIndexArrayType[]> m_ThreaderBSplineTransformIndices
virtual void SynchronizeTransforms() const
void SetUseSequentialSampling(bool useSequential)
typename Superclass::ParametersValueType CoordinateRepresentationType
std::unique_ptr< unsigned int[]> m_ThreaderNumberOfMovingImageSamples
void GetValueAndDerivativeMultiThreadedInitiate() const
typename FixedImageMaskType::ConstPointer FixedImageMaskConstPointer
unsigned int GetNumberOfParameters() const override
SizeValueType GetNumberOfSpatialSamples()
void SetUseAllPixels(bool useAllPixels)
void SetNumberOfWorkUnits(ThreadIdType numberOfThreads)
typename FixedImageMaskType::Pointer FixedImageMaskPointer
static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION GetValueAndDerivativeMultiThreadedPostProcess(void *workunitInfoAsVoid)
virtual void MultiThreadingInitialize()
virtual void Initialize()
virtual void GetValueThreadPreProcess(ThreadIdType, bool) const
typename TMovingImage::PixelType MovingImagePixelType
virtual void SampleFixedImageIndexes(FixedImageSampleContainer &samples) const
~ImageToImageMetric() override=default
std::vector< FixedImageSamplePoint > FixedImageSampleContainer
void SetUseFixedImageIndexes(bool useIndexes)
std::unique_ptr< TransformPointer[]> m_ThreaderTransform
virtual void TransformPoint(unsigned int sampleNumber, MovingImagePointType &mappedPoint, bool &sampleOk, double &movingImageValue, ThreadIdType threadId) const
static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION GetValueMultiThreaded(void *workunitInfoAsVoid)
virtual void SampleFullFixedImageRegion(FixedImageSampleContainer &samples) const
void SetFixedImageSamplesIntensityThreshold(const FixedImagePixelType &thresh)
void SetUseFixedImageSamplesIntensityThreshold(bool useThresh)
void SetFixedImageIndexes(const FixedImageIndexContainer &indexes)
typename MovingImageType::IndexType MovingImageIndexType
virtual void GetValueAndDerivativeThreadPreProcess(ThreadIdType, bool) const
typename TransformType::OutputPointType OutputPointType
virtual void GetValueAndDerivativeThread(ThreadIdType threadId) const
virtual void SetFixedImageRegion(const FixedImageRegionType reg)
typename InterpolatorType::Pointer InterpolatorPointer
typename TransformType::JacobianType TransformJacobianType
virtual void ComputeGradient()
virtual void PreComputeTransformValues()
typename TransformType::InputPointType InputPointType
Templated n-dimensional image class.
Definition: itkImage.h:89
Control indentation during Print() invocation.
Definition: itkIndent.h:50
Base class for all image interpolators.
Light weight base class for most itk classes.
A class for performing multithreaded execution.
Class to hold and manage different parameter types used during optimization.
A templated class holding a geometric point in n-Dimensional space.
Definition: itkPoint.h:54
This class is a base for the CostFunctions returning a single value.
Superclass::ParametersType ParametersType
Implementation of the composite pattern.
Transform points and vectors from an input space to an output space.
Definition: itkTransform.h:84
SmartPointer< const Self > ConstPointer
SmartPointer< Self > Pointer
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
*par Constraints *The filter image with at least two dimensions and a vector *length of at least The theory supports extension to scalar but *the implementation of the itk vector classes do not **The template parameter TRealType must be floating point(float or double) or *a user-defined "real" numerical type with arithmetic operations defined *sufficient to compute derivatives. **\par Performance *This filter will automatically multithread if run with *SetUsePrincipleComponents
ITKCommon_EXPORT std::ostream & operator<<(std::ostream &out, typename AnatomicalOrientation::CoordinateEnum value)
unsigned int ThreadIdType
Definition: itkIntTypes.h:102
long IndexValueType
Definition: itkIntTypes.h:93
unsigned long SizeValueType
Definition: itkIntTypes.h:86
itk::ITK_THREAD_RETURN_TYPE ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION