Unable to perform simple image registration

Hi,

Duplicate of Unable to perform simple image registration · Issue #1956 · SimpleITK/SimpleITK · GitHub, as I was directed to ask the question here.

I am using SimpleITK-SimpleElastix since SimpleITK-2.2.1 seem to give an error of module 'SimpleITK' has no attribute 'ElastixImageFilter' when trying selx = sitk.ElastixImageFilter(). Anyway, I am trying to register an MR from TCIA to the SRI24 atlas.

>>> import SimpleITK as sitk
>>> print(sitk.Version())     
SimpleITK Version: 2.0.0rc2.dev908-g8244e (ITK 5.1)
Compiled: Mar  3 2021 16:40:05
>>> fixed_image = sitk.ReadImage("C:/Projects/SimpleReg/atlases/sri24/image.nii.gz", sitk.sitkFloat32) 
>>> moving_image = sitk.ReadImage("C:/Projects/SimpleReg/data/tcia_aaac_t1ce.nii.gz", sitk.sitkFloat32) 
>>> selx = sitk.ElastixImageFilter()
>>> selx.SetLogToConsole(True)
<SimpleITK.SimpleITK.ElastixImageFilter; proxy of <Swig Object of type 'itk::simple::ElastixImageFilter::Self *' at 0x000001E0B03BF690> >
>>> parameterMap = sitk.GetDefaultParameterMap("rigid")
>>> parameterMap["MaximumNumberOfIterations"] = ["1024"]
>>> sitk.PrintParameterMap(parameterMap)
ParameterObject (000001E0AF906110)
  RTTI typeinfo:   class elastix::ParameterObject
  Reference Count: 1
  Modified Time: 1130
  Debug: Off
  Object Name:
  Observers:
    none
ParameterMap 0:
  (AutomaticParameterEstimation "true")
  (AutomaticScalesEstimation "true")
  (CheckNumberOfSamples "true")
  (DefaultPixelValue 0)
  (FinalBSplineInterpolationOrder 3)
  (FixedImagePyramid "FixedSmoothingImagePyramid")
  (ImageSampler "RandomCoordinate")
  (Interpolator "LinearInterpolator")
  (MaximumNumberOfIterations 1024)
  (MaximumNumberOfSamplingAttempts 8)
  (Metric "AdvancedMattesMutualInformation")
  (MovingImagePyramid "MovingSmoothingImagePyramid")
  (NewSamplesEveryIteration "true")
  (NumberOfResolutions 4)
  (NumberOfSamplesForExactGradient 4096)
  (NumberOfSpatialSamples 2048)
  (Optimizer "AdaptiveStochasticGradientDescent")
  (Registration "MultiResolutionRegistration")
  (ResampleInterpolator "FinalBSplineInterpolator")
  (Resampler "DefaultResampler")
  (ResultImageFormat "nii")
  (Transform "EulerTransform")
  (WriteIterationInfo "false")
  (WriteResultImage "true")
>>> selx.SetParameterMap(parameterMap)
<SimpleITK.SimpleITK.ElastixImageFilter; proxy of <Swig Object of type 'itk::simple::ElastixImageFilter::Self *' at 0x000001E0B0057150> >
>>> selx.SetFixedImage(fixed_image)
<SimpleITK.SimpleITK.ElastixImageFilter; proxy of <Swig Object of type 'itk::simple::ElastixImageFilter::Self *' at 0x000001E0B03BF4B0> >
>>> selx.SetMovingImage(moving_image)
<SimpleITK.SimpleITK.ElastixImageFilter; proxy of <Swig Object of type 'itk::simple::ElastixImageFilter::Self *' at 0x000001E0B03BF5A0> >
>>> selx.Execute()
Installing all components.
InstallingComponents was successful.

ELASTIX version: 5.000
Command line options from ElastixBase:
-fMask    unspecified, so no fixed mask used
-mMask    unspecified, so no moving mask used
-out      ./
-priority unspecified, so NORMAL process priority
-threads  unspecified, so all available threads are used
WARNING: The parameter "UseDirectionCosines", requested at entry number 0, does not exist at all.
  The default value "true" is used instead.

WARNING: The option "UseDirectionCosines" was not found in your parameter file.
  From elastix 4.8 it defaults to true!
This may change the behavior of your registrations considerably.

Command line options from TransformBase:
-t0       unspecified, so no initial transform used

Reading images...
Reading images took 0 ms.

WARNING: the fixed pyramid schedule is not fully specified!
  A default pyramid schedule is used.
WARNING: the moving pyramid schedule is not fully specified!
  A default pyramid schedule is used.
WARNING: The parameter "AutomaticTransformInitialization", requested at entry number 0, does not exist at all.
  The default value "false" is used instead.
WARNING: The parameter "AutomaticTransformInitializationMethod", requested at entry number 0, does not exist at all.
  The default value "GeometricalCenter" is used instead.
Transform parameters are initialized as: [0, 0, 0, 0, 0, 0]
Scales are estimated automatically.
Scales for transform parameters are: [6916.916666666667, 6916.916666666667, 9583.833333333334, 1, 1, 1]
Initialization of all components (before registration) took: 8 ms.
Preparation of the image pyramids took: 344 ms.

Resolution: 0
WARNING: The parameter "ShowExactMetricValue", requested at entry number 0, does not exist at all.
  The default value "false" is used instead.
WARNING: The parameter "UseMultiThreadingForMetrics", requested at entry number 0, does not exist at all.
  The default value "true" is used instead.
Setting the fixed masks took: 0 ms.
Setting the moving masks took: 0 ms.
WARNING: The parameter "FixedImageBSplineInterpolationOrder", requested at entry number 0, does not exist at all.
  The default value "1" is used instead.
