pythonitk

ITK python wrapper not working with other optimizers during registration (v4)


Using the example given on the itk website, I am able to get the RegularStepGradientDescentOptimizerv4 to work with MeanSquaresImageToImageMetricv4 and ImageRegistrationMethodv4 in python. However, when I want to use QuasiNewtonOptimizerv4, I get the error TypeError: in method 'itkImageRegistrationMethodv4REGv4F2F2_SetOptimizer', argument 2 of type 'itkObjectToObjectOptimizerBaseTemplateD *'.

My code for each is as follows.

    PixelType = itk.ctype('float')
    fixedImage = itk.GetImageFromArray(fixed_img.astype(np.float32))
    movingImage = itk.GetImageFromArray(moving_img.astype(np.float32))
    
    Dimension = fixedImage.GetImageDimension()
    FixedImageType = itk.Image[PixelType, Dimension]
    MovingImageType = itk.Image[PixelType, Dimension]

    TransformType = itk.TranslationTransform[itk.D, Dimension]
    initialTransform = TransformType.New()

    optimizer = itk.QuasiNewtonOptimizerv4Template.New()

    optimizer1 = itk.RegularStepGradientDescentOptimizerv4.New(
        LearningRate=1,
        MinimumStepLength=0.001,
        RelaxationFactor=0.05,
        NumberOfIterations=100,
    )
    
    
    metric = itk.MeanSquaresImageToImageMetricv4[FixedImageType, MovingImageType].New()
    
    #THIS WORKS
    registration = itk.ImageRegistrationMethodv4.New(
        FixedImage=fixedImage,
        MovingImage=movingImage,
        Metric=metric,
        Optimizer=optimizer1,
        InitialTransform=initialTransform,
    )


    #THIS DOESN'T WORK
    registration = itk.ImageRegistrationMethodv4.New(
        FixedImage=fixedImage,
        MovingImage=movingImage,
        Metric=metric,
        Optimizer=optimizer,
        InitialTransform=initialTransform,
    )

Is there a way to get registration with the QuasiNewtonOptimizerv4 to work in python? Please advise, thanks.


Solution

  • Maybe you need to declare optimizer like this: optimizer = itk.QuasiNewtonOptimizerv4Template[itk.D].New()