Skip to content

Multivariate segmentation example for a tensor image

stnava edited this page Aug 15, 2023 · 3 revisions
  • initialize the tensor segmentation with kmeans on the FA

  • segment the tensor image using the priors from the kmeans a multivariate distance measurement

import ants
import numpy as np
tt=ants.image_read("../PPMI_template0_tensor.nii.gz").split_channels()
fa=ants.image_read("../PPMI_template0_tensor_fa.nii.gz")
mask=ants.get_mask( ants.image_read( "PPMI_template0_brainstem_2_tensor.nii.gz" ))
# initial segmentation
seg=ants.atropos( fa, mask, i='Kmeans[5]', m='[0.0,1x1x1]', c='[5,0]', verbose=1 )
ants.image_write(seg['segmentation'],'/tmp/temp.nii.gz')
# prior_based_segmentation(image, priors, mask, priorweight=0.25, mrf=0.1, iterations=25)
tenseg=ants.prior_based_segmentation( tt, seg['probabilityimages'], mask, priorweight=0.25, mrf=0.1, iterations=25)
# this is a bug that should be fixed but here is a workaround
mat=ants.images_to_matrix(tenseg['probabilityimages'], mask )
segvec = np.argmax( mat, axis=0 )+1
segimg=ants.make_image( mask, segvec )
ants.image_write( segimg, '/tmp/temp2.nii.gz')