【Python】使用VTK实现3D可视化医学图像(格式.nii.gz)

需要安装依赖 itkvtk,效果如下

【Python】使用VTK实现3D可视化医学图像(格式.nii.gz)_第1张图片

代码

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

参考:

A NIfTI (nii.gz) 3D Visualizer using VTK and Qt5
Load nifti image with vtk ()
ITK笔记–读取3D NIFTI图像并用VTK可视化

你可能感兴趣的:(医学图像,Python,python,开发语言)