使用simpleitk库读取dicom图像,使用mayavi库和tvtk库可视化3d灰度图像
from mayavi import mlab
import SimpleITK as sitk
from tvtk.util.ctf import ColorTransferFunction, PiecewiseFunction
# image_path = 'xxx'
img = sitk.ReadImage(img_path)
img_arr = sitk.GetArrayFromImage(img)
C, H, W = img_arr.shape
print("img shape =", img_arr.shape) # C, H, W
img_arr = img_arr[:C, :H, :W//2]
## mayavi
# 表面绘制
# mlab.contour3d(img_arr)
# 体绘制
vol = mlab.pipeline.volume(mlab.pipeline.scalar_field(img_arr), name='3d-ultrasound')
ctf = ColorTransferFunction() # 该函数决定体绘制的颜色、灰度等
vol._ctf = ctf
vol._volume_property.set_color(ctf) #进行更改,体绘制的colormap及color
vol.update_ctf = True
otf = PiecewiseFunction()
otf.add_point(20,0.2)
vol._otf = otf
vol._volume_property.set_scalar_opacity(otf)
# mlab.volume_slice(img_arr, colormap='gray',
# plane_orientation='z_axes', slice_index=W//2) # 设定z轴切面
mlab.show()
参考: