medical image常用工具

2020-10-03  本文已影响0人  yohn

python

R

matlab

shell


ANTspy, Nibabel, SimpleITK使用心得

ANTspy

import numpy as np
import ants

# test the shape change between ants and numpy
_data = np.random.randn(30, 40, 50)
_img = ants.from_numpy(_data)
print(_img.dimension) #3
print(_data.shape) #(30, 40, 50)
print(_img.shape) #(30, 40, 50)

Nibabel

import numpy as np
import nibabel as nib

# load image(assume image shape (x, y, z) in itk-snap)
nib_img = nib.load(filename_image)
# return image shape, (x, y, z) 
nib_img.shape
# convert to ndarry, shape(x, y, z)
nib_data = nib_img.get_fdata()
# convert to nifti image, (x, y, z)
nib_img = nib.Nifti1Image(nib_data, np.eye(4))# LPI
# save image
nib.save(nib_img, save_path)

SimpleITK

在进行读入图像(shape: (x, y,z))后,ants, sitk和nib的图像对象维度均为(x, y,z),但将图像转为ndarray后,sitk对应的ndarray变为(z,y,x),ants和nib对应的ndarray为(x, y,z)

import numpy as np
import SimpleITK as sitk

# load image(assume image shape (x, y, z) in itk-snap)
itk_img = sitk.ReadImage(filename_image)
# return image shape, (x, y, z) 
itk_img.GetSize()
# return image orientation
itk_img.GetDirection()
# set image orientation to LPI
itk_img.SetDirection((-1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 1.0))
# convert to ndarry, shape(z, y, x)
itk_data = sitk.GetArrayFromImage(itk_img)



# create image with shape(2, 4, 8, 5) , vecotr mean multi-channel image 
itk_img = sitk.Image([2,4,8], sitk.sitkVectorFloat32, 5)
# convert to ndarry which shape is (8, 4, 2, 5)
itk_data = sitk.GetArrayFromImage(itk_img)

# Get a SimpleITK Image from a numpy array. If isVector is True, then the Image will have a Vector pixel type(multi channel image), and the last dimension of the ndarray will be considered the component index(channel index). By default when isVector is None, 4D images are automatically considered 3D vector images
itk_img = sitk.GetImageFromArray(itk_data, isVector=False)

# save image
sitk.WriteImage(itk_img, save_path)
上一篇 下一篇

猜你喜欢

热点阅读