需要安装依赖 itk
和 vtk
,效果如下
代码
import itk
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersGeneral import vtkDiscreteMarchingCubes
from vtkmodules.vtkRenderingCore import vtkActor, vtkPolyDataMapper, vtkRenderer, \
vtkRenderWindow, vtkRenderWindowInteractor
def show_3d_nifti_image(nifti_file_name):
# Read NIFTI file
itk_img = itk.imread(filename=nifti_file_name)
# Convert itk to vtk
vtk_img = itk.vtk_image_from_image(l_image=itk_img)
# Extract vtkImageData contour to vtkPolyData
contour = vtkDiscreteMarchingCubes()
contour.SetInputData(vtk_img)
# Define colors, mapper, actor, renderer, renderWindow, renderWindowInteractor
colors = vtkNamedColors()
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(contour.GetOutputPort())
actor = vtkActor()
actor.SetMapper(mapper)
renderer = vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(colors.GetColor3d("SteelBlue"))
renderWindow = vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()
if __name__ == '__main__':
# show_3d_nifti_image("F:/dataset/LiTS/lits_train/label/segmentation-0.nii")
参考:文章来源:https://www.toymoban.com/news/detail-505920.html
A NIfTI (nii.gz) 3D Visualizer using VTK and Qt5
Load nifti image with vtk ()
ITK笔记–读取3D NIFTI图像并用VTK可视化文章来源地址https://www.toymoban.com/news/detail-505920.html
到了这里,关于【Python】使用VTK实现3D可视化医学图像(格式.nii.gz)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!