Use Elastix inside my module

Hello everybody,

I am sorry if this will result you as a silly question,

I need to call the 3DSlicer Elastix extension inside my module. I cannot find informations about how to know which parameters I need to set before calling an external module.

In particular, once I press the apply button on my Module, Elastix should register the input image of my module with a second atlas image that It will be loaded inside the python script (invisible to the user) . The registration needs to be MRI monomodal and the output image needs to be visualized on the screen.

I also need to save the resulting elastix output image and the affine transformation for further processing inside my module.

thank you very much for any help

1 Like

Most Python scripted modules contain test that serves as an example of how to use the module from without its user interface, from another Python script.

For Elastix, the test/example is here:

Note that when you use it from another module then you have to import the Elastix first and use the Elastix namespace. For example, to get the logic:

import Elastix
logic = Elastix.ElastixLogic()

To use an atlas, add the atlas to the scene right before the registration and then remove it from the scene once the registration is completed. You can keep the volume node in memory in a Python variable so that you don’t need to load it each time you run the registration.

My understanding is that Elastix currently can only save transformation result as a displacement field. I’ve reported this on their bugtracker, hopefully we’ll hear from them soon. Maybe if you add a note at the bug report then they maybe it would motivate them a bit more to look into this issue. The bug report is here: Save transformation result as an ITK transform file · Issue #18 · SuperElastix/elastix · GitHub

thank you very much Andras, I managed to do everything except keeping the atlas volume node in memory. What I am currently doing is just loading the volume every time :

image = slicer.util.loadVolume(’/home/marlene/Desktop/atlas+stripping/A1_skull.nii’,returnNode = True)

could you suggest me a more efficient way ?

thank you again

Probably loading the node from file does not take significant amount of time (compared to how long the registration takes), but for your information, you can remove and add back the volume to the scene like this:

self.maskImage = slicer.util.loadVolume(’/home/marlene/Desktop/atlas+stripping/A1_skull.nii’,returnNode = True)
# Node is in the scene now and also as a class member variable, which is not deleted when you return from the method

# Node is not in the scene anymore but is not deleted because it is still referenced by self.maskImage

# Node is in the scene again

self.maskImage = None
# Nothing references the volume now, so it is permanently deleted from memory

Thank you again for your time , this was really useful :slight_smile: