ants.contrib.sampling.transforms

Various data augmentation transforms for ANTsImage types

List of Transformations:

  • CastIntensity

  • BlurIntensity

  • NormalizeIntensity

  • RescaleIntensity

  • ShiftScaleIntensity

  • SigmoidIntensity

Todo

  • RotateImage

  • ShearImage

  • ScaleImage

  • DeformImage

  • PadImage

  • HistogramEqualizeIntensity

  • TruncateIntensity

  • SharpenIntensity

  • MorpholigicalIntensity
    • MD

    • ME

    • MO

    • MC

    • GD

    • GE

    • GO

    • GC

Classes

BlurIntensity(sigma, width)

Transform for blurring the intensity of an ANTsImage using a Gaussian Filter

CastIntensity(pixeltype)

Cast the pixeltype of an ANTsImage to a given type.

FlipImage(axis1, axis2)

Transform an image by flipping two axes.

LocallyBlurIntensity([conductance, iters])

Blur an ANTsImage locally using a gradient anisotropic diffusion filter, thereby preserving the sharpeness of edges as best as possible.

MultiResolutionImage([levels, keep_shape])

Generate a set of images at multiple resolutions from an original image

NormalizeIntensity()

Normalize the intensity values of an ANTsImage to have zero mean and unit variance

RescaleIntensity(min_val, max_val)

Rescale the pixeltype of an ANTsImage linearly to be between a given minimum and maximum value.

ScaleImage(scale[, reference, interp])

Scale an image in physical space.

ShiftScaleIntensity(shift, scale)

Shift and scale the intensity of an ANTsImage

SigmoidIntensity(min_val, max_val, alpha, beta)

Transform an image using a sigmoid function

TranslateImage(translation[, reference, interp])

Translate an image in physical space.

class BlurIntensity(sigma, width)[source]

Bases: object

Transform for blurring the intensity of an ANTsImage using a Gaussian Filter

transform(X, y=None)[source]

Blur an image by applying a gaussian filter.

Parameters:
  • X (ants.core.ANTsImage) – image to transform

  • y (ANTsImage (optional)) – another image to transform.

Example

>>> import ants
>>> blur = ants.contrib.BlurIntensity(2,3)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_b = blur.transform(img2d)
>>> ants.plot(img2d)
>>> ants.plot(img2d_b)
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_b = blur.transform(img3d)
>>> ants.plot(img3d)
>>> ants.plot(img3d_b)
class CastIntensity(pixeltype)[source]

Bases: object

Cast the pixeltype of an ANTsImage to a given type. This code uses the C++ ITK library directly, so it is fast.

NOTE: This offers a ~2.5x speedup over using img.clone(pixeltype):

Timings vs Cloning

>>> import ants
>>> import time
>>> caster = ants.contrib.CastIntensity('float')
>>> img = ants.image_read(ants.get_data('mni')).clone('unsigned int')
>>> s = time.time()
>>> for i in range(1000):
...     img_float = caster.transform(img)
>>> e = time.time()
>>> print(e - s) # 9.6s
>>> s = time.time()
>>> for i in range(1000):
...     img_float = img.clone('float')
>>> e = time.time()
>>> print(e - s) # 25.3s
transform(X, y=None)[source]

Transform an image by casting its type

Parameters:
  • X (ants.core.ANTsImage) – image to cast

  • y (ANTsImage (optional)) – another image to cast.

Example

>>> import ants
>>> caster = ants.contrib.CastIntensity('float')
>>> img2d = ants.image_read(ants.get_data('r16')).clone('unsigned int')
>>> img2d_float = caster.transform(img2d)
>>> print(img2d.pixeltype, '- ', img2d_float.pixeltype)
>>> img3d = ants.image_read(ants.get_data('mni')).clone('unsigned int')
>>> img3d_float = caster.transform(img3d)
>>> print(img3d.pixeltype, ' - ' , img3d_float.pixeltype)
class FlipImage(axis1, axis2)[source]

Bases: object

Transform an image by flipping two axes.

transform(X, y=None)[source]

Transform an image by applying a sigmoid function.

Parameters:
  • X (ants.core.ANTsImage) – image to transform

  • y (ANTsImage (optional)) – another image to transform.

