Python 3D 医療画像可視化

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

参考:

Python、Mayavi を使用したスカラー医療画像の 3D ボリューム レンダリング - Jesse He のブログ - CSDN ブログ

おすすめ

転載: blog.csdn.net/qq_41021141/article/details/125983550