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_rgb.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
nclass=8
seg=ants.atropos( fa, mask, i='Kmeans['+str(nclass)+']', m='[0.0,1x1x1]', c='[5,0]', verbose=1 )
iprob=seg['probabilityimages']
# or randomly 
iprob=[]
for k in range(nclass):
  temp = np.random.uniform(low=0.0, high=1.0, size=fa.shape )
  temp = ants.from_numpy(temp)
  temp=ants.copy_image_info(fa, temp)
  iprob.append( temp )
# prior_based_segmentation(image, priors, mask, priorweight=0.0, mrf=0.1, iterations=25)
pwt=0.0
tenseg=ants.prior_based_segmentation( tt, iprob, mask, priorweight=pwt, mrf=0.05, 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')