Example

>>> import ants
>>> flipper = ants.contrib.FlipImage(0,1)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = flipper.transform(img2d)
>>> ants.plot(img2d)
>>> ants.plot(img2d_r)
>>> flipper2 = ants.contrib.FlipImage(1,0)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = flipper2.transform(img2d)
>>> ants.plot(img2d)
>>> ants.plot(img2d_r)
class LocallyBlurIntensity(conductance=1, iters=5)[source]

Bases: object

Blur an ANTsImage locally using a gradient anisotropic diffusion filter, thereby preserving the sharpeness of edges as best as possible.

transform(X, y=None)[source]

Locally blur an image by applying a gradient anisotropic diffusion filter.

Parameters:
  • X (ants.core.ANTsImage) – image to transform

  • y (ANTsImage (optional)) – another image to transform.

Example

>>> import ants
>>> blur = ants.contrib.LocallyBlurIntensity(1,5)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_b = blur.transform(img2d)
>>> ants.plot(img2d)
>>> ants.plot(img2d_b)
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_b = blur.transform(img3d)
>>> ants.plot(img3d)
>>> ants.plot(img3d_b)
class MultiResolutionImage(levels=4, keep_shape=False)[source]

Bases: object

Generate a set of images at multiple resolutions from an original image

transform(X, y=None)[source]

Generate a set of multi-resolution ANTsImage types

Parameters:
  • X (ants.core.ANTsImage) – image to transform

  • y (ANTsImage (optional)) – another image to transform

Example

>>> import ants
>>> multires = ants.contrib.MultiResolutionImage(levels=4)
>>> img = ants.image_read(ants.get_data('r16'))
>>> imgs = multires.transform(img)
class NormalizeIntensity[source]

Bases: object

Normalize the intensity values of an ANTsImage to have zero mean and unit variance

NOTE: this transform is more-or-less the same in speed as an equivalent numpy+scikit-learn solution.

Timing vs Numpy+Scikit-Learn

>>> import ants
>>> import numpy as np
>>> from sklearn.preprocessing import StandardScaler
>>> import time
>>> img = ants.image_read(ants.get_data('mni'))
>>> arr = img.numpy().reshape(1,-1)
>>> normalizer = ants.contrib.NormalizeIntensity()
>>> normalizer2 = StandardScaler()
>>> s = time.time()
>>> for i in range(100):
...     img_scaled = normalizer.transform(img)
>>> e = time.time()
>>> print(e - s) # 3.3s
>>> s = time.time()
>>> for i in range(100):
...     arr_scaled = normalizer2.fit_transform(arr)
>>> e = time.time()
>>> print(e - s) # 3.5s
transform(X, y=None)[source]

Transform an image by normalizing its intensity values to have zero mean and unit variance.

Parameters:
  • X (ants.core.ANTsImage) – image to transform

  • y (ANTsImage (optional)) – another image to transform.

Example

>>> import ants
>>> normalizer = ants.contrib.NormalizeIntensity()
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = normalizer.transform(img2d)
>>> print(img2d.mean(), ',', img2d.std(), ' -> ', img2d_r.mean(), ',', img2d_r.std())
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = normalizer.transform(img3d)
>>> print(img3d.mean(), ',' , img3d.std(), ',', ' -> ', img3d_r.mean(), ',' , img3d_r.std())
class RescaleIntensity(min_val, max_val)[source]

Bases: object

Rescale the pixeltype of an ANTsImage linearly to be between a given minimum and maximum value. This code uses the C++ ITK library directly, so it is fast.

NOTE: this offered a ~5x speedup over using built-in arithmetic operations in ANTs. It is also more-or-less the same in speed as an equivalent numpy+scikit-learn solution.

Timing vs Built-in Operations

>>> import ants
>>> import time
>>> rescaler = ants.contrib.RescaleIntensity(0,1)
>>> img = ants.image_read(ants.get_data('mni'))
>>> s = time.time()
>>> for i in range(100):
...     img_float = rescaler.transform(img)
>>> e = time.time()
>>> print(e - s) # 2.8s
>>> s = time.time()
>>> for i in range(100):
...     maxval = img.max()
...     img_float = (img - maxval) / (maxval - img.min())
>>> e = time.time()
>>> print(e - s) # 13.9s

