def get_major_minor_axis(img):
"""
Finds the major and minor axis as 3d vectors of the passed in image
:param img: CZYX numpy array
:return: tuple containing two numpy arrays representing the major and minor axis as 3d vectors
"""
# do a mean projection if more than 3 axes
if img.ndim > 3:
z, y, x = np.nonzero(np.mean(img, axis=tuple(range(img.ndim - 3))))
else:
z, y, x = np.nonzero(img)
coords = np.stack([x - np.mean(x), y - np.mean(y), z - np.mean(z)])
# eigenvectors and values of the covariance matrix
evals, evecs = np.linalg.eig(np.cov(coords))
# return largest and smallest eigenvectors (major and minor axis)
order = np.argsort(evals)
return (evecs[:, order[-1]], evecs[:, order[0]])
评论列表
文章目录