Home

Awesome

PyElastix - Python wrapper for the Elastix nonrigid registration toolkit

This Python module wraps the Elastix registration toolkit. Elastix is a powerful tool, suitable for 2D and 3D images, capable of rigid as well as non-rigid (i.e. elastic) image registration.

For an overview of other image registration projects in Python, see http://pyimreg.github.io.

Installation

Install this library using pip install pyelastix or conda install pyelastix -c conda-forge. This module depends on numpy.

Further, the Elastix command-line application needs to be installed on your computer. You can obtain a copy at http://elastix.isi.uu.nl/.

This module tries to detect the Elastix executable in a series of common locations, such as program directories, the user directory, and next to this module. The executable (or the directory that contains it) can also be provided by setting the ELASTIX_PATH environment variable.

How it works

This module writes the images to register to disk, calls Elastix to do the registration, and reads the resulting data. The temporary data is automatically cleaned up. This approach keeps this module relatively easy, while providing the full power of the awesome Elastix registration toolkit.

Example

# Given im1 and im2 images stored as numpy arrays ...

import pyelastix

# Get params and change a few values
params = pyelastix.get_default_params()
params.MaximumNumberOfIterations = 200
params.FinalGridSpacingInVoxels = 10

# Apply the registration (im1 and im2 can be 2D or 3D)
im1_deformed, field = pyelastix.register(im1, im2, params)

See example.py for a more complete example.

API


Parameters()

Struct object to represent the parameters for the Elastix registration toolkit. Sets of parameters can be combined by addition. (When adding p1 + p2, any parameters present in both objects will take the value that the parameter has in p2.)

Use get_default_params() to get a Parameters struct with sensible default values.

get_advanced_params()

Get Parameters struct with parameters that most users do not want to think about.

get_default_params(type='BSPLINE')

Get Parameters struct with parameters that users may want to tweak. The given type specifies the type of allowed transform, and can be 'RIGID', 'AFFINE', 'BSPLINE'.

For detail on what parameters are available and how they should be used, we refer to the Elastix documentation. Here is a description of the most common parameters:

get_elastix_exes()

Get the executables for elastix and transformix. Raises an error if they cannot be found.

get_tempdir()

Get the temporary directory where pyelastix stores its temporary files. The directory is specific to the current process and the calling thread. Generally, the user does not need this; directories are automatically cleaned up. Though Elastix log files are also written here.

register(im1, im2, params, exact_params=False, verbose=1)

Perform the registration of im1 to im2, using the given parameters. Returns (im1_deformed, field), where field is a tuple with arrays describing the deformation for each dimension (x-y-z order, in world units).

Parameters:

If im1 is a list of images, performs a groupwise registration. In this case the resulting field is a list of fields, each indicating the deformation to the "average" image.