ants.core.ants_image_io

Image IO

Functions

clone(image[, pixeltype])

Create a copy of the given ANTsImage with the same data and info, possibly with a different data type for the image data.

copy(image[, pixeltype])

Create a copy of the given ANTsImage with the same data and info, possibly with a different data type for the image data.

dicom_read(directory[, pixeltype])

Read a set of dicom files in a directory into a single ANTsImage.

from_numpy(data[, origin, spacing, ...])

Create an ANTsImage object from a numpy array

from_numpy_like(data, image)

image_clone(image[, pixeltype])

Clone an ANTsImage

image_header_info(filename)

Read file info from image header

image_read(filename[, dimension, pixeltype, ...])

Read an ANTsImage from file

image_write(image, filename[, ri])

Write an ANTsImage to file

make_image(shape[, voxval, spacing, origin, ...])

Make an image with given size and voxel values or fill a mask with a vector of voxel values.

new_image_like(image, data)

Create a new ANTsImage with the same header information, but with a new image array.

read_image_metadata(filename)

Read metadata dictionary from image file.

dicom_read(directory, pixeltype='float')[source]

Read a set of dicom files in a directory into a single ANTsImage. The origin of the resulting 3D image will be the origin of the first dicom image read.

Parameters:

directory (str) – folder in which all the dicom images exist

Return type:

ants.core.ANTsImage

Example

>>> import ants
>>> img = ants.dicom_read('~/desktop/dicom-subject/')
from_numpy(data, origin=None, spacing=None, direction=None, has_components=False, is_rgb=False)[source]

Create an ANTsImage object from a numpy array

ANTsR function: as.antsImage

Parameters:
  • data (numpy.ndarray) – image data array

  • origin (tuple/list) – image origin

  • spacing (tuple/list) – image spacing

  • direction (list/ndarray) – image direction

  • has_components (bool) – whether the image has vector components.

  • is_rgb (bool) – whether the image is RGB. This implies has_components=True.

Returns:

image with given data and any given information

Return type:

ants.core.ANTsImage

from_numpy_like(data, image)[source]
image_clone(image, pixeltype=None)[source]

Clone an ANTsImage

ANTsR function: antsImageClone

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

  • pixeltype (string (optional)) – new datatype for image

Return type:

ants.core.ANTsImage

image_header_info(filename)[source]

Read file info from image header

ANTsR function: antsImageHeaderInfo

Parameters:

filename (str) – name of image file from which info will be read

Return type:

dict

image_read(filename, dimension=None, pixeltype='float', reorient=False)[source]

Read an ANTsImage from file

ANTsR function: antsImageRead

Parameters:
  • filename (str) – Name of the file to read the image from.

  • dimension (int) – Number of dimensions of the image read. This need not be the same as the dimensions of the image in the file. Allowed values: 2, 3, 4. If not provided, the dimension is obtained from the image file

  • pixeltype (str) – C++ datatype to be used to represent the pixels read. This datatype need not be the same as the datatype used in the file. Options: unsigned char, unsigned int, float, double. Use pixeltype=None to use the datatype in the file if supported. Unsupported datatypes will be converted to float.

  • reorient (boolean | string) –

    if True, the image will be reoriented to RPI if it is 3D if False, nothing will happen if string, this should be the 3-letter orientation to which the

    input image will reoriented if 3D.

    if the image is 2D, this argument is ignored

Return type:

ants.core.ANTsImage

image_write(image, filename, ri=False)[source]

Write an ANTsImage to file

ANTsR function: antsImageWrite

Parameters:
  • image (ants.core.ANTsImage) – image to save to file

  • filename (str) – name of file to which image will be saved

  • ri (bool) –

    if True, return image. This allows for using this function in a pipeline:
    >>> img2 = img.smooth_image(2.).image_write(file1, ri=True).threshold_image(0,20).image_write(file2, ri=True)
    

    if False, do not return image

make_image(shape, voxval=0, spacing=None, origin=None, direction=None, has_components=False, pixeltype='float')[source]

Make an image with given size and voxel values or fill a mask with a vector of voxel values.

ANTsR function: makeImage

Parameters:
  • shape (tuple/ANTsImage) – input image shape (tuple) or a mask (ANTsImage). If a mask, the output image has the same shape as the mask, and the number of voxel values in voxval should match the number of positive voxels in the mask. If a tuple, the number of voxel values in voxval should match the product of the dimensions in the tuple.

  • voxval (scalar) – input image value. Either a scalar (single value) to be placed in all voxels, or a vector of with length matching the number of voxels required by the shape argument.

  • spacing (tuple/list) – image spatial resolution.

  • origin (tuple/list) – image spatial origin.

  • direction (list/ndarray) – direction matrix to convert from index to physical space.

  • components (bool) – whether there are components per pixel or not.

  • pixeltype (str) – a supported pixel type for ANTsImage.

Return type:

ants.core.ANTsImage

new_image_like(image, data)[source]

Create a new ANTsImage with the same header information, but with a new image array.

Parameters:

data (numpy.ndarray or py::capsule) – New array or pointer for the image. It must have the same shape as the current image data.

Return type:

ants.core.ANTsImage

read_image_metadata(filename)[source]

Read metadata dictionary from image file. This uses ITK ImageIO to efficiently read the metadata without reading the pixel data.

Parameters:

filename (str) – name of image file from which metadata will be read.

Return type:

dict