Slicer中修改Segment

通过updateSegmentBinaryLabelmapFromArray来进行修改
例子1

import logging
import os

import vtk
import qt
import slicer
from slicer.ScriptedLoadableModule import *
from slicer.util import VTKObservationMixin

class SSeg(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 = "SSeg"  # 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#SSeg">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 SSegWidget(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):

        ScriptedLoadableModuleWidget.setup(self)

        #设定按钮
        self.applyButton = qt.QPushButton("apply")
        self.applyButton.toolTip = "load to Slicer"
        self.applyButton.enabled = True
        #添加到界面中
        self.layout.addWidget(self.applyButton)
            
        #信号和槽函数
        self.applyButton.connect('clicked(bool)', self.onApplyButton)
        #垂直布局
        self.layout.addStretch(1)

        imageFile="/home/chenxiaohui/tmp/liver-orig006.mhd"
        labelFile="/home/chenxiaohui/tmp/liver-seg006.nii"
        imageNode = slicer.util.loadVolume(imageFile)
        # segNode = slicer.util.loadSegmentation(labelFile)

        # segmentId = segNode.GetSegmentation().GetSegmentIdBySegmentName('Segment_1')
        # segmentArray = slicer.util.arrayFromSegmentBinaryLabelmap(segNode, segmentId, imageNode)

        # Modify the segmentation
        segmentArray[:] = 0  # clear the segmentation
        segmentArray[ slicer.util.arrayFromVolume(imageNode) > 140 ] = 1  # create segment by simple thresholding of an image


    def onApplyButton(self):

        print("onApplyButton============>")

例子2

import logging
import os

import vtk
import qt
import slicer
from slicer.ScriptedLoadableModule import *
from slicer.util import VTKObservationMixin

class SSeg(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 = "SSeg"  # 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#SSeg">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 SSegWidget(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):

        ScriptedLoadableModuleWidget.setup(self)

        #设定按钮
        self.applyButton = qt.QPushButton("apply")
        self.applyButton.toolTip = "load to Slicer"
        self.applyButton.enabled = True
        #添加到界面中
        self.layout.addWidget(self.applyButton)
            
        #信号和槽函数
        self.applyButton.connect('clicked(bool)', self.onApplyButton)
        #垂直布局
        self.layout.addStretch(1)

        imageFile="/home/chenxiaohui/tmp/liver-orig006.mhd"
        labelFile="/home/chenxiaohui/tmp/liver-seg006.nii"
        imageNode = slicer.util.loadVolume(imageFile)
 
        self.segmentNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
        #设定参考图像 因为segmentNode要有spacing dicrection和origin这些 所以要给定参考图像
        self.segmentNode.SetReferenceImageGeometryParameterFromVolumeNode(imageNode)
        self.segmentNode.SetName("liver-seg006")

        self.segmentation = self.segmentNode.GetSegmentation()
        self.segmentation.AddEmptySegment("test")

        segmentId = self.segmentNode.GetSegmentation().GetSegmentIdBySegmentName('test')
        self.segment=self.segmentNode.GetSegmentation().GetSegment(segmentId)
        self.segment.SetColor(0,0,1)

        segmentArray = slicer.util.arrayFromSegmentBinaryLabelmap(self.segmentNode, segmentId, imageNode)
        segmentArray[:] = 0  # clear the segmentation
        segmentArray[ slicer.util.arrayFromVolume(imageNode) > 200 ] = 1  # create segment by simple thresholding of an image
        slicer.util.updateSegmentBinaryLabelmapFromArray(segmentArray, self.segmentNode, segmentId, imageNode)



    def onApplyButton(self):

        print("onApplyButton============>")
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值