Start programming in Slicer: Transforms module, Elastix

Hi, I’m new in slicer so I have beginners questions:

I’d like to use slicer modules through python programming, which I see in many places that is possible, just not sure how to get started. I don’t want to work through the consol, but rather write scripts etc.

  1. How do I do that- where do I write the scripts?
  2. After I’ll figure out 1 - Is the only option is to create an extantion, and that way to operate it?
  3. Can I access into a module script (how?) set a breakpoint and see how it works (as I can do in MATALB functions), like in Transformation module?

Question regarding elastix and transformation module:
when I apply elastix transformation on an image using transformation module, I assuem it do resampling+ interpolation.

  1. How\Can I control the type of interpolation?
  2. Is the elastix transformation include both rigid+non-linear registration in a vector field? How can I visualize the transformation without applying it on an image?


Welcome to Slicer.

There are 4 simple video tutorials that explains the basics Slicer python programming for beginners (note: German audio with English subtitle).

elastix uses parameter text file that controls the registration process. You can modify it to control any registration component you want including interpolation, you can access all these parameter files by clicking on “Advanced” then click on “Show database folder” button.

Thank you @brhoom !
I will look at the videos!
Regarding elastix,
1.I’m asking about the result- the transformation- How can I see it? how can I apply it with contoling the interpolation? is the same interpolation that was used during the registration is used when I apply the transformation resulted from the registration process?
2. Parameters files- how can I know which one is related to which preset Inputs? (they don’t have the same names)

Slicer plugin is just an interface, to learn more about it, search for If you want to learn more about elastix, install it and read the elastix manual, it has simple examples for different stuff, a simple usage is:

 elastix -f fixed_image -m moving_image -p parmaeter_file.txt -out output_folder

To see the results file:
Advanced->show temp folder.
The transformation is saved in TransformParameters.0.txt.

search for ElastixParameterSetDatabase.xml, it explains the parameters files connection to Slicer interface. You can control the interpolation by modifying the used parameters file before the registration, e.g. by modifying the lines (Interpolator “BSplineInterpolator”), (FixedImageBSplineInterpolationOrder 1 ), (BSplineInterpolationOrder 1) .

1 Like

These videos look great - thanks for the link!

1 Like

The official Slicer programming tutorial is quite good, too. It is fully up-to-date for Slicer-4.8:

1 Like

Yes, of course. You can choose from a number of integrated development environments with visual debuggers. I mostly use PyCharm but Visual Studio and LiClipse are quite good, too. See detailed instructions here: Documentation/Nightly/Extensions/DebuggingTools - Slicer Wiki

Yes, Elastix provides the entire transformation as a displacement field. This is somewhat inconvenient for linear and bspline transforms where it would be more efficient to be able to work with matrices and coefficients instead. We plan to have this option implemented soon.

You can visualize a transform using Transforms module’s Display section.

I scucedde using the instructions here:
to set a breakpoint.
but when I want to stop and go back to slicer, slicer is getting “not responding” , and I have to close it and open again. Is this a normal behaviour?

Yes, when a breakpoint is hit, execution is stopped. No need to close and open anything, just use Resume program to continue.

Hi @brhoom
I tried to change the ResampleInterpolator to:
(ResampleInterpolator “NearestNeighborResampleInterpolator”)
and removed (FinalBSplineInterpolationOrder 1)
In order to change the final interpolation that is applied on the image.
But I get an error:

Traceback (most recent call last):

File “C:/Users/user/AppData/Roaming/NA-MIC/Extensions-26813/SlicerElastix/lib/Slicer-4.8/qt-scripted-modules/”, line 327, in onApplyButton
movingVolumeMaskNode = self.movingVolumeMaskSelector.currentNode())
File “C:/Users/user/AppData/Roaming/NA-MIC/Extensions-26813/SlicerElastix/lib/Slicer-4.8/qt-scripted-modules/”, line 575, in registerVolumes
File “C:/Users/user/AppData/Roaming/NA-MIC/Extensions-26813/SlicerElastix/lib/Slicer-4.8/qt-scripted-modules/”, line 515, in logProcessOutput
raise subprocess.CalledProcessError(return_code, “elastix”)
CalledProcessError: Command ‘elastix’ returned non-zero exit status 1

when I tried to keep

(ResampleInterpolator “BSplineInterpolator”)
(FinalBSplineInterpolationOrder 0)

it runs, but the result seems like it didn’t do anything…

could you please help me understand what do I do wrong?

Please don’t address members in your question as this will encourage other members to answer. Try to isolate the problem, is it elastix problem or slicer problem? check elastix log, run elastix using the same images and parameters and check the result.

check elastix log

This was a wonderfull advice! I’ve realized I didn’t use the parameter files I though I’m using.

run elastix using the same images and parameters and check the result

What do you mean?
Thank you!

I meant using the terminal without slicer e.g.

elastix -f fixed_image -m moving_image -p parameter.txt -o output_folder

I’m sorry, but I have a realy begginer question, hope it’s ok to ask.
Where can I run terminal commands? the command line of windows doesn’t recognize elastix.


should I first install elastix seperatly, and then do it?

I am not a windows user so I don’t know the path of elastix in windows, simply search for “elastix” in your system then provide the full path to run e.g. elastix_Path\elastix.exe -f …

You can also download elastix binaries for windows from elastix official website and read the elastix manual to learn how to use it.

On Windows, Elastix is bundled with the SlicerElastix extension. Elastix path and complete list of command-line arguments that are passed to Elastix are all shown if you enable “Show detailed log during registration” in Advanced section.