ants.label.labels_to_matrix

labels_to_matrix(image, mask, target_labels=None, missing_val=nan)[source]

Convert a labeled image to an n x m binary matrix where n = number of voxels and m = number of labels. Only includes values inside the provided mask while including background ( image == 0 ) for consistency with timeseries2matrix and other image to matrix operations.

ANTsR function: labels2matrix

Parameters:
  • image (ants.core.ANTsImage) – input label image

  • mask (ants.core.ANTsImage) – defines domain of interest

  • target_labels (list/tuple) – defines target regions to be returned. if the target label does not exist in the input label image, then the matrix will contain a constant value of missing_val (default None) in that row.

  • missing_val (scalar) – value to use for missing label values

Return type:

numpy.ndarray

Example

>>> import ants
>>> fi = ants.image_read(ants.get_ants_data('r16')).resample_image((60,60),1,0)
>>> mask = ants.get_mask(fi)
>>> labs = ants.kmeans_segmentation(fi,3)['segmentation']
>>> labmat = ants.labels_to_matrix(labs, mask)