简体   繁体   English

使用vtk()加载nifti图像

[英]Load nifti image with vtk ()

I'm trying to load a NIFTI image in vtk using python. 我正在尝试使用python在vtk中加载NIFTI图像。 The data is already load using nibabel, and I can visualize it with a pyplot (1 slice per plot, my data is a ndarray). 数据已经使用nibabel加载了,我可以使用pyplot对其进行可视化(每个绘图1个切片,我的数据是ndarray)。 But I want to load it has an ImagePlaneWidget ... something like this. 但是我想加载它有一个ImagePlaneWidget ...就像这样。

在此输入图像描述

I can't load the data like imageplanewidget wants (vtkDataSet)... How can I do that? 我无法加载像imageplanewidget想要的数据(vtkDataSet)......我怎么能这样做?

import nibabel as nib
import matplotlib.pyplot as plt
import vtk

img = nib.load('image.nii.gz')
img_data = img.get_data()
print img_data.shape

def show_slices(slices):
    fig,axes = plt.subplots(1, len(slices))
    for i, slice in enumerate(slices):
        axes[i].imshow(slice.T, cmap="gray", origin="lower")

slice_0=img_data[100, :, :]
slice_1=img_data[:, 100, :]
slice_2=img_data[:, :, 100]
show_slices([slice_0, slice_1, slice_2])
plt.suptitle("Image")
plt.show()

plane = vtk.vtkImagePlaneWidget()
plane.SetInputData(img_data)

Thanks a lot, I'm newbie in python and vtk 非常感谢,我是python和vtk的新手

And.. I can do it... 而且......我能做到......

import vtk
import nibabel as nib

img = nib.load('image.nii.gz')
img_data = img.get_data()
img_data_shape = img_data.shape

dataImporter = vtk.vtkImageImport()
dataImporter.SetDataScalarTypeToShort()
data_string = img_data.tostring()
dataImporter.SetNumberOfScalarComponents(1)
dataImporter.CopyImportVoidPointer(data_string, len(data_string))
dataImporter.SetDataExtent(0, img_data_shape[0] - 1, 0, img_data_shape[1] - 1, 0, img_data_shape[2] - 1)
dataImporter.SetWholeExtent(0, img_data_shape[0] - 1, 0, img_data_shape[1] - 1, 0, img_data_shape[2] - 1)
dataImporter.Update()
temp_data = dataImporter.GetOutput()
new_data = vtk.vtkImageData()
new_data.DeepCopy(temp_data)

#outline
outline=vtk.vtkOutlineFilter()
outline.SetInputData(new_data)
outlineMapper=vtk.vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)

#Picker
picker = vtk.vtkCellPicker()
picker.SetTolerance(0.005)

#PlaneWidget
planeWidgetX = vtk.vtkImagePlaneWidget()
planeWidgetX.DisplayTextOn()
planeWidgetX.SetInputData(new_data)
planeWidgetX.SetPlaneOrientationToXAxes()
planeWidgetX.SetSliceIndex(100)
planeWidgetX.SetPicker(picker)
planeWidgetX.SetKeyPressActivationValue("x")
prop1 = planeWidgetX.GetPlaneProperty()
prop1.SetColor(1, 0, 0)

planeWidgetY = vtk.vtkImagePlaneWidget()
planeWidgetY.DisplayTextOn()
planeWidgetY.SetInputData(new_data)
planeWidgetY.SetPlaneOrientationToYAxes()
planeWidgetY.SetSliceIndex(100)
planeWidgetY.SetPicker(picker)
planeWidgetY.SetKeyPressActivationValue("y")
prop2 = planeWidgetY.GetPlaneProperty()
prop2.SetColor(1, 1, 0)
planeWidgetY.SetLookupTable(planeWidgetX.GetLookupTable())

planeWidgetZ = vtk.vtkImagePlaneWidget()
planeWidgetZ.DisplayTextOn()
planeWidgetZ.SetInputData(new_data)
planeWidgetZ.SetPlaneOrientationToZAxes()
planeWidgetZ.SetSliceIndex(100)
planeWidgetZ.SetPicker(picker)
planeWidgetZ.SetKeyPressActivationValue("z")
prop2 = planeWidgetY.GetPlaneProperty()
prop2.SetColor(0, 0, 1)
planeWidgetZ.SetLookupTable(planeWidgetX.GetLookupTable())

#Renderer
renderer = vtk.vtkRenderer()
renderer.SetBackground(0, 0, 1)

#RenderWindow
renwin = vtk.vtkRenderWindow()
renwin.AddRenderer(renderer)

#Add outlineactor
renderer.AddActor(outlineActor)
renwin.SetSize(800,800)

interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renwin)

#Load widget interactors and enable
planeWidgetX.SetInteractor(interactor)
planeWidgetX.On()
planeWidgetY.SetInteractor(interactor)
planeWidgetY.On()
planeWidgetZ.SetInteractor(interactor)
planeWidgetZ.On()

interactor.Initialize()
renwin.Render()
interactor.Start()

You have two possibilities: 你有两种可能性:

  1. Re-read the image directly from a vtk reader. 直接从vtk阅读器重新读取图像。 I am not sure that the vtkNiftiReader is available in the current (and in your) python distribution, and it doesn't seem that nibabel exports files in a format known by vtk 我不确定vtkNiftiReader是否在当前(以及你的)python发行版中可用,并且nibabel似乎不会以vtk已知的格式导出文件
  2. Use the imageimporter to import an image from an array (I guess img_data is an array) see an example in http://wiki.scipy.org/Cookbook/vtkVolumeRendering or https://github.com/cni/psych204a/blob/master/vtk_render.py 使用imageimporter从数组中导入图像(我猜img_data是一个数组)请参阅http://wiki.scipy.org/Cookbook/vtkVolumeRenderinghttps://github.com/cni/psych204a/blob/中的示例主/ vtk_render.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM