SimpleITK  
sitkImageRegistrationMethod.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 * 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 sitkImageRegistrationMethod_h
19#define sitkImageRegistrationMethod_h
20
21#include "sitkRegistration.h"
22
23#include "sitkDetail.h"
24#include "sitkImage.h"
25#include "sitkPixelIDTokens.h"
27#include "sitkProcessObject.h"
28
29#include "sitkRandomSeed.h"
30#include "sitkInterpolator.h"
31#include "sitkTransform.h"
32
33namespace itk
34{
35
36#ifndef SWIG
37template <typename TInternalComputationValueType>
39template <typename TFixedImage,
40 typename TMovingImage,
41 typename TVirtualImage,
42 typename TInternalComputationValueType,
43 typename TMetricTraits>
45
46template <typename TFixedImageType, typename TMovingImageType, typename TVirtualImageType, typename TCoordRep>
48
49template <typename TMetric>
51
52template <unsigned int VDimension>
53class SpatialObject;
54
55class Command;
56class EventObject;
57#endif
58
59
60namespace simple
61{
63
88{
89public:
92
94
96
97 std::string
98 GetName() const override
99 {
100 return std::string("ImageRegistrationMethod");
101 }
102
109 std::string
110 ToString() const override;
111
118 {
119 return this->m_Interpolator;
120 }
121 void
123 {
124 this->m_Interpolator = Interpolator;
125 }
126
127
144#if !defined(SWIG) || defined(JAVASWIG) || defined(CSHARPSWIG)
145 // Only wrap this method if the wrapping language is strongly typed
146 void
147 SetInitialTransform(const Transform & transform);
148#endif
149 void
150 SetInitialTransform(Transform & transform, bool inPlace = true);
153 {
154 return this->m_InitialTransform;
155 }
156 bool
158 {
159 return this->m_InitialTransformInPlace;
160 }
161
162
178 void
180 bool inPlace = true,
181 const std::vector<unsigned int> & scaleFactors = std::vector<unsigned int>());
182
196 void
198 {
199 this->m_MovingInitialTransform = transform;
200 }
203 {
204 return this->m_MovingInitialTransform;
205 }
206
207
218 void
220 {
221 this->m_FixedInitialTransform = transform;
222 }
225 {
226 return this->m_FixedInitialTransform;
227 }
228
229
230
235 void
236 SetVirtualDomain(const std::vector<uint32_t> & virtualSize,
237 const std::vector<double> & virtualOrigin,
238 const std::vector<double> & virtualSpacing,
239 const std::vector<double> & virtualDirection);
240 void
241 SetVirtualDomainFromImage(const Image & virtualImage);
243
250 void
252
257 void
259
264 void
265 SetMetricAsDemons(double intensityDifferenceThreshold = 0.001);
266
271 void
272 SetMetricAsJointHistogramMutualInformation(unsigned int numberOfHistogramBins = 20,
273 double varianceForJointPDFSmoothing = 1.5);
274
279 void
281
287 void
288 SetMetricAsMattesMutualInformation(unsigned int numberOfHistogramBins = 50);
289
290
297
302 void
304 unsigned int numberOfIterations,
305 double convergenceMinimumValue = 1e-6,
306 unsigned int convergenceWindowSize = 10,
307 double lineSearchLowerLimit = 0,
308 double lineSearchUpperLimit = 5.0,
309 double lineSearchEpsilon = 0.01,
310 unsigned int lineSearchMaximumIterations = 20,
311 EstimateLearningRateType estimateLearningRate = Once,
312 double maximumStepSizeInPhysicalUnits = 0.0);
313
318 void
320 double minStep,
321 unsigned int numberOfIterations,
322 double relaxationFactor = 0.5,
323 double gradientMagnitudeTolerance = 1e-4,
324 EstimateLearningRateType estimateLearningRate = Never,
325 double maximumStepSizeInPhysicalUnits = 0.0);
326
331 void
332 SetOptimizerAsGradientDescent(double learningRate,
333 unsigned int numberOfIterations,
334 double convergenceMinimumValue = 1e-6,
335 unsigned int convergenceWindowSize = 10,
336 EstimateLearningRateType estimateLearningRate = Once,
337 double maximumStepSizeInPhysicalUnits = 0.0);
338
343 void
345 unsigned int numberOfIterations,
346 double convergenceMinimumValue = 1e-6,
347 unsigned int convergenceWindowSize = 10,
348 double lineSearchLowerLimit = 0,
349 double lineSearchUpperLimit = 5.0,
350 double lineSearchEpsilon = 0.01,
351 unsigned int lineSearchMaximumIterations = 20,
352 EstimateLearningRateType estimateLearningRate = Once,
353 double maximumStepSizeInPhysicalUnits = 0.0);
354
361 void
362 SetOptimizerAsLBFGSB(double gradientConvergenceTolerance = 1e-5,
363 unsigned int numberOfIterations = 500,
364 unsigned int maximumNumberOfCorrections = 5,
365 unsigned int maximumNumberOfFunctionEvaluations = 2000,
366 double costFunctionConvergenceFactor = 1e+7,
367 double lowerBound = std::numeric_limits<double>::min(),
368 double upperBound = std::numeric_limits<double>::max(),
369 bool trace = false);
370
383 void
384 SetOptimizerAsLBFGS2(double solutionAccuracy = 1e-5,
385 unsigned int numberOfIterations = 0,
386 unsigned int hessianApproximateAccuracy = 6,
387 unsigned int deltaConvergenceDistance = 0,
388 double deltaConvergenceTolerance = 1e-5,
389 unsigned int lineSearchMaximumEvaluations = 40,
390 double lineSearchMinimumStep = 1e-20,
391 double lineSearchMaximumStep = 1e20,
392 double lineSearchAccuracy = 1e-4);
393
394
411 void
412 SetOptimizerAsExhaustive(const std::vector<unsigned int> & numberOfSteps, double stepLength = 1.0);
413
423 void
424 SetOptimizerAsAmoeba(double simplexDelta,
425 unsigned int numberOfIterations,
426 double parametersConvergenceTolerance = 1e-8,
427 double functionConvergenceTolerance = 1e-4,
428 bool withRestarts = false);
429
441 void
442 SetOptimizerWeights(const std::vector<double> & weights);
443 std::vector<double>
446
451 void
452 SetOptimizerAsPowell(unsigned int numberOfIterations = 100,
453 unsigned int maximumLineIterations = 100,
454 double stepLength = 1,
455 double stepTolerance = 1e-6,
456 double valueTolerance = 1e-6);
457
466 void
467 SetOptimizerAsOnePlusOneEvolutionary(unsigned int numberOfIterations = 100,
468 double epsilon = 1.5e-4,
469 double initialRadius = 1.01,
470 double growthFactor = -1.0,
471 double shrinkFactor = -1.0,
472 unsigned int seed = sitkWallClock);
473
474
476 void
477 SetOptimizerScales(const std::vector<double> & scales);
478
485 void
486 SetOptimizerScalesFromJacobian(unsigned int centralRegionRadius = 5);
487
493 void
494 SetOptimizerScalesFromIndexShift(unsigned int centralRegionRadius = 5, double smallParameterVariation = 0.01);
495
502 void
503 SetOptimizerScalesFromPhysicalShift(unsigned int centralRegionRadius = 5, double smallParameterVariation = 0.01);
504
505
515 void
516 SetMetricFixedMask(const Image & binaryMask);
517
527 void
528 SetMetricMovingMask(const Image & binaryMask);
529
548 void
549 SetMetricSamplingPercentage(double percentage, unsigned int seed = sitkWallClock);
550 void
551 SetMetricSamplingPercentagePerLevel(const std::vector<double> & percentage, unsigned int seed = sitkWallClock);
553
555 const std::vector<double> &
557
574
579 void
581
591 void
593 void
598 void
603
604
605
616 void
618 void
623 void
628
629
630
638 void
639 SetShrinkFactorsPerLevel(const std::vector<unsigned int> & shrinkFactors);
640
649 void
650 SetSmoothingSigmasPerLevel(const std::vector<double> & smoothingSigmas);
651
658 void
660 void
665 void
670
671
672
675 Execute(const Image & fixed, const Image & moving);
676
677
687 double
688 MetricEvaluate(const Image & fixed, const Image & moving);
689
690
697 unsigned int
699 std::vector<double>
701 double
703 double
705 double
707
716 uint64_t
718
719
720 unsigned int
722
730 std::vector<double>
732
735 std::string
737
738
747 bool
749
750protected:
751 template <class TImage>
753 ExecuteInternal(const Image & fixed, const Image & moving);
754
755 template <class TImage>
756 double
757 EvaluateInternal(const Image & fixed, const Image & moving);
758
759
761 CreateOptimizer(unsigned int numberOfTransformParameters);
762
763 template <unsigned int VDimension>
766
767 template <class TImageType>
768 itk::ImageToImageMetricv4<TImageType,
769 TImageType,
770 TImageType,
771 double,
774
775 template <class TImageType>
776 void
778 itk::ImageToImageMetricv4<TImageType,
779 TImageType,
780 TImageType,
781 double,
783 const TImageType *,
784 const TImageType *);
785
786 template <typename TMetric>
789
790 template <typename TTransformAdaptorPointer, typename TRegistrationMethod>
791 std::vector<TTransformAdaptorPointer>
792 CreateTransformParametersAdaptor(TRegistrationMethod * method);
793
794 void
796 void
797 OnActiveProcessDelete() noexcept override;
798 unsigned long
799 AddITKObserver(const itk::EventObject &, itk::Command *) override;
800 void
802
803private:
804 std::function<unsigned int()> m_pfGetOptimizerIteration;
805 std::function<std::vector<double>()> m_pfGetOptimizerPosition;
808 std::function<double()> m_pfGetMetricValue;
810 std::function<std::vector<double>()> m_pfGetOptimizerScales;
813
814
815 std::function<unsigned int()> m_pfGetCurrentLevel;
816
817 std::function<void(itk::TransformBase * outTransform)> m_pfUpdateWithBestValue;
818
819 template <class TMemberFunctionPointer>
821 {
822 using ObjectType = typename ::detail::FunctionTraits<TMemberFunctionPointer>::ClassType;
823
824 template <typename TImageType>
825 constexpr TMemberFunctionPointer
827 {
828 return &ObjectType::template EvaluateInternal<TImageType>;
829 }
830 };
831
832 typedef Transform (ImageRegistrationMethod::*MemberFunctionType)(const Image & fixed, const Image & moving);
833 typedef double (ImageRegistrationMethod::*EvaluateMemberFunctionType)(const Image & fixed, const Image & moving);
835
841
842 std::vector<uint32_t> m_VirtualDomainSize;
843 std::vector<double> m_VirtualDomainOrigin;
844 std::vector<double> m_VirtualDomainSpacing;
845 std::vector<double> m_VirtualDomainDirection;
846
847 // optimizer
882 std::vector<unsigned int> m_OptimizerNumberOfSteps;
895 unsigned int m_OptimizerSeed;
904
905
906 std::vector<unsigned int> m_TransformBSplineScaleFactors;
907
908 std::vector<double> m_OptimizerWeights;
909
918 std::vector<double> m_OptimizerScales;
921
922 // metric
933 unsigned int m_MetricRadius;
937
940
941 std::vector<double> m_MetricSamplingPercentage;
944
947
948 std::vector<unsigned int> m_ShrinkFactorsPerLevel;
949 std::vector<double> m_SmoothingSigmasPerLevel;
951
954 unsigned int m_Iteration;
956
958};
959
960} // namespace simple
961} // namespace itk
962
963#endif // sitkImageRegistrationMethod_h
A deformable transform over a bounded spatial domain using a BSpline representation for a 2D or 3D co...
double EvaluateInternal(const Image &fixed, const Image &moving)
void SetOptimizerAsExhaustive(const std::vector< unsigned int > &numberOfSteps, double stepLength=1.0)
Set the optimizer to sample the metric at regular steps.
std::function< std::vector< double >()> m_pfGetOptimizerPosition
void SetMetricAsCorrelation()
Use negative normalized cross correlation image metric.
void SetMetricAsMeanSquares()
Use negative means squares image metric.
void SetOptimizerWeights(const std::vector< double > &weights)
A per parameter weighting array for the optimizer.
void SetMetricSamplingStrategy(MetricSamplingStrategyType strategy)
Set sampling strategy for sample generation.
Transform(ImageRegistrationMethod::* MemberFunctionType)(const Image &fixed, const Image &moving)
void MetricUseMovingImageGradientFilterOff()
Enable image gradient computation by a filter.
void SetInterpolator(InterpolatorEnum Interpolator)
Set and get the interpolator to use.
double(ImageRegistrationMethod::* EvaluateMemberFunctionType)(const Image &fixed, const Image &moving)
std::vector< double > GetOptimizerScales() const
Get the OptimizerScales.
void SetMetricUseMovingImageGradientFilter(bool)
Enable image gradient computation by a filter.
uint64_t GetMetricNumberOfValidPoints() const
void SetMetricSamplingPercentagePerLevel(const std::vector< double > &percentage, unsigned int seed=sitkWallClock)
Set percentage of pixels sampled for metric evaluation.
itk::SpatialObject< VDimension > * CreateSpatialObjectMask(const Image &mask)
const std::vector< double > & GetMetricSamplingPercentagePerLevel() const
Get the percentage of pixels used for metric evaluation.
void SetMetricUseFixedImageGradientFilter(bool)
Enable image gradient computation by a filter.
void SetOptimizerAsAmoeba(double simplexDelta, unsigned int numberOfIterations, double parametersConvergenceTolerance=1e-8, double functionConvergenceTolerance=1e-4, bool withRestarts=false)
Set optimizer to Nelder-Mead downhill simplex algorithm.
itk::ImageToImageMetricv4< TImageType, TImageType, TImageType, double, itk::DefaultImageToImageMetricTraitsv4< TImageType, TImageType, TImageType, double > > * CreateMetric()
bool GetInitialTransformInPlace() const
Set the initial transform and parameters to optimize.
void SetVirtualDomainFromImage(const Image &virtualImage)
Set the virtual domain used for sampling.
Transform Execute(const Image &fixed, const Image &moving)
Optimize the configured registration problem.
std::vector< double > GetOptimizerPosition() const
void SetMetricAsJointHistogramMutualInformation(unsigned int numberOfHistogramBins=20, double varianceForJointPDFSmoothing=1.5)
Use mutual information between two images.
void SetOptimizerScalesFromJacobian(unsigned int centralRegionRadius=5)
Estimate scales from Jacobian norms.
void SetOptimizerAsGradientDescentLineSearch(double learningRate, unsigned int numberOfIterations, double convergenceMinimumValue=1e-6, unsigned int convergenceWindowSize=10, double lineSearchLowerLimit=0, double lineSearchUpperLimit=5.0, double lineSearchEpsilon=0.01, unsigned int lineSearchMaximumIterations=20, EstimateLearningRateType estimateLearningRate=Once, double maximumStepSizeInPhysicalUnits=0.0)
Gradient descent optimizer with a golden section line search.
void SmoothingSigmasAreSpecifiedInPhysicalUnitsOn()
Enable the smoothing sigmas for each level in physical units (default) or in terms of voxels.
void SetOptimizerAsPowell(unsigned int numberOfIterations=100, unsigned int maximumLineIterations=100, double stepLength=1, double stepTolerance=1e-6, double valueTolerance=1e-6)
Powell optimization using Brent line search.
void PreUpdate(itk::ProcessObject *p) override
void MetricUseFixedImageGradientFilterOff()
Enable image gradient computation by a filter.
void MetricUseFixedImageGradientFilterOn()
Enable image gradient computation by a filter.
void SetMetricAsMattesMutualInformation(unsigned int numberOfHistogramBins=50)
Use the mutual information between two images to be registered using the method of Mattes et al.
Transform GetFixedInitialTransform() const
Set transform mapping to the fixed domain.
itk::ObjectToObjectOptimizerBaseTemplate< double > * m_ActiveOptimizer
void SetOptimizerAsLBFGSB(double gradientConvergenceTolerance=1e-5, unsigned int numberOfIterations=500, unsigned int maximumNumberOfCorrections=5, unsigned int maximumNumberOfFunctionEvaluations=2000, double costFunctionConvergenceFactor=1e+7, double lowerBound=std::numeric_limits< double >::min(), double upperBound=std::numeric_limits< double >::max(), bool trace=false)
Limited memory Broyden Fletcher Goldfarb Shannon minimization with simple bounds.
void SetMetricFixedMask(const Image &binaryMask)
Set an image mask in order to restrict the sampled points for the metric.
std::string ToString() const override
Print the information about the object to a string.
void SetShrinkFactorsPerLevel(const std::vector< unsigned int > &shrinkFactors)
Set the isotropic shrink factors for each level.
void SetMetricSamplingPercentage(double percentage, unsigned int seed=sitkWallClock)
Set percentage of pixels sampled for metric evaluation.
Transform GetInitialTransform()
Set the initial transform and parameters to optimize.
std::string GetOptimizerStopConditionDescription() const
itk::RegistrationParameterScalesEstimator< TMetric > * CreateScalesEstimator()
void SetMetricMovingMask(const Image &binaryMask)
Set an image mask in order to restrict the sampled points for the metric in the moving image space.
std::function< unsigned int()> m_pfGetOptimizerIteration
void SetOptimizerAsOnePlusOneEvolutionary(unsigned int numberOfIterations=100, double epsilon=1.5e-4, double initialRadius=1.01, double growthFactor=-1.0, double shrinkFactor=-1.0, unsigned int seed=sitkWallClock)
1+1 evolutionary optimizer strategy.
void MetricUseMovingImageGradientFilterOn()
Enable image gradient computation by a filter.
MetricSamplingStrategyType
Sampling strategies for obtaining points.
std::function< void(itk::TransformBase *outTransform)> m_pfUpdateWithBestValue
itk::ObjectToObjectOptimizerBaseTemplate< double > * CreateOptimizer(unsigned int numberOfTransformParameters)
void SetOptimizerAsConjugateGradientLineSearch(double learningRate, unsigned int numberOfIterations, double convergenceMinimumValue=1e-6, unsigned int convergenceWindowSize=10, double lineSearchLowerLimit=0, double lineSearchUpperLimit=5.0, double lineSearchEpsilon=0.01, unsigned int lineSearchMaximumIterations=20, EstimateLearningRateType estimateLearningRate=Once, double maximumStepSizeInPhysicalUnits=0.0)
Conjugate gradient descent optimizer with a golden section line search for nonlinear optimization.
void SetInitialTransform(Transform &transform, bool inPlace=true)
Set the initial transform and parameters to optimize.
void OnActiveProcessDelete() noexcept override
Transform ExecuteInternal(const Image &fixed, const Image &moving)
void SetVirtualDomain(const std::vector< uint32_t > &virtualSize, const std::vector< double > &virtualOrigin, const std::vector< double > &virtualSpacing, const std::vector< double > &virtualDirection)
Set the virtual domain used for sampling.
unsigned long AddITKObserver(const itk::EventObject &, itk::Command *) override
double MetricEvaluate(const Image &fixed, const Image &moving)
Get the value of the metric given the state of the method.
void SetOptimizerAsGradientDescent(double learningRate, unsigned int numberOfIterations, double convergenceMinimumValue=1e-6, unsigned int convergenceWindowSize=10, EstimateLearningRateType estimateLearningRate=Once, double maximumStepSizeInPhysicalUnits=0.0)
Gradient descent optimizer.
std::vector< unsigned int > m_TransformBSplineScaleFactors
std::vector< double > GetOptimizerWeights() const
A per parameter weighting array for the optimizer.
void SetupMetric(itk::ImageToImageMetricv4< TImageType, TImageType, TImageType, double, itk::DefaultImageToImageMetricTraitsv4< TImageType, TImageType, TImageType, double > > *, const TImageType *, const TImageType *)
void SetSmoothingSigmasPerLevel(const std::vector< double > &smoothingSigmas)
Set the sigmas of Gaussian used for smoothing.
void SetOptimizerAsRegularStepGradientDescent(double learningRate, double minStep, unsigned int numberOfIterations, double relaxationFactor=0.5, double gradientMagnitudeTolerance=1e-4, EstimateLearningRateType estimateLearningRate=Never, double maximumStepSizeInPhysicalUnits=0.0)
Regular Step Gradient descent optimizer.
void RemoveITKObserver(EventCommand &e) override
InterpolatorEnum GetInterpolator()
Set and get the interpolator to use.
std::function< uint64_t()> m_pfGetMetricNumberOfValidPoints
unsigned int GetCurrentLevel() const
void SetOptimizerScalesFromIndexShift(unsigned int centralRegionRadius=5, double smallParameterVariation=0.01)
Estimate scales from maximum voxel shift in index space cause by parameter change.
void SetOptimizerScalesFromPhysicalShift(unsigned int centralRegionRadius=5, double smallParameterVariation=0.01)
Estimating scales of transform parameters a step sizes, from the maximum voxel shift in physical spac...
@ Once
Estimate learning once each level, ignore provided values.
@ EachIteration
Estimate learning rate at each iteration.
@ Never
Never run estimation, use provided value.
void SetOptimizerScales(const std::vector< double > &scales)
Manually set per parameter weighting for the transform parameters.
void SetMetricAsDemons(double intensityDifferenceThreshold=0.001)
Use demons image metric.
std::function< unsigned int()> m_pfGetCurrentLevel
unsigned int GetOptimizerIteration() const
void SetMovingInitialTransform(const Transform &transform)
Set a fixed transform component towards moving domain.
void SetSmoothingSigmasAreSpecifiedInPhysicalUnits(bool arg)
Enable the smoothing sigmas for each level in physical units (default) or in terms of voxels.
std::function< std::string()> m_pfGetOptimizerStopConditionDescription
void SetInitialTransform(const Transform &transform)
Set the initial transform and parameters to optimize.
void SetInitialTransformAsBSpline(BSplineTransform &transform, bool inPlace=true, const std::vector< unsigned int > &scaleFactors=std::vector< unsigned int >())
Set an initial BSpline transform to optimize.
void SmoothingSigmasAreSpecifiedInPhysicalUnitsOff()
Enable the smoothing sigmas for each level in physical units (default) or in terms of voxels.
void SetOptimizerAsLBFGS2(double solutionAccuracy=1e-5, unsigned int numberOfIterations=0, unsigned int hessianApproximateAccuracy=6, unsigned int deltaConvergenceDistance=0, double deltaConvergenceTolerance=1e-5, unsigned int lineSearchMaximumEvaluations=40, double lineSearchMinimumStep=1e-20, double lineSearchMaximumStep=1e20, double lineSearchAccuracy=1e-4)
Limited memory Broyden Fletcher Goldfarb Shannon minimization without bounds.
std::vector< TTransformAdaptorPointer > CreateTransformParametersAdaptor(TRegistrationMethod *method)
std::function< std::vector< double >()> m_pfGetOptimizerScales
void SetMetricAsANTSNeighborhoodCorrelation(unsigned int radius)
Use normalized cross correlation using a small neighborhood for each voxel between two images,...
void SetFixedInitialTransform(const Transform &transform)
Set transform mapping to the fixed domain.
Transform GetMovingInitialTransform() const
Set a fixed transform component towards moving domain.
The Image class for SimpleITK.
Definition sitkImage.h:77
friend class itk::simple::Command
A simplified wrapper around a variety of ITK transforms.
TransformBaseTemplate< double > TransformBase
STL namespace.
#define SITKRegistration_EXPORT
typename ::detail::FunctionTraits< TMemberFunctionPointer >::ClassType ObjectType