0
votes

I am running the python example of simpleitk from this link https://simpleitk.readthedocs.io/en/master/Examples/ImageRegistrationMethod1/Documentation.html

in spyder anaconda.

I have installed packages simpleitk,system,os through anaconda Prompt .

from __future__ import print_function

import SimpleITK as sitk
import sys
import os


def command_iteration(method) :
    print("{0:3} = {1:10.5f} : {2}".format(method.GetOptimizerIteration(),
                                   method.GetMetricValue(),
                                   method.GetOptimizerPosition()))

if len ( sys.argv ) < 4:
    print( "Usage: {0} <fixedImageFilter> <movingImageFile> <outputTransformFile>".format(sys.argv[0]))
cc


fixed = sitk.ReadImage(sys.argv[1], sitk.sitkFloat32)
print(sys.argv[1])
moving = sitk.ReadImage(sys.argv[2], sitk.sitkFloat32)

R = sitk.ImageRegistrationMethod()
R.SetMetricAsMeanSquares()
R.SetOptimizerAsRegularStepGradientDescent(4.0, .01, 200 )
R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension()))
R.SetInterpolator(sitk.sitkLinear)

R.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(R) )

outTx = R.Execute(fixed, moving)

print("-------")
print(outTx)
print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
print(" Iteration: {0}".format(R.GetOptimizerIteration()))
print(" Metric value: {0}".format(R.GetMetricValue()))

sitk.WriteTransform(outTx,  sys.argv[3])

if ( not "SITK_NOSHOW" in os.environ ):

    resampler = sitk.ResampleImageFilter()
    resampler.SetReferenceImage(fixed);
    resampler.SetInterpolator(sitk.sitkLinear)
    resampler.SetDefaultPixelValue(100)
    resampler.SetTransform(outTx)

    out = resampler.Execute(moving)
    simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8)
    simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8)
    cimg = sitk.Compose(simg1, simg2, simg1//2.+simg2//2.)
    sitk.Show( cimg, "ImageRegistration1 Composition" )

On running this code it is giving me error following error Usage: E:/registration/simpleitk.py An exception has occurred, use %tb to see the full traceback.

SystemExit: 1

C:\Users\aBC\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3275: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

How i can correct this error?

1

1 Answers

0
votes

I suspect that the script is not able to load an image in the call to sitk.ReadImage. It is expecting a file name to be provided in the sys.argv[1] variable. The script is designed to be run on the command line with the fixed image file name, the moving image file name and the output transform file name provided as command line arguments.

If you can't provide command line argument in Spyder (I don't know; I'm not familiar with it), you could hard code the file names in the script.

If you have further questions about SimpleITK, I suggest checking the ITK Discourse, discourse.itk.org.