在Slicer中创建vtkMRMLScalarVolumeNode

目的:
平常为了将体数据显示到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)

效果

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值