2 import vtk, qt, ctk, slicer
5 from SegmentEditorEffects
import *
8 """ MaringEffect grows or shrinks the segment by a specified margin 12 scriptedEffect.name =
'Margin' 13 AbstractScriptedSegmentEditorEffect.__init__(self, scriptedEffect)
16 import qSlicerSegmentationsEditorEffectsPythonQt
as effects
17 clonedEffect = effects.qSlicerSegmentEditorScriptedEffect(
None)
18 clonedEffect.setPythonSource(__file__.replace(
'\\',
'/'))
22 iconPath = os.path.join(os.path.dirname(__file__),
'Resources/Icons/Margin.png')
23 if os.path.exists(iconPath):
24 return qt.QIcon(iconPath)
28 return "Grow or shrink selected segment by specified margin size." 32 operationLayout = qt.QVBoxLayout()
40 self.scriptedEffect.addLabeledOptionsWidget(
"Operation:", operationLayout)
44 self.
marginSizeMMSpinBox.setToolTip(
"Segment boundaries will be shifted by this distance. Positive value means the segments will grow, negative value means segment will shrink.")
50 self.
marginSizeLabel.setToolTip(
"Size change in pixel. Computed from the segment's spacing and the specified margin size.")
52 marginSizeFrame = qt.QHBoxLayout()
54 self.
marginSizeMMLabel = self.scriptedEffect.addLabeledOptionsWidget(
"Margin size:", marginSizeFrame)
58 self.
applyButton.objectName = self.__class__.__name__ +
'Apply' 59 self.
applyButton.setToolTip(
"Grows or shrinks selected segment by the specified margin.")
60 self.scriptedEffect.addOptionsWidget(self.
applyButton)
69 return slicer.util.mainWindow().cursor
72 self.scriptedEffect.setParameterDefault(
"MarginSizeMm", 3)
75 selectedSegmentLabelmapSpacing = [1.0, 1.0, 1.0]
76 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
77 if selectedSegmentLabelmap:
78 selectedSegmentLabelmapSpacing = selectedSegmentLabelmap.GetSpacing()
80 marginSizeMM = abs(self.scriptedEffect.doubleParameter(
"MarginSizeMm"))
81 marginSizePixel = [int(math.floor(marginSizeMM / spacing))
for spacing
in selectedSegmentLabelmapSpacing]
82 return marginSizePixel
85 marginSizeMM = self.scriptedEffect.doubleParameter(
"MarginSizeMm")
98 selectedSegmentLabelmapSpacing = [1.0, 1.0, 1.0]
99 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
100 if selectedSegmentLabelmap:
101 selectedSegmentLabelmapSpacing = selectedSegmentLabelmap.GetSpacing()
103 if marginSizePixel[0] < 1
or marginSizePixel[1] < 1
or marginSizePixel[2] < 1:
108 self.
marginSizeLabel.text =
"Actual: {0} x {1} x {2} mm ({3}x{4}x{5} pixel)".format(*marginSizeMM, *marginSizePixel)
113 self.setWidgetMinMaxStepFromImageSpacing(self.
marginSizeMMSpinBox, self.scriptedEffect.selectedSegmentLabelmap())
125 self.scriptedEffect.setParameter(
"MarginSizeMm", marginSizeMM)
128 selectedSegmentLabelmapSpacing = [1.0, 1.0, 1.0]
129 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
130 if selectedSegmentLabelmap:
131 selectedSegmentLabelmapSpacing = selectedSegmentLabelmap.GetSpacing()
134 marginSizeMM = [abs((marginSizePixel[i])*selectedSegmentLabelmapSpacing[i])
for i
in range(3)]
136 if marginSizeMM[i] > 0:
137 marginSizeMM[i] = round(marginSizeMM[i], max(int(-math.floor(math.log10(marginSizeMM[i]))),1))
142 if not self.scriptedEffect.confirmCurrentSegmentVisible():
145 self.scriptedEffect.saveStateForUndo()
148 modifierLabelmap = self.scriptedEffect.defaultModifierLabelmap()
149 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
151 marginSizeMM = self.scriptedEffect.doubleParameter(
"MarginSizeMm")
156 thresh = vtk.vtkImageThreshold()
157 thresh.SetInputData(selectedSegmentLabelmap)
158 thresh.ThresholdByLower(0)
159 thresh.SetInValue(backgroundValue)
160 thresh.SetOutValue(labelValue)
161 thresh.SetOutputScalarType(selectedSegmentLabelmap.GetScalarType())
162 if (marginSizeMM < 0):
166 thresh.SetInValue(labelValue)
167 thresh.SetOutValue(backgroundValue)
170 margin = vtkITK.vtkITKImageMargin()
171 margin.SetInputConnection(thresh.GetOutputPort())
172 margin.CalculateMarginInMMOn()
173 margin.SetOuterMarginMM(abs(marginSizeMM))
176 if marginSizeMM >= 0:
177 modifierLabelmap.ShallowCopy(margin.GetOutput())
180 thresh = vtk.vtkImageThreshold()
181 thresh.SetInputData(margin.GetOutput())
182 thresh.ThresholdByLower(0)
183 thresh.SetInValue(labelValue)
184 thresh.SetOutValue(backgroundValue)
185 thresh.SetOutputScalarType(selectedSegmentLabelmap.GetScalarType())
187 modifierLabelmap.ShallowCopy(thresh.GetOutput())
190 self.scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
192 qt.QApplication.restoreOverrideCursor()
def setupOptionsFrame(self)
def growOperationToggled(self, toggled)
def createCursor(self, widget)
def __init__(self, scriptedEffect)
def shrinkOperationToggled(self, toggled)
def getMarginSizePixel(self)
def setMRMLDefaults(self)
def updateMRMLFromGUI(self)
def updateGUIFromMRML(self)
def getMarginSizeMM(self)