Imports images of multiple types (2D or 3D) as nib.Nifti1Image

register_image_loader(name, requires, min_version=None, auto_discover=True)[source]

Function decorator to register an image loader.


Ordered dictionary were each entry is a function decorated with @register_image_loader. This is, every loader that NiftyNet supports. This dictionary will be dynamically filled and will be identical for every NiftyNet installation.

Used only for information or error messages and logging purposes.


A subset of the SUPPORTED_LOADERS that contain only the loaders that have the required library/module installed on the system. Dynamically filled from every function decorated with @register_image_loader that passes the import check. This list will be different for every installation, as it is platform dependant.

Inspedted and used to load images in runtime.

Adding a new loader only requires to decorate a function with @register_image_loader and it will populate SUPPORTED_LOADERS and AVAILABLE_LOADERS accordingly in runtime. The function will receive a filename as its only parameter, and will return an image and its 4x4 affinity matrix. Dummy example:

@register_image_loader(‘fake’, requires=’numpy’, min_version=‘1.13.3’,
def imread_numpy(filename):
np = require_module(‘numpy’) return image2nibabel(np.random.rand(100, 100, 3), np.eye(4))

It registers a loader named ‘fake’ that requires numpy version >= ‘1.13.3’ to be installed. It will first dynamically load numpy library and then return a (100, 100, 3) fake color image and an identity (4, 4) affinity matrix. loader = fake in the data section of a config file will select this loader and generate fake data.

When auto_discover=True (default) the method will be available to be automatically discovered and used if loader is not provided in the config file. This is, if no loader is specified, all the loaders registered with auto_discover=True will be looped in priority order.

load_image_obj(filename, loader=None)[source]

Loads an image from a given loader or checking multiple loaders.

If loader is specified the selected loader will be used if it exists in AVAILABLE_LOADERS (see above).

If no loader is specified, all the loaders registered with auto_discover=True (default) will be looped in priority order.


Default nibabel loader for NiftyNet.


OpenCV image loader with identity 2D affine.


Scikit-image loader with an identity affine matrix.


PIL (Pillow) image loader with an identity affine matrix.


SimpleITK requires two function calls to retrieve a numpy array.


Fake loader to load random data with numpy

image2nibabel(img, affine=array([[1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.], [0., 0., 0., 1.]]))[source]

Loads a RGB or Grayscale Image from a file and stores it in a 5D array, moving the color channels to the last axis for color images.

class ImageAsNibabel(img, affine)[source]

Bases: nibabel.nifti1.Nifti1Image

Wrapper class around a Nibabel file format. Loads an image using PIL (or scikit-image if available) and transforms it to a nib.Nifti1Image.

The resulting 2D color image is already translated to a 5D array, swapping the channels to the last axis.


Get affine transform in LPS