ants.utils.matrix_image

Functions

image_list_to_matrix(image_list[, mask, ...])

Read images into rows of a matrix, given a mask - much faster for large datasets as it is based on C++ implementations.

images_from_matrix(data_matrix, mask)

Unmasks rows of a matrix and writes as images

images_to_matrix(image_list[, mask, sigma, ...])

Read images into rows of a matrix, given a mask - much faster for large datasets as it is based on C++ implementations.

matrix_from_images(image_list[, mask, ...])

Read images into rows of a matrix, given a mask - much faster for large datasets as it is based on C++ implementations.

matrix_to_images(data_matrix, mask)

Unmasks rows of a matrix and writes as images

matrix_to_timeseries(image, matrix[, mask])

converts a matrix to a ND image.

timeseries_to_matrix(image[, mask])

Convert a timeseries image into a matrix.

image_list_to_matrix(image_list, mask=None, sigma=None, epsilon=0.5)

Read images into rows of a matrix, given a mask - much faster for large datasets as it is based on C++ implementations.

ANTsR function: imagesToMatrix

Parameters:
  • image_list (list of ANTsImage types) – images to convert to ndarray

  • mask (ANTsImage (optional)) – Mask image, voxels in the mask (>= epsilon) are placed in the matrix. If None, the first image in image_list is thresholded at its mean value to create a mask.

  • sigma (scaler (optional)) – smoothing factor

  • epsilon (scalar) – threshold for mask, values >= epsilon are included in the mask.

Returns:

array with a row for each image shape = (N_IMAGES, N_VOXELS)

Return type:

numpy.ndarray

Example

>>> import ants
>>> img = ants.image_read(ants.get_ants_data('r16'))
>>> img2 = ants.image_read(ants.get_ants_data('r16'))
>>> img3 = ants.image_read(ants.get_ants_data('r16'))
>>> mat = ants.image_list_to_matrix([img,img2,img3])
images_from_matrix(data_matrix, mask)

Unmasks rows of a matrix and writes as images

ANTsR function: matrixToImages

Parameters:
  • data_matrix (numpy.ndarray) – each row corresponds to an image array should have number of columns equal to non-zero voxels in the mask

  • mask (ants.core.ANTsImage) – image containing a binary mask. Rows of the matrix are unmasked and written as images. The mask defines the output image space

Return type:

list of ANTsImage types

Example

>>> import ants
>>> img = ants.image_read(ants.get_ants_data('r16'))
>>> msk = ants.get_mask( img )
>>> img2 = ants.image_read(ants.get_ants_data('r16'))
>>> img3 = ants.image_read(ants.get_ants_data('r16'))
>>> mat = ants.image_list_to_matrix([img,img2,img3], msk )
>>> ilist = ants.matrix_to_images( mat, msk )
images_to_matrix(image_list, mask=None, sigma=None, epsilon=0.5)[source]

Read images into rows of a matrix, given a mask - much faster for large datasets as it is based on C++ implementations.

ANTsR function: imagesToMatrix

Parameters:
  • image_list (list of ANTsImage types) – images to convert to ndarray

  • mask (ANTsImage (optional)) – Mask image, voxels in the mask (>= epsilon) are placed in the matrix. If None, the first image in image_list is thresholded at its mean value to create a mask.

  • sigma (scaler (optional)) – smoothing factor

  • epsilon (scalar) – threshold for mask, values >= epsilon are included in the mask.

Returns:

array with a row for each image shape = (N_IMAGES, N_VOXELS)

Return type:

numpy.ndarray

Example

>>> import ants
>>> img = ants.image_read(ants.get_ants_data('r16'))
>>> img2 = ants.image_read(ants.get_ants_data('r16'))
>>> img3 = ants.image_read(ants.get_ants_data('r16'))
>>> mat = ants.image_list_to_matrix([img,img2,img3])
matrix_from_images(image_list, mask=None, sigma=None, epsilon=0.5)

Read images into rows of a matrix, given a mask - much faster for large datasets as it is based on C++ implementations.

ANTsR function: imagesToMatrix

Parameters:
  • image_list (list of ANTsImage types) – images to convert to ndarray

  • mask (ANTsImage (optional)) – Mask image, voxels in the mask (>= epsilon) are placed in the matrix. If None, the first image in image_list is thresholded at its mean value to create a mask.

  • sigma (scaler (optional)) – smoothing factor

  • epsilon (scalar) – threshold for mask, values >= epsilon are included in the mask.

Returns:

array with a row for each image shape = (N_IMAGES, N_VOXELS)

Return type:

numpy.ndarray

Example

>>> import ants
>>> img = ants.image_read(ants.get_ants_data('r16'))
>>> img2 = ants.image_read(ants.get_ants_data('r16'))
>>> img3 = ants.image_read(ants.get_ants_data('r16'))
>>> mat = ants.image_list_to_matrix([img,img2,img3])
matrix_to_images(data_matrix, mask)[source]

Unmasks rows of a matrix and writes as images

ANTsR function: matrixToImages

Parameters:
  • data_matrix (numpy.ndarray) – each row corresponds to an image array should have number of columns equal to non-zero voxels in the mask

  • mask (ants.core.ANTsImage) – image containing a binary mask. Rows of the matrix are unmasked and written as images. The mask defines the output image space

Return type:

list of ANTsImage types

Example

>>> import ants
>>> img = ants.image_read(ants.get_ants_data('r16'))
>>> msk = ants.get_mask( img )
>>> img2 = ants.image_read(ants.get_ants_data('r16'))
>>> img3 = ants.image_read(ants.get_ants_data('r16'))
>>> mat = ants.image_list_to_matrix([img,img2,img3], msk )
>>> ilist = ants.matrix_to_images( mat, msk )
matrix_to_timeseries(image, matrix, mask=None)[source]

converts a matrix to a ND image.

ANTsR function: matrix2timeseries

Parameters:
  • image (reference ND image) –

  • matrix (matrix to convert to image) –

  • mask (mask image defining voxels of interest) –

Return type:

ants.core.ANTsImage

Example

>>> import ants
>>> img = ants.make_image( (10,10,10,5 ) )
>>> mask = ants.ndimage_to_list( img )[0] * 0
>>> mask[ 4:8, 4:8, 4:8 ] = 1
>>> mat = ants.timeseries_to_matrix( img, mask = mask )
>>> img2 = ants.matrix_to_timeseries( img,  mat, mask)
timeseries_to_matrix(image, mask=None)[source]

Convert a timeseries image into a matrix.

ANTsR function: timeseries2matrix

Parameters:
  • image (image whose slices we convert to a matrix. E.g. a 3D image of size) – x by y by z will convert to a z by x*y sized matrix

  • mask (ANTsImage (optional)) – image containing binary mask. voxels in the mask are placed in the matrix

Returns:

array with a row for each image shape = (N_IMAGES, N_VOXELS)

Return type:

numpy.ndarray

Example

>>> import ants
>>> img = ants.make_image( (10,10,10,5 ) )
>>> mat = ants.timeseries_to_matrix( img )