ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
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{
52
53template <typename TFixedImage, typename TMovingImage>
54class ITK_TEMPLATE_EXPORT ImageToImageMetric : public SingleValuedCostFunction
56public:
57 ITK_DISALLOW_COPY_AND_MOVE(ImageToImageMetric);
67
69 itkOverrideGetNameOfClassMacro(ImageToImageMetric);
70
72 using MovingImageType = TMovingImage;
73 using MovingImagePixelType = typename TMovingImage::PixelType;
74 using MovingImageConstPointer = typename MovingImageType::ConstPointer;
75
77 using FixedImageType = TFixedImage;
78 using FixedImagePixelType = typename TFixedImage::PixelType;
79 using FixedImageConstPointer = typename FixedImageType::ConstPointer;
80 using FixedImageRegionType = typename FixedImageType::RegionType;
81
83 static constexpr unsigned int MovingImageDimension = TMovingImage::ImageDimension;
84 static constexpr unsigned int FixedImageDimension = TFixedImage::ImageDimension;
85
88
94
96 using FixedImageIndexType = typename FixedImageType::IndexType;
97 using FixedImageIndexValueType = typename FixedImageIndexType::IndexValueType;
98 using MovingImageIndexType = typename MovingImageType::IndexType;
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
140 itkSetConstObjectMacro(FixedImage, FixedImageType);
141 itkGetConstObjectMacro(FixedImage, FixedImageType);
145 itkSetConstObjectMacro(MovingImage, MovingImageType);
146 itkGetConstObjectMacro(MovingImage, MovingImageType);
149 itkSetObjectMacro(Transform, TransformType);
150
152 itkGetModifiableObjectMacro(Transform, TransformType);
153
155 itkSetObjectMacro(Interpolator, InterpolatorType);
156
158 itkGetModifiableObjectMacro(Interpolator, InterpolatorType);
159
166
167 itkGetConstReferenceMacro(NumberOfPixelsCounted, SizeValueType);
168
170 virtual void
172
174 itkGetConstReferenceMacro(FixedImageRegion, FixedImageRegionType);
175
178#ifndef ITK_FUTURE_LEGACY_REMOVE
179 virtual void
181 {
182 const auto * const constArg = arg;
183 // Call the overload defined by itkSetConstObjectMacro, or an override.
184 this->SetMovingImageMask(constArg);
185 }
186#endif
187 itkSetConstObjectMacro(MovingImageMask, MovingImageMaskType);
188 itkGetConstObjectMacro(MovingImageMask, MovingImageMaskType);
192#ifndef ITK_FUTURE_LEGACY_REMOVE
193 virtual void
195 {
196 const auto * const constArg = arg;
197 // Call the overload defined by itkSetConstObjectMacro, or an override.
198 this->SetFixedImageMask(constArg);
199 }
200#endif
201 itkSetConstObjectMacro(FixedImageMask, FixedImageMaskType);
202 itkGetConstObjectMacro(FixedImageMask, FixedImageMaskType);
206 void
208
209 void
210 SetUseFixedImageIndexes(bool useIndexes);
211
212 itkGetConstReferenceMacro(UseFixedImageIndexes, bool);
213
216 void
218 itkGetConstReferenceMacro(NumberOfWorkUnits, ThreadIdType);
222 itkSetMacro(ComputeGradient, bool);
223 itkGetConstReferenceMacro(ComputeGradient, bool);
224 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
257 virtual void
259 itkGetConstReferenceMacro(NumberOfFixedImageSamples, SizeValueType);
263 void
268
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
326 void
328 void
348 itkSetMacro(UseCachingOfBSplineWeights, bool);
349 itkGetConstReferenceMacro(UseCachingOfBSplineWeights, bool);
350 itkBooleanMacro(UseCachingOfBSplineWeights);
352
353#ifndef ITK_FUTURE_LEGACY_REMOVE
354 using MultiThreaderType [[deprecated("Please use `itk::MultiThreaderBase` directly.")]] = MultiThreaderBase;
355#endif
356
359 itkGetModifiableObjectMacro(Threader, MultiThreaderBase);
360 const TransformPointer *
362 {
363 return m_ThreaderTransform.get();
364 }
365
366protected:
368 ~ImageToImageMetric() override = default;
369
370 void
371 PrintSelf(std::ostream & os, Indent indent) const override;
372
379 {
380 public:
382
384
385 friend inline std::ostream &
386 operator<<(std::ostream & os, const FixedImageSamplePoint & val)
387 {
388 os << "point: " << static_cast<typename NumericTraits<FixedImagePointType>::PrintType>(val.point) << std::endl;
389 os << "value: " << val.value << std::endl;
390 os << "valueIndex: " << val.valueIndex << std::endl;
391
392 return os;
393 }
394
395 public:
397 double value{ 0 };
398 unsigned int valueIndex{ 0 };
399 };
400
403
406
408 using FixedImageSampleContainer = std::vector<FixedImageSamplePoint>;
409
414 virtual void
416
418 virtual void
420
425 virtual void
427
430
432
434 // m_NumberOfPixelsCounted must be mutable because the const
435 // thread consolidation functions merge each work unit's values
436 // onto this accumulator variable.
438
441
444
447 std::unique_ptr<TransformPointer[]> m_ThreaderTransform;
448
450
451 bool m_ComputeGradient{ true };
453
456
458
459 bool m_UseAllPixels{ false };
461
462 bool m_ReseedIterator{ false };
463
464 mutable int m_RandomSeed{};
465
471
472#ifndef ITK_FUTURE_LEGACY_REMOVE
476 bool m_TransformIsBSpline{ false };
477#endif
478
482
483 static constexpr unsigned int DeformationSplineOrder = 3;
484
487
489 using WeightsValueType = typename BSplineTransformWeightsType::ValueType;
491
493 using IndexValueType = typename BSplineTransformIndexArrayType::ValueType;
495
496 using MovingImagePointArrayType = std::vector<MovingImagePointType>;
497 using BooleanArrayType = std::vector<bool>;
505
509
511
516
518
519 // Variables needed for optionally caching values when using a BSpline
520 // transform.
524
525 mutable std::unique_ptr<BSplineTransformWeightsType[]> m_ThreaderBSplineTransformWeights;
526 mutable std::unique_ptr<BSplineTransformIndexArrayType[]> m_ThreaderBSplineTransformIndices;
527
529 virtual void
531
536 virtual void
537 TransformPoint(unsigned int sampleNumber,
538 MovingImagePointType & mappedPoint,
539 bool & sampleOk,
540 double & movingImageValue,
541 ThreadIdType threadId) const;
542
547 virtual void
548 TransformPointWithDerivatives(unsigned int sampleNumber,
549 MovingImagePointType & mappedPoint,
550 bool & sampleOk,
551 double & movingImageValue,
552 ImageDerivativesType & movingImageGradient,
553 ThreadIdType threadId) const;
554
555#ifndef ITK_FUTURE_LEGACY_REMOVE
559 bool m_InterpolatorIsBSpline{ false };
560#endif
561
564
567
571 virtual void
573 ImageDerivativesType & gradient,
574 ThreadIdType threadId) const;
575
579
589 {
590 public:
592 : m_ConstMetricPointer{ i2i_metricPointer }
593 {}
594 [[nodiscard]] const ImageToImageMetric *
596 {
598 }
599
600 private:
602 };
603
612 {
613 public:
615 : m_WorkUnitInfo(static_cast<const MultiThreaderBase::WorkUnitInfo *>(workunitInfoAsVoid))
616 {}
617 [[nodiscard]] ThreadIdType
619 {
620 return m_WorkUnitInfo->WorkUnitID;
621 }
622 [[nodiscard]] const ImageToImageMetric *
624 {
625 return (static_cast<ConstantPointerWrapper *>(m_WorkUnitInfo->UserData))->GetConstMetricPointer();
626 }
627
628 private:
630 };
631
633 std::unique_ptr<ConstantPointerWrapper> m_ConstSelfWrapper;
634 mutable std::unique_ptr<unsigned int[]> m_ThreaderNumberOfMovingImageSamples;
637
638 void
640
641 void
643
646 GetValueMultiThreaded(void * workunitInfoAsVoid);
647
650 GetValueMultiThreadedPostProcess(void * workunitInfoAsVoid);
651
653 virtual inline void
655
658 virtual inline void
659 GetValueThreadPreProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
660 {}
661 virtual inline bool
663 SizeValueType itkNotUsed(fixedImageSample),
664 const MovingImagePointType & itkNotUsed(mappedPoint),
665 double itkNotUsed(movingImageValue)) const
666 {
667 return false;
668 }
669 virtual inline void
670 GetValueThreadPostProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
671 {}
672
673 void
675
676 void
678
680 GetValueAndDerivativeMultiThreaded(void * workunitInfoAsVoid);
681
684
685 virtual inline void
687
688 virtual inline void
689 GetValueAndDerivativeThreadPreProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
690 {}
691 virtual inline bool
693 SizeValueType itkNotUsed(fixedImageSample),
694 const MovingImagePointType & itkNotUsed(mappedPoint),
695 double itkNotUsed(movingImageValue),
696 const ImageDerivativesType & itkNotUsed(movingImageGradientValue)) const
697 {
698 return false;
699 }
700 virtual inline void
701 GetValueAndDerivativeThreadPostProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
702 {}
703
709 virtual void
711
712private:
714};
715} // end namespace itk
716
717#ifndef ITK_MANUAL_INSTANTIATION
718# include "itkImageToImageMetric.hxx"
719#endif
720
721#endif
Array2D class representing a 2D array.
Definition itkArray2D.h:43
A base class with common elements of BSplineTransform and BSplineDeformableTransform.
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.
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 NumericTraits< MovingImagePixelType >::RealType RealType
virtual const SizeValueType & GetNumberOfPixelsCounted() const
virtual void SetFixedImageMask(const FixedImageMaskType *_arg)
void ReinitializeSeed(int seed)
virtual void GetValueThreadPostProcess(ThreadIdType threadId, bool withinSampleThread) const
virtual void SetNumberOfFixedImageSamples(SizeValueType numSamples)
virtual bool GetValueAndDerivativeThreadProcessSample(ThreadIdType threadId, SizeValueType fixedImageSample, const MovingImagePointType &mappedPoint, double movingImageValue, const ImageDerivativesType &movingImageGradientValue) const
static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION GetValueAndDerivativeMultiThreaded(void *workunitInfoAsVoid)
void SetTransformParameters(const ParametersType &parameters) const
void SetNumberOfWorkUnits(ThreadIdType numberOfWorkUnits)
typename BSplineTransformWeightsType::ValueType WeightsValueType
typename FixedImageIndexType::IndexValueType FixedImageIndexValueType
BSplineInterpolateImageFunction< MovingImageType, CoordinateRepresentationType > BSplineInterpolatorType
typename MovingImageType::ConstPointer MovingImageConstPointer
void GetValueMultiThreadedPostProcessInitiate() const
std::unique_ptr< ConstantPointerWrapper > m_ConstSelfWrapper
typename MovingImageMaskType::ConstPointer MovingImageMaskConstPointer
SpatialObject< Self::FixedImageDimension > FixedImageMaskType
BSplineBaseTransform< CoordinateRepresentationType, FixedImageType::ImageDimension, Self::DeformationSplineOrder > BSplineTransformType
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)
virtual void SetMovingImageMask(const MovingImageMaskType *_arg)
typename GradientImageFilterType::Pointer GradientImageFilterPointer
typename BSplineTransformType::ParameterIndexArrayType BSplineTransformIndexArrayType
const TransformPointer * GetThreaderTransform()
CovariantVector< double, Self::MovingImageDimension > ImageDerivativesType
virtual bool GetValueThreadProcessSample(ThreadIdType threadId, SizeValueType fixedImageSample, const MovingImagePointType &mappedPoint, double movingImageValue) const
virtual void GetValueAndDerivativeThreadPostProcess(ThreadIdType threadId, bool withinSampleThread) const
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
SizeValueType GetNumberOfMovingImageSamples()
virtual void GetValueAndDerivativeThreadPreProcess(ThreadIdType threadId, bool withinSampleThread) const
typename BSplineTransformIndexArrayType::ValueType IndexValueType
virtual void GetValueThread(ThreadIdType threadId) const
typename BSplineTransformType::WeightsType BSplineTransformWeightsType
typename TransformType::ParametersType TransformParametersType
void GetValueAndDerivativeMultiThreadedPostProcessInitiate() const
Image< GradientPixelType, Self::MovingImageDimension > GradientImageType
std::unique_ptr< BSplineTransformWeightsType[]> m_ThreaderBSplineTransformWeights
std::unique_ptr< BSplineTransformIndexArrayType[]> m_ThreaderBSplineTransformIndices
InterpolateImageFunction< MovingImageType, CoordinateRepresentationType > InterpolatorType
virtual void SynchronizeTransforms() const
void SetUseSequentialSampling(bool useSequential)
typename Superclass::ParametersValueType CoordinateRepresentationType
void GetValueAndDerivativeMultiThreadedInitiate() const
typename FixedImageMaskType::ConstPointer FixedImageMaskConstPointer
unsigned int GetNumberOfParameters() const override
CovariantVector< RealType, Self::MovingImageDimension > GradientPixelType
SizeValueType GetNumberOfSpatialSamples()
FixedArray< SizeValueType, FixedImageType::ImageDimension > BSplineParametersOffsetType
void SetUseAllPixels(bool useAllPixels)
static ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION GetValueAndDerivativeMultiThreadedPostProcess(void *workunitInfoAsVoid)
virtual void MultiThreadingInitialize()
virtual void Initialize()
virtual void SampleFixedImageIndexes(FixedImageSampleContainer &samples) const
~ImageToImageMetric() override=default
Transform< CoordinateRepresentationType, Self::MovingImageDimension, Self::FixedImageDimension > TransformType
void SetUseFixedImageIndexes(bool useIndexes)
virtual void TransformPoint(unsigned int sampleNumber, MovingImagePointType &mappedPoint, bool &sampleOk, double &movingImageValue, ThreadIdType threadId) const
virtual void GetValueThreadPreProcess(ThreadIdType threadId, bool withinSampleThread) const
virtual const SizeValueType & GetNumberOfFixedImageSamples() 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)
CentralDifferenceImageFunction< MovingImageType, CoordinateRepresentationType > DerivativeFunctionType
virtual void GetValueAndDerivativeThread(ThreadIdType threadId) const
virtual void SetFixedImageRegion(const FixedImageRegionType reg)
SpatialObject< Self::MovingImageDimension > MovingImageMaskType
GradientRecursiveGaussianImageFilter< MovingImageType, GradientImageType > GradientImageFilterType
virtual void PreComputeTransformValues()
Templated n-dimensional image class.
Definition itkImage.h:89
Control indentation during Print() invocation.
Definition itkIndent.h:50
Base class for all image interpolators.
A class for performing multithreaded execution.
SmartPointer< Self > Pointer
Array< ParametersValueType > DerivativeType
Superclass::ParametersValueType ParametersValueType
Superclass::ParametersType ParametersType
Implements transparent reference counting.
Implementation of the composite pattern.
Transform points and vectors from an input space to an output space.
std::ostream & operator<<(std::ostream &os, const itk::DOMNode &object)
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned int ThreadIdType
unsigned long SizeValueType
Definition itkIntTypes.h:86
itk::ITK_THREAD_RETURN_TYPE ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION