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
139 itkSetConstObjectMacro(FixedImage, FixedImageType);
140 itkGetConstObjectMacro(FixedImage, FixedImageType);
142
144 itkSetConstObjectMacro(MovingImage, MovingImageType);
145 itkGetConstObjectMacro(MovingImage, MovingImageType);
147
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
177#ifndef ITK_FUTURE_LEGACY_REMOVE
178 virtual void
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);
189
191#ifndef ITK_FUTURE_LEGACY_REMOVE
192 virtual void
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);
203
206 void
208
209 void
210 SetUseFixedImageIndexes(bool useIndexes);
211
212 itkGetConstReferenceMacro(UseFixedImageIndexes, bool);
213
215 void
217 itkGetConstReferenceMacro(NumberOfWorkUnits, ThreadIdType);
219
221 itkSetMacro(ComputeGradient, bool);
222 itkGetConstReferenceMacro(ComputeGradient, bool);
223 itkBooleanMacro(ComputeGradient);
225
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);
260
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
325 void
327 void
330
347 itkSetMacro(UseCachingOfBSplineWeights, bool);
348 itkGetConstReferenceMacro(UseCachingOfBSplineWeights, bool);
349 itkBooleanMacro(UseCachingOfBSplineWeights);
351
354 itkGetModifiableObjectMacro(Threader, MultiThreaderType);
355 const TransformPointer *
357 {
358 return m_ThreaderTransform.get();
359 }
360
361
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 friend inline 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
404
407
409 using FixedImageSampleContainer = std::vector<FixedImageSamplePoint>;
410
415 virtual void
417
419 virtual void
421
426 virtual void
428
431
433
435 // m_NumberOfPixelsCounted must be mutable because the const
436 // thread consolidation functions merge each work unit's values
437 // onto this accumulator variable.
439
442
445
448 std::unique_ptr<TransformPointer[]> m_ThreaderTransform;
449
451
452 bool m_ComputeGradient{ true };
454
457
459
460 bool m_UseAllPixels{ false };
462
463 bool m_ReseedIterator{ false };
464
465 mutable int m_RandomSeed{};
466
472
473#ifndef ITK_FUTURE_LEGACY_REMOVE
477 bool m_TransformIsBSpline{ false };
478#endif
479
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
512
517
519
520 // Variables needed for optionally caching values when using a BSpline
521 // transform.
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
565
568
572 virtual void
574 ImageDerivativesType & gradient,
575 ThreadIdType threadId) const;
576
580
590 {
591 public:
593 : m_ConstMetricPointer{ i2i_metricPointer }
594 {}
595 const ImageToImageMetric *
597 {
599 }
600
601
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 }
629
630
631 private:
633 };
634
636 std::unique_ptr<ConstantPointerWrapper> m_ConstSelfWrapper;
637 mutable std::unique_ptr<unsigned int[]> m_ThreaderNumberOfMovingImageSamples;
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 {}
674
675
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:
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.
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.
ITKTransform_EXPORT std::ostream & operator<<(std::ostream &out, const TransformBaseTemplateEnums::TransformCategory value)
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