Hello,
I am a beginner at coding in python and using Simple ITK.
I am doing a non-rigid registration of 2 set of CT from differents patients and I am facing severals issues…
The first one is the error : “Failed to allocate memory for image”
indeed, i can not select the whole directory which contains my CT (around 600 CT of 500 ko)
But if i select only a few CT from the directory, it works (around 200)
The second thing is the run time.
Indeed, the code takes 1h30 to run and show the registration.
I changed some parameters like “number of iteration” or “convergence window” to reduce the run-time but nothing… still takes too long
How I can reduce this run-time ? Do I have to change the parameter or my code is not good itself ?
Can someone please help …
The code i am using is the following :
sitk registration 2.py (4.0 KB)
(if you cannot click on the link, here s the code)
from __future__ import print_function
import SimpleITK as sitk
import sys
import os
data_directory = "E:/document/_11F06DD_Y5_M6_P50_Copie"
series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory)
if not series_IDs:
print("ERROR: given directory \"" + data_directory + "\" does not contain a DICOM series.")
sys.exit(1)
series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(data_directory, series_IDs[0])
data_directory2 = "E:/Document/ctpatient"
series_IDs2 = sitk.ImageSeriesReader.GetGDCMSeriesIDs(data_directory2)
if not series_IDs2:
print("ERROR: given directory \"" + data_directory2 + "\" does not contain a DICOM series.")
sys.exit(1)
series_file_names2 = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(data_directory2, series_IDs2[0])
def command_iteration(method, bspline_transform):
if method.GetOptimizerIteration() == 0:
print(bspline_transform)
print("{0:3} = {1:10.5f}".format(method.GetOptimizerIteration(), method.GetMetricValue()))
def command_multi_iteration(method):
if R.GetCurrentLevel() > 0:
print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
print(" Iteration: {0}".format(R.GetOptimizerIteration()))
print(" Metric value: {0}".format(R.GetMetricValue()))
print("--------- Resolution Changing ---------")
fixed = sitk.ReadImage(series_file_names, sitk.sitkFloat32)
moving = sitk.ReadImage(series_file_names2, sitk.sitkFloat32)
transformDomainMeshSize = [2]*fixed.GetDimension()
tx = sitk.BSplineTransformInitializer(fixed, transformDomainMeshSize)
print("Initial Number of Parameters: {0}".format(tx.GetNumberOfParameters()))
R = sitk.ImageRegistrationMethod()
R.SetMetricAsJointHistogramMutualInformation()
R.SetOptimizerAsGradientDescentLineSearch(50,
100,
convergenceMinimumValue=1e-2,
convergenceWindowSize=3)
R.SetInterpolator(sitk.sitkLinear)
R.SetInitialTransformAsBSpline(tx,
inPlace=True,
scaleFactors=[1,2,5])
R.SetShrinkFactorsPerLevel([4,2,1])
R.SetSmoothingSigmasPerLevel([4,2,1])
R.AddCommand(sitk.sitkIterationEvent, lambda: command_iteration(R, tx))
R.AddCommand(sitk.sitkMultiResolutionIterationEvent, lambda: command_multi_iteration(R))
R_finale = R.Execute(fixed, moving)
print("-------")
print(tx)
print(R_finale)
print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription()))
print(" Iteration: {0}".format(R.GetOptimizerIteration()))
print(" Metric value: {0}".format(R.GetMetricValue()))
sitk.WriteTransform(R_finale, 'W:/DICOMresultats/resultats.txt')
if ( not "SITK_NOSHOW" in os.environ ):
resampler = sitk.ResampleImageFilter()
resampler.SetReferenceImage(fixed);
resampler.SetInterpolator(sitk.sitkLinear)
resampler.SetDefaultPixelValue(100)
# get the transformation
resampler.SetTransform(R_finale)
# transform and resample moving image
out = resampler.Execute(moving)
# rescaling image intensities
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, "Image Registration Composition")