Timing vs Numpy+Scikit-Learn

>>> import ants
>>> import numpy as np
>>> from sklearn.preprocessing import MinMaxScaler
>>> import time
>>> img = ants.image_read(ants.get_data('mni'))
>>> arr = img.numpy().reshape(1,-1)
>>> rescaler = ants.contrib.RescaleIntensity(-1,1)
>>> rescaler2 = MinMaxScaler((-1,1)).fit(arr)
>>> s = time.time()
>>> for i in range(100):
...     img_scaled = rescaler.transform(img)
>>> e = time.time()
>>> print(e - s) # 2.8s
>>> s = time.time()
>>> for i in range(100):
...     arr_scaled = rescaler2.transform(arr)
>>> e = time.time()
>>> print(e - s) # 3s
transform(X, y=None)[source]

Transform an image by linearly rescaling its intensity to be between a minimum and maximum value

Parameters:
  • X (ants.core.ANTsImage) – image to transform

  • y (ANTsImage (optional)) – another image to transform.

Example

>>> import ants
>>> rescaler = ants.contrib.RescaleIntensity(0,1)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = rescaler.transform(img2d)
>>> print(img2d.min(), ',', img2d.max(), ' -> ', img2d_r.min(), ',', img2d_r.max())
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = rescaler.transform(img3d)
>>> print(img3d.min(), ',' , img3d.max(), ' -> ', img3d_r.min(), ',' , img3d_r.max())
class ScaleImage(scale, reference=None, interp='linear')[source]

Bases: object

Scale an image in physical space. This function calls highly optimized ITK/C++ code.

transform(X, y=None)[source]

Example

>>> import ants
>>> scaler = ants.contrib.ScaleImage((1.2,1.2))
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = scaler.transform(img2d)
>>> ants.plot(img2d, img2d_r)
>>> scaler = ants.contrib.ScaleImage((1.2,1.2,1.2))
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = scaler.transform(img3d)
>>> ants.plot(img3d, img3d_r)
class ShiftScaleIntensity(shift, scale)[source]

Bases: object

Shift and scale the intensity of an ANTsImage

transform(X, y=None)[source]

Transform an image by shifting and scaling its intensity values.

Parameters:
  • X (ants.core.ANTsImage) – image to transform

  • y (ANTsImage (optional)) – another image to transform.

Example

>>> import ants
>>> shiftscaler = ants.contrib.ShiftScaleIntensity(10,2.)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = shiftscaler.transform(img2d)
>>> print(img2d.min(), ',', img2d.max(), ' -> ', img2d_r.min(), ',', img2d_r.max())
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = shiftscaler.transform(img3d)
>>> print(img3d.min(), ',' , img3d.max(), ',', ' -> ', img3d_r.min(), ',' , img3d_r.max())
class SigmoidIntensity(min_val, max_val, alpha, beta)[source]

Bases: object

Transform an image using a sigmoid function

transform(X, y=None)[source]

Transform an image by applying a sigmoid function.

Parameters:
  • X (ants.core.ANTsImage) – image to transform

  • y (ANTsImage (optional)) – another image to transform.

Example

>>> import ants
>>> sigscaler = ants.contrib.SigmoidIntensity(0,1,1,1)
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = sigscaler.transform(img2d)
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = sigscaler.transform(img3d)
class TranslateImage(translation, reference=None, interp='linear')[source]

Bases: object

Translate an image in physical space. This function calls highly optimized ITK/C++ code.

transform(X, y=None)[source]

Example

>>> import ants
>>> translater = ants.contrib.TranslateImage((40,0))
>>> img2d = ants.image_read(ants.get_data('r16'))
>>> img2d_r = translater.transform(img2d)
>>> ants.plot(img2d, img2d_r)
>>> translater = ants.contrib.TranslateImage((40,0,0))
>>> img3d = ants.image_read(ants.get_data('mni'))
>>> img3d_r = translater.transform(img3d)
>>> ants.plot(img3d, img3d_r, axis=2)