38 def setupOptionsFrame(self):
39 operationLayout = qt.QVBoxLayout()
47 self.
scriptedEffect.addLabeledOptionsWidget(_(
"Operation:"), operationLayout)
51 self.
marginSizeMMSpinBox.setToolTip(_(
"Segment boundaries will be shifted by this distance. "
52 "Positive value means the segments will grow, negative value means segment will shrink."))
58 self.
marginSizeLabel.setToolTip(_(
"Size change in pixel. Computed from the segment's spacing and the specified margin size."))
60 marginSizeFrame = qt.QHBoxLayout()
67 _(
"Grow or shrink all visible segments in this segmentation node. This operation may take a while."))
73 self.
applyButton.objectName = self.__class__.__name__ +
"Apply"
74 self.
applyButton.setToolTip(_(
"Grows or shrinks selected segment /default) or all segments (checkbox) by the specified margin."))
91 def getMarginSizePixel(self):
92 selectedSegmentLabelmapSpacing = [1.0, 1.0, 1.0]
93 selectedSegmentLabelmap = self.
scriptedEffect.selectedSegmentLabelmap()
94 if selectedSegmentLabelmap:
95 selectedSegmentLabelmapSpacing = selectedSegmentLabelmap.GetSpacing()
97 marginSizeMM = abs(self.
scriptedEffect.doubleParameter(
"MarginSizeMm"))
98 marginSizePixel = [math.floor(marginSizeMM / spacing)
for spacing
in selectedSegmentLabelmapSpacing]
99 return marginSizePixel
101 def updateGUIFromMRML(self):
102 marginSizeMM = self.
scriptedEffect.doubleParameter(
"MarginSizeMm")
115 selectedSegmentLabelmapSpacing = [1.0, 1.0, 1.0]
116 selectedSegmentLabelmap = self.
scriptedEffect.selectedSegmentLabelmap()
117 if selectedSegmentLabelmap:
118 selectedSegmentLabelmapSpacing = selectedSegmentLabelmap.GetSpacing()
120 if marginSizePixel[0] < 1
or marginSizePixel[1] < 1
or marginSizePixel[2] < 1:
125 self.
marginSizeLabel.text = _(
"Actual:") +
" {} x {} x {} mm ({}x{}x{} pixel)".format(*marginSizeMM, *marginSizePixel)
130 applyToAllVisibleSegments = qt.Qt.Unchecked
if self.
scriptedEffect.integerParameter(
"ApplyToAllVisibleSegments") == 0
else qt.Qt.Checked
151 def getMarginSizeMM(self):
152 selectedSegmentLabelmapSpacing = [1.0, 1.0, 1.0]
153 selectedSegmentLabelmap = self.
scriptedEffect.selectedSegmentLabelmap()
154 if selectedSegmentLabelmap:
155 selectedSegmentLabelmapSpacing = selectedSegmentLabelmap.GetSpacing()
158 marginSizeMM = [abs((marginSizePixel[i]) * selectedSegmentLabelmapSpacing[i])
for i
in range(3)]
160 if marginSizeMM[i] > 0:
161 marginSizeMM[i] = round(marginSizeMM[i], max(int(-math.floor(math.log10(marginSizeMM[i]))), 1))
168 def processMargin(self):
171 selectedSegmentLabelmap = self.
scriptedEffect.selectedSegmentLabelmap()
173 marginSizeMM = self.
scriptedEffect.doubleParameter(
"MarginSizeMm")
178 thresh = vtk.vtkImageThreshold()
179 thresh.SetInputData(selectedSegmentLabelmap)
180 thresh.ThresholdByLower(0)
181 thresh.SetInValue(backgroundValue)
182 thresh.SetOutValue(labelValue)
183 thresh.SetOutputScalarType(selectedSegmentLabelmap.GetScalarType())
188 thresh.SetInValue(labelValue)
189 thresh.SetOutValue(backgroundValue)
193 margin = vtkITK.vtkITKImageMargin()
194 margin.SetInputConnection(thresh.GetOutputPort())
195 margin.CalculateMarginInMMOn()
196 margin.SetOuterMarginMM(abs(marginSizeMM))
199 if marginSizeMM >= 0:
200 modifierLabelmap.ShallowCopy(margin.GetOutput())
203 thresh = vtk.vtkImageThreshold()
204 thresh.SetInputData(margin.GetOutput())
205 thresh.ThresholdByLower(0)
206 thresh.SetInValue(labelValue)
207 thresh.SetOutValue(backgroundValue)
208 thresh.SetOutputScalarType(selectedSegmentLabelmap.GetScalarType())
210 modifierLabelmap.ShallowCopy(thresh.GetOutput())
213 self.
scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
222 qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
225 applyToAllVisibleSegments = int(self.
scriptedEffect.parameter(
"ApplyToAllVisibleSegments")) != 0 \
226 if self.
scriptedEffect.parameter(
"ApplyToAllVisibleSegments")
else False
228 if applyToAllVisibleSegments:
230 inputSegmentIDs = vtk.vtkStringArray()
231 segmentationNode = self.
scriptedEffect.parameterSetNode().GetSegmentationNode()
232 segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(inputSegmentIDs)
234 selectedStartSegmentID = self.
scriptedEffect.parameterSetNode().GetSelectedSegmentID()
235 if inputSegmentIDs.GetNumberOfValues() == 0:
236 logging.info(
"Margin operation skipped: there are no visible segments.")
239 for index
in range(inputSegmentIDs.GetNumberOfValues()):
240 segmentID = inputSegmentIDs.GetValue(index)
242 .format(segmentName=segmentationNode.GetSegmentation().GetSegment(segmentID).GetName()))
243 self.
scriptedEffect.parameterSetNode().SetSelectedSegmentID(segmentID)
246 self.
scriptedEffect.parameterSetNode().SetSelectedSegmentID(selectedStartSegmentID)
251 qt.QApplication.restoreOverrideCursor()