目的:
平常为了将体数据显示到Slicer中,可以直接把体数据拖到Slicer中进行显示
现在是准备用代码将体数据显示到Slicer中
import logging
import os
import vtk
import slicer
from slicer.ScriptedLoadableModule import *
from slicer.util import VTKObservationMixin
import numpy as np
class Test(ScriptedLoadableModule):
"""Uses ScriptedLoadableModule base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def __init__(self, parent):
ScriptedLoadableModule.__init__(self, parent)
self.parent.title = "Test" # TODO: make this more human readable by adding spaces
self.parent.categories = ["Examples"] # TODO: set categories (folders where the module shows up in the module selector)
self.parent.dependencies = [] # TODO: add here list of module names that this module requires
self.parent.contributors = ["John Doe (AnyWare Corp.)"] # TODO: replace with "Firstname Lastname (Organization)"
# TODO: update with short description of the module and a link to online module documentation
self.parent.helpText = """
This is an example of scripted loadable module bundled in an extension.
See more information in <a href="https://github.com/organization/projectname#Test">module documentation</a>.
"""
# TODO: replace with organization, grant and thanks
self.parent.acknowledgementText = """
This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc., Andras Lasso, PerkLab,
and Steve Pieper, Isomics, Inc. and was partially funded by NIH grant 3P41RR013218-12S1.
"""
class TestWidget(ScriptedLoadableModuleWidget, VTKObservationMixin):
"""Uses ScriptedLoadableModuleWidget base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def __init__(self, parent=None):
"""
Called when the user opens the module the first time and the widget is initialized.
"""
ScriptedLoadableModuleWidget.__init__(self, parent)
VTKObservationMixin.__init__(self) # needed for parameter node observation
self.logic = None
self._parameterNode = None
self._updatingGUIFromParameterNode = False
def setup(self):
"""
Called when the user opens the module the first time and the widget is initialized.
"""
ScriptedLoadableModuleWidget.setup(self)
import qt
imageData = vtk.vtkImageData()
#imageSize、imageOrigin、imageSpacing和imageDirections这些信息都可以在Slicer中的Volume模块中看到
imageSize = [256,256,112 ]
imageOrigin = [119.531,119.531,-77.7]
imageSpacing=[0.9375,0.9375,1.4000]
imageDirections = [[-1,0,0], [0,-1,0], [0,0,1]]
#设定Dimension
imageData.SetDimensions(imageSize)
#数据的类型是int,因为是标量数据,所以填1
imageData.AllocateScalars(vtk.VTK_FLOAT, 1)
brainDataArray=np.load("/home/chenxiaohui/tmp/brainData.npy")
data_vtk = vtk.util.numpy_support.numpy_to_vtk(brainDataArray.ravel(),array_type=vtk.VTK_FLOAT)
imageData.GetPointData().SetScalars(data_vtk)
referenceVolumeNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScalarVolumeNode")
referenceVolumeNode.SetOrigin(imageOrigin)
referenceVolumeNode.SetSpacing(imageSpacing)
referenceVolumeNode.SetAndObserveImageData(imageData)
referenceVolumeNode.SetName("brain")
referenceVolumeNode.SetIJKToRASDirections(imageDirections)
效果