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);
355 itkGetModifiableObjectMacro(Threader, MultiThreaderType);
356 const TransformPointer *
358 {
359 return m_ThreaderTransform.get();
360 }
361
362protected:
364 ~ImageToImageMetric() override = default;
365
366 void
367 PrintSelf(std::ostream & os, Indent indent) const override;
368
375 {
376 public:
378
380
381 friend inline std::ostream &
382 operator<<(std::ostream & os, const FixedImageSamplePoint & val)
383 {
384 os << "point: " << static_cast<typename NumericTraits<FixedImagePointType>::PrintType>(val.point) << std::endl;
385 os << "value: " << val.value << std::endl;
386 os << "valueIndex: " << val.valueIndex << std::endl;
387
388 return os;
389 }
390
391 public:
393 double value{ 0 };
394 unsigned int valueIndex{ 0 };
395 };
396
399
402
404 using FixedImageSampleContainer = std::vector<FixedImageSamplePoint>;
405
410 virtual void
412
414 virtual void
416
421 virtual void
423
426
428
430 // m_NumberOfPixelsCounted must be mutable because the const
431 // thread consolidation functions merge each work unit's values
432 // onto this accumulator variable.
434
437
440
443 std::unique_ptr<TransformPointer[]> m_ThreaderTransform;
444
446
447 bool m_ComputeGradient{ true };
449
452
454
455 bool m_UseAllPixels{ false };
457
458 bool m_ReseedIterator{ false };
459
460 mutable int m_RandomSeed{};
461
467
468#ifndef ITK_FUTURE_LEGACY_REMOVE
472 bool m_TransformIsBSpline{ false };
473#endif
474
478
479 static constexpr unsigned int DeformationSplineOrder = 3;
480
483
485 using WeightsValueType = typename BSplineTransformWeightsType::ValueType;
487
489 using IndexValueType = typename BSplineTransformIndexArrayType::ValueType;
491
492 using MovingImagePointArrayType = std::vector<MovingImagePointType>;
493 using BooleanArrayType = std::vector<bool>;
501
505
507
512
514
515 // Variables needed for optionally caching values when using a BSpline
516 // transform.
520
521 mutable std::unique_ptr<BSplineTransformWeightsType[]> m_ThreaderBSplineTransformWeights;
522 mutable std::unique_ptr<BSplineTransformIndexArrayType[]> m_ThreaderBSplineTransformIndices;
523
525 virtual void
527
532 virtual void
533 TransformPoint(unsigned int sampleNumber,
534 MovingImagePointType & mappedPoint,
535 bool & sampleOk,
536 double & movingImageValue,
537 ThreadIdType threadId) const;
538
543 virtual void
544 TransformPointWithDerivatives(unsigned int sampleNumber,
545 MovingImagePointType & mappedPoint,
546 bool & sampleOk,
547 double & movingImageValue,
548 ImageDerivativesType & movingImageGradient,
549 ThreadIdType threadId) const;
550
551#ifndef ITK_FUTURE_LEGACY_REMOVE
555 bool m_InterpolatorIsBSpline{ false };
556#endif
557
560
563
567 virtual void
569 ImageDerivativesType & gradient,
570 ThreadIdType threadId) const;
571
575
585 {
586 public:
588 : m_ConstMetricPointer{ i2i_metricPointer }
589 {}
590 [[nodiscard]] const ImageToImageMetric *
592 {
594 }
595
596 private:
598 };
599
608 {
609 public:
611 : m_WorkUnitInfo(static_cast<const typename MultiThreaderType::WorkUnitInfo *>(workunitInfoAsVoid))
612 {}
613 [[nodiscard]] ThreadIdType
615 {
616 return m_WorkUnitInfo->WorkUnitID;
617 }
618 [[nodiscard]] const ImageToImageMetric *
620 {
621 return (static_cast<ConstantPointerWrapper *>(m_WorkUnitInfo->UserData))->GetConstMetricPointer();
622 }
623
624 private:
626 };
627
629 std::unique_ptr<ConstantPointerWrapper> m_ConstSelfWrapper;
630 mutable std::unique_ptr<unsigned int[]> m_ThreaderNumberOfMovingImageSamples;
633
634 void
636
637 void
639
642 GetValueMultiThreaded(void * workunitInfoAsVoid);
643
646 GetValueMultiThreadedPostProcess(void * workunitInfoAsVoid);
647
649 virtual inline void
651
654 virtual inline void
655 GetValueThreadPreProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
656 {}
657 virtual inline bool
659 SizeValueType itkNotUsed(fixedImageSample),
660 const MovingImagePointType & itkNotUsed(mappedPoint),
661 double itkNotUsed(movingImageValue)) const
662 {
663 return false;
664 }
665 virtual inline void
666 GetValueThreadPostProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
667 {}
668
669 void
671
672 void
674
676 GetValueAndDerivativeMultiThreaded(void * workunitInfoAsVoid);
677
680
681 virtual inline void
683
684 virtual inline void
685 GetValueAndDerivativeThreadPreProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
686 {}
687 virtual inline bool
689 SizeValueType itkNotUsed(fixedImageSample),
690 const MovingImagePointType & itkNotUsed(mappedPoint),
691 double itkNotUsed(movingImageValue),
692 const ImageDerivativesType & itkNotUsed(movingImageGradientValue)) const
693 {
694 return false;
695 }
696 virtual inline void
697 GetValueAndDerivativeThreadPostProcess(ThreadIdType itkNotUsed(threadId), bool itkNotUsed(withinSampleThread)) const
698 {}
699
705 virtual void
707
708private:
710};
711} // end namespace itk
712
713#ifndef ITK_MANUAL_INSTANTIATION
714# include "itkImageToImageMetric.hxx"
715#endif
716
717#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