2 import vtk, qt, ctk, slicer
4 from SegmentEditorEffects
import *
7 """ MaringEffect grows or shrinks the segment by a specified margin 11 scriptedEffect.name =
'Margin' 12 AbstractScriptedSegmentEditorEffect.__init__(self, scriptedEffect)
15 import qSlicerSegmentationsEditorEffectsPythonQt
as effects
16 clonedEffect = effects.qSlicerSegmentEditorScriptedEffect(
None)
17 clonedEffect.setPythonSource(__file__.replace(
'\\',
'/'))
21 iconPath = os.path.join(os.path.dirname(__file__),
'Resources/Icons/Margin.png')
22 if os.path.exists(iconPath):
23 return qt.QIcon(iconPath)
27 return "Grow or shrink selected segment by specified margin size." 31 operationLayout = qt.QVBoxLayout()
39 self.scriptedEffect.addLabeledOptionsWidget(
"Operation:", operationLayout)
43 self.
marginSizeMmSpinBox.setToolTip(
"Segment boundaries will be shifted by this distance. Positive value means the segments will grow, negative value means segment will shrink.")
49 self.
kernelSizePixel.setToolTip(
"Size change in pixels. Computed from the segment's spacing and the specified margin size.")
51 marginSizeFrame = qt.QHBoxLayout()
54 self.
marginSizeMmLabel = self.scriptedEffect.addLabeledOptionsWidget(
"Margin size:", marginSizeFrame)
57 self.
applyButton.objectName = self.__class__.__name__ +
'Apply' 58 self.
applyButton.setToolTip(
"Grows or shrinks selected segment by the specified margin.")
59 self.scriptedEffect.addOptionsWidget(self.
applyButton)
68 return slicer.util.mainWindow().cursor
71 self.scriptedEffect.setParameterDefault(
"MarginSizeMm", 3)
74 selectedSegmentLabelmapSpacing = [1.0, 1.0, 1.0]
75 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
76 if selectedSegmentLabelmap:
77 selectedSegmentLabelmapSpacing = selectedSegmentLabelmap.GetSpacing()
80 marginSizeMm = abs(self.scriptedEffect.doubleParameter(
"MarginSizeMm"))
81 kernelSizePixel = [int(round((marginSizeMm / selectedSegmentLabelmapSpacing[componentIndex]+1)/2)*2-1)
for componentIndex
in range(3)]
82 return kernelSizePixel
85 marginSizeMm = self.scriptedEffect.doubleParameter(
"MarginSizeMm")
100 if kernelSizePixel[0]<=1
and kernelSizePixel[1]<=1
and kernelSizePixel[2]<=1:
104 self.
kernelSizePixel.text =
"{0}x{1}x{2} pixels".format(abs(kernelSizePixel[0]), abs(kernelSizePixel[1]), abs(kernelSizePixel[2]))
107 self.setWidgetMinMaxStepFromImageSpacing(self.
marginSizeMmSpinBox, self.scriptedEffect.selectedSegmentLabelmap())
119 self.scriptedEffect.setParameter(
"MarginSizeMm", marginSizeMm)
123 self.scriptedEffect.saveStateForUndo()
126 modifierLabelmap = self.scriptedEffect.defaultModifierLabelmap()
127 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
129 marginSizeMm = self.scriptedEffect.doubleParameter(
"MarginSizeMm")
135 thresh = vtk.vtkImageThreshold()
136 thresh.SetInputData(selectedSegmentLabelmap)
137 thresh.ThresholdByLower(0)
138 thresh.SetInValue(backgroundValue)
139 thresh.SetOutValue(labelValue)
140 thresh.SetOutputScalarType(selectedSegmentLabelmap.GetScalarType())
142 erodeDilate = vtk.vtkImageDilateErode3D()
143 erodeDilate.SetInputConnection(thresh.GetOutputPort())
146 erodeDilate.SetDilateValue(labelValue)
147 erodeDilate.SetErodeValue(backgroundValue)
150 erodeDilate.SetDilateValue(backgroundValue)
151 erodeDilate.SetErodeValue(labelValue)
154 qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
156 erodeDilate.SetKernelSize(kernelSizePixel[0],kernelSizePixel[1],kernelSizePixel[2])
158 modifierLabelmap.DeepCopy(erodeDilate.GetOutput())
161 self.scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
163 qt.QApplication.restoreOverrideCursor()
def setupOptionsFrame(self)
def getKernelSizePixel(self)
def growOperationToggled(self, toggled)
def createCursor(self, widget)
def __init__(self, scriptedEffect)
def shrinkOperationToggled(self, toggled)
def setMRMLDefaults(self)
def updateMRMLFromGUI(self)
def updateGUIFromMRML(self)