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 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
108 if selectedSegmentLabelmap:
110 selectedSegmentLabelmapSpacing = selectedSegmentLabelmap.GetSpacing()
111 singleStep = min(selectedSegmentLabelmapSpacing)
113 singleStep = pow(10,math.floor(math.log(singleStep)/math.log(10)))
126 self.scriptedEffect.setParameter(
"MarginSizeMm", marginSizeMm)
130 self.scriptedEffect.saveStateForUndo()
133 modifierLabelmap = self.scriptedEffect.defaultModifierLabelmap()
134 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
136 marginSizeMm = self.scriptedEffect.doubleParameter(
"MarginSizeMm")
142 thresh = vtk.vtkImageThreshold()
143 thresh.SetInputData(selectedSegmentLabelmap)
144 thresh.ThresholdByLower(0)
145 thresh.SetInValue(backgroundValue)
146 thresh.SetOutValue(labelValue)
147 thresh.SetOutputScalarType(selectedSegmentLabelmap.GetScalarType())
149 erodeDilate = vtk.vtkImageDilateErode3D()
150 erodeDilate.SetInputConnection(thresh.GetOutputPort())
153 erodeDilate.SetDilateValue(labelValue)
154 erodeDilate.SetErodeValue(backgroundValue)
157 erodeDilate.SetDilateValue(backgroundValue)
158 erodeDilate.SetErodeValue(labelValue)
161 qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
163 erodeDilate.SetKernelSize(kernelSizePixel[0],kernelSizePixel[1],kernelSizePixel[2])
165 modifierLabelmap.DeepCopy(erodeDilate.GetOutput())
168 self.scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
170 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)