def covariance_matrices(im, labels, return_mm3=True):
"""
Considers the label as a point distribution in the space, and returns the covariance matrix of the points
distributions.
:param im: input nibabel image
:param labels: list of labels input.
:param return_mm3: if true the answer is in mm if false in voxel indexes.
:return: covariance matrix of the point distribution of the label
"""
cov_matrices = [np.zeros([3, 3])] * len(labels)
for l_id, l in enumerate(labels):
coords = np.where(im.get_data() == l) # returns [X_vector, Y_vector, Z_vector]
if np.count_nonzero(coords) > 0:
cov_matrices[l_id] = np.cov(coords)
else:
cov_matrices[l_id] = np.nan * np.ones([3, 3])
if return_mm3:
cov_matrices = [im.affine[:3, :3].dot(cm.astype(np.float64)) for cm in cov_matrices]
return cov_matrices
评论列表
文章目录