Python 3D medical image visualization

Use the simpleitk library to read dicom images, and use the mayavi library and tvtk library to visualize 3d grayscale images

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()

reference:

Python, using Mayavi for 3D volume rendering of scalar medical images - Jesse He's Blog - CSDN Blog

Guess you like

Origin blog.csdn.net/qq_41021141/article/details/125983550
Recommended