通过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============>")
1082

被折叠的 条评论
为什么被折叠?