WARNING: The parameter "UseRandomSampleRegion", requested at entry number 0, does not exist at all.
  The default value "false" is used instead.
WARNING: The parameter "NumberOfHistogramBins", requested at entry number 0, does not exist at all.
  The default value "32" is used instead.
WARNING: The parameter "NumberOfFixedHistogramBins", requested at entry number 0, does not exist at all.
  The default value "32" is used instead.
WARNING: The parameter "NumberOfMovingHistogramBins", requested at entry number 0, does not exist at all.
  The default value "32" is used instead.
WARNING: The parameter "FixedLimitRangeRatio", requested at entry number 0, does not exist at all.
  The default value "0.01" is used instead.
WARNING: The parameter "MovingLimitRangeRatio", requested at entry number 0, does not exist at all.
  The default value "0.01" is used instead.
WARNING: The parameter "FixedKernelBSplineOrder", requested at entry number 0, does not exist at all.
  The default value "0" is used instead.
WARNING: The parameter "MovingKernelBSplineOrder", requested at entry number 0, does not exist at all.
  The default value "3" is used instead.
WARNING: The parameter "UseFastAndLowMemoryVersion", requested at entry number 0, does not exist at all.
  The default value "true" is used instead.
WARNING: The parameter "UseJacobianPreconditioning", requested at entry number 0, does not exist at all.
  The default value "false" is used instead.
WARNING: The parameter "FiniteDifferenceDerivative", requested at entry number 0, does not exist at all.
  The default value "false" is used instead.
WARNING: The parameter "SP_A", requested at entry number 0, does not exist at all.
  The default value "20" is used instead.

WARNING: You have set MaximumNumberOfSamplingAttempts to 8.
  This functionality is known to cause problems (stack overflow) for large values.
  If elastix stops or segfaults for no obvious reason, reduce this value.
  You may select the RandomSparseMask image sampler to fix mask-related problems.

WARNING: The parameter "SigmoidInitialTime", requested at entry number 0, does not exist at all.
  The default value "0" is used instead.
WARNING: The parameter "MaxBandCovSize", requested at entry number 0, does not exist at all.
  The default value "192" is used instead.
WARNING: The parameter "NumberOfBandStructureSamples", requested at entry number 0, does not exist at all.
  The default value "10" is used instead.
WARNING: The parameter "UseAdaptiveStepSizes", requested at entry number 0, does not exist at all.
  The default value "true" is used instead.
WARNING: The parameter "UseConstantStep", requested at entry number 0, does not exist at all.
  The default value "false" is used instead.
WARNING: The parameter "MaximumStepLengthRatio", requested at entry number 0, does not exist at all.
  The default value "1" is used instead.
WARNING: The parameter "MaximumStepLength", requested at entry number 0, does not exist at all.
  The default value "0.988281" is used instead.
WARNING: The parameter "NumberOfGradientMeasurements", requested at entry number 0, does not exist at all.
  The default value "0" is used instead.
WARNING: The parameter "NumberOfJacobianMeasurements", requested at entry number 0, does not exist at all.
  The default value "1000" is used instead.
WARNING: The parameter "SigmoidScaleFactor", requested at entry number 0, does not exist at all.
  The default value "0.1" is used instead.
Elastix initialization of all components (for this resolution) took: 7 ms.
  Computing the fixed image extrema took 9 ms.
  Computing the moving image extrema took 1 ms.
Initialization of AdvancedMattesMutualInformation metric took: 12 ms.
Starting automatic parameter estimation for AdaptiveStochasticGradientDescent ...
WARNING: The parameter "ASGDParameterEstimationMethod", requested at entry number 0, does not exist at all.
  The default value "Original" is used instead.
  Computing JacobianTerms ...
  Computing the Jacobian terms took 0.001097s
  NumberOfGradientMeasurements to estimate sigma_i: 6
  Sampling gradients ...
Time spent in resolution 0 (ITK initialization and iterating): 0.016 s.
Stopping condition: Error in metric.
Settings of AdaptiveStochasticGradientDescent in resolution 0:
( SP_a 1.000000 )
( SP_A 20.000000 )
( SP_alpha 0.602000 )
( SigmoidMax 1.000000 )
( SigmoidMin -0.800000 )
( SigmoidScale 0.000000 )


itk::ExceptionObject (000000D40E9EBA88)
Location: "ElastixTemplate - Run()"
File: c:\dafne\simpleelastix\build\elastix\common\costfunctions\itkAdvancedImageToImageMetric.hxx
Line: 1007
Description: itk::ERROR: AdvancedMattesMutualInformationMetric(000001E0B16D1CF0): Too many samples map outside moving image buffer: 0 / 4332


Error occurred during actual registration.


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Projects\SimpleReg\venv\lib\site-packages\SimpleITK\SimpleITK.py", line 11130, in Execute
    return _SimpleITK.ElastixImageFilter_Execute(self)
RuntimeError: Exception thrown in SimpleITK ElastixImageFilter_Execute: C:\dafne\SimpleElastix\Code\Elastix\src\sitkElastixImageFilterImpl.cxx:259:
sitk::ERROR:
itk::ExceptionObject (000000D40E9EBEC0)
Location: "unknown"
File: c:\dafne\simpleelastix\build\elastix\core\main\elxElastixFilter.hxx
Line: 253
Description: itk::ERROR: ElastixFilter(000001E0AF9A1920): Internal elastix error: See elastix log (use LogToConsoleOn() or LogToFileOn()).

Unsure what exactly I am doing wrong, here. Any help will be appreciated! Thanks.

Cheers,
Sarthak

This is usually the result of initial transform which produces no overlap between fixed and moving images. @Niels_Dekker should elastix initialize the transform, if it is not provided like in this example?