SuperElastix / SimpleElastix

Multi-lingual medical image registration library
http://simpleelastix.github.io
Apache License 2.0
507 stars 149 forks source link

Modifying parameters and WriteParameterFile/ReadParameterFile #469

Open ckashuk-aiq opened 2 years ago

ckashuk-aiq commented 2 years ago

I try to do the following: def generate_transform(): rigid_transform = sitk.ElastixImageFilter() ... rigid_map["Metric"] = ["AdvancedNormalizedCorrelation"] (etc)... rigid_transform.SetParameterMap(rigid_map) rigid_transform.Execute() return rigid_transform

Then: rigid = define_transform() rigid.SetFixedImage(image1) (etc)... rigid.Execute()

And I get the expected result, so I save the transform

sitk.WriteParameterFile(rigid.GetTransformParameterMap()[0], path_to_xform)

And in a separate script, I want to use it: rigid_parameters = sitk.ReadParameterFile(path_to_xform) rigid_transform = sitk.TransformixImageFilter() rigid_transform.SetTransformParameterMap(rigid_parameters) print(rigid_transform.GetTransformParameterMap()[0]["Metric"])

but the modified parameter is not in the path_to_xform file, it prints as empty, and the results are not what I expected.

Can I not save a modified transform to be used later, or am I using a SimpleITK function by mistake somewhere?

ckashuk-aiq commented 2 years ago

Further testing, I understand now that we only need the TransformParameters parameter to transform additional images. But I also still see a difference, minor I do admit, which I can only figure to be from the output file being written to 6 digits, could this be true?

I do the following: -run a transform on an image, xform1 -save the parameters from [0] to a file -load the parameters from the file, and create a new TransformixImageFIlter() xform2 with ['TransformParameters'] from the file

pseudocode:

(def_reg defined)

sitk.WriteParameterFile(def_reg.GetTransformParameterMap()[0], xform1_path) xform1 = sitk.ReadParameterFile(xform1_path) def_reg_new = sitk.TransformixImageFilter() def_reg_new.SetTransformParameterMap(xform1) def_reg_new2 = sitk.TransformixImageFilter() def_reg_new2.SetTransformParameterMap(def_reg.GetTransformParameterMap()[0])

apply both transforms to the same file, and compare the resulting GetArrayFromImage()