134 def splitSegments(self, minimumSize=0, maxNumberOfSegments=0, split=True):
136 minimumSize: if 0 then it means that all islands are kept, regardless of size
137 maxNumberOfSegments: if 0 then it means that all islands are kept, regardless of how many
140 qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
145 selectedSegmentLabelmap = self.
scriptedEffect.selectedSegmentLabelmap()
147 castIn = vtk.vtkImageCast()
148 castIn.SetInputData(selectedSegmentLabelmap)
149 castIn.SetOutputScalarTypeToUnsignedInt()
153 islandMath = vtkITK.vtkITKIslandMath()
154 islandMath.SetInputConnection(castIn.GetOutputPort())
155 islandMath.SetFullyConnected(
False)
156 islandMath.SetMinimumSize(minimumSize)
159 islandImage = slicer.vtkOrientedImageData()
160 islandImage.ShallowCopy(islandMath.GetOutput())
161 selectedSegmentLabelmapImageToWorldMatrix = vtk.vtkMatrix4x4()
162 selectedSegmentLabelmap.GetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
163 islandImage.SetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
165 islandCount = islandMath.GetNumberOfIslands()
166 islandOrigCount = islandMath.GetOriginalNumberOfIslands()
167 ignoredIslands = islandOrigCount - islandCount
168 logging.debug(
"%d islands created (%d ignored)" % (islandCount, ignoredIslands))
170 baseSegmentName =
"Label"
171 selectedSegmentID = self.
scriptedEffect.parameterSetNode().GetSelectedSegmentID()
172 segmentationNode = self.
scriptedEffect.parameterSetNode().GetSegmentationNode()
173 with slicer.util.NodeModify(segmentationNode):
174 segmentation = segmentationNode.GetSegmentation()
175 selectedSegment = segmentation.GetSegment(selectedSegmentID)
176 selectedSegmentName = selectedSegment.GetName()
177 if selectedSegmentName
is not None and selectedSegmentName !=
"":
178 baseSegmentName = selectedSegmentName
180 labelValues = vtk.vtkIntArray()
181 slicer.vtkSlicerSegmentationsModuleLogic.GetAllLabelValues(labelValues, islandImage)
182 numberOfIslands = labelValues.GetNumberOfTuples()
194 for i
in range(1, numberOfIslands):
195 if maxNumberOfSegments > 0
and i >= maxNumberOfSegments:
199 labelValue = int(labelValues.GetTuple1(i))
200 segment = slicer.vtkSegment()
201 name = baseSegmentName +
"_" + str(i + 1)
202 segment.SetName(name)
203 segment.AddRepresentation(
204 slicer.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName(),
205 selectedSegment.GetRepresentation(slicer.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName()))
206 segmentation.AddSegment(segment)
207 segmentID = segmentation.GetSegmentIdBySegment(segment)
208 segment.SetLabelValue(segmentation.GetUniqueLabelValueForSharedLabelmap(selectedSegmentID))
210 threshold = vtk.vtkImageThreshold()
211 threshold.SetInputData(islandMath.GetOutput())
212 threshold.ThresholdBetween(labelValue, labelValue)
213 threshold.SetInValue(1)
214 threshold.SetOutValue(0)
218 modifierImage = slicer.vtkOrientedImageData()
219 modifierImage.DeepCopy(threshold.GetOutput())
220 modifierImage.SetGeometryFromImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
224 self.
scriptedEffect.modifySegmentByLabelmap(segmentationNode, segmentID, modifierImage,
225 slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeAdd)
228 threshold = vtk.vtkImageThreshold()
229 threshold.SetInputData(islandMath.GetOutput())
230 if numberOfIslands > 0
and not split
and maxNumberOfSegments <= 0:
233 threshold.ThresholdByLower(0)
234 threshold.SetInValue(0)
235 threshold.SetOutValue(1)
236 elif numberOfIslands > 0:
238 labelValue = int(labelValues.GetTuple1(0))
239 threshold.ThresholdBetween(labelValue, labelValue)
240 threshold.SetInValue(1)
241 threshold.SetOutValue(0)
245 threshold.ThresholdByLower(0)
246 threshold.SetInValue(0)
247 threshold.SetOutValue(0)
251 modifierImage = slicer.vtkOrientedImageData()
252 modifierImage.DeepCopy(threshold.GetOutput())
253 modifierImage.SetGeometryFromImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
254 self.
scriptedEffect.modifySegmentByLabelmap(segmentationNode, selectedSegmentID, modifierImage,
255 slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
257 qt.QApplication.restoreOverrideCursor()
259 def processInteractionEvents(self, callerInteractor, eventId, viewWidget):
260 import vtkSegmentationCorePython
as vtkSegmentationCore
269 if viewWidget.className() !=
"qMRMLSliceWidget":
273 eventId != vtk.vtkCommand.LeftButtonPressEvent
274 or callerInteractor.GetShiftKey()
275 or callerInteractor.GetControlKey()
276 or callerInteractor.GetAltKey()
281 confirmedEditingAllowed = self.
scriptedEffect.confirmCurrentSegmentVisible()
284 or confirmedEditingAllowed == self.
scriptedEffect.ConfirmedWithDialog
298 segmentationNode = self.
scriptedEffect.parameterSetNode().GetSegmentationNode()
299 visibleSegmentIds = vtk.vtkStringArray()
300 segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(visibleSegmentIds)
301 if visibleSegmentIds.GetNumberOfValues() == 0:
302 logging.info(
"Island operation skipped: there are no visible segments")
308 qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
312 if operationName == ADD_SELECTED_ISLAND:
313 inputLabelImage = slicer.vtkOrientedImageData()
314 if not segmentationNode.GenerateMergedLabelmapForAllSegments(inputLabelImage,
315 vtkSegmentationCore.vtkSegmentation.EXTENT_UNION_OF_SEGMENTS_PADDED,
316 None, visibleSegmentIds):
317 logging.error(
"Failed to apply island operation: cannot get list of visible segments")
318 qt.QApplication.restoreOverrideCursor()
321 selectedSegmentLabelmap = self.
scriptedEffect.selectedSegmentLabelmap()
325 thresh = vtk.vtkImageThreshold()
326 thresh.SetInputData(selectedSegmentLabelmap)
327 thresh.ThresholdByLower(0)
328 thresh.SetInValue(backgroundValue)
329 thresh.SetOutValue(labelValue)
330 thresh.SetOutputScalarType(selectedSegmentLabelmap.GetScalarType())
333 import vtkSegmentationCorePython
as vtkSegmentationCore
335 inputLabelImage = slicer.vtkOrientedImageData()
336 inputLabelImage.ShallowCopy(thresh.GetOutput())
337 selectedSegmentLabelmapImageToWorldMatrix = vtk.vtkMatrix4x4()
338 selectedSegmentLabelmap.GetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
339 inputLabelImage.SetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
341 xy = callerInteractor.GetEventPosition()
342 ijk = self.
xyToIjk(xy, viewWidget, inputLabelImage, segmentationNode.GetParentTransformNode())
343 pixelValue = inputLabelImage.GetScalarComponentAsFloat(ijk[0], ijk[1], ijk[2], 0)
346 floodFillingFilter = vtk.vtkImageThresholdConnectivity()
347 floodFillingFilter.SetInputData(inputLabelImage)
348 seedPoints = vtk.vtkPoints()
349 origin = inputLabelImage.GetOrigin()
350 spacing = inputLabelImage.GetSpacing()
351 seedPoints.InsertNextPoint(origin[0] + ijk[0] * spacing[0], origin[1] + ijk[1] * spacing[1], origin[2] + ijk[2] * spacing[2])
352 floodFillingFilter.SetSeedPoints(seedPoints)
353 floodFillingFilter.ThresholdBetween(pixelValue, pixelValue)
355 if operationName == ADD_SELECTED_ISLAND:
356 floodFillingFilter.SetInValue(1)
357 floodFillingFilter.SetOutValue(0)
358 floodFillingFilter.Update()
360 modifierLabelmap.DeepCopy(floodFillingFilter.GetOutput())
361 self.
scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeAdd)
363 elif pixelValue != 0:
364 if operationName == KEEP_SELECTED_ISLAND:
365 floodFillingFilter.SetInValue(1)
366 floodFillingFilter.SetOutValue(0)
368 floodFillingFilter.SetInValue(1)
369 floodFillingFilter.SetOutValue(0)
371 floodFillingFilter.Update()
373 modifierLabelmap.DeepCopy(floodFillingFilter.GetOutput())
375 if operationName == KEEP_SELECTED_ISLAND:
376 self.
scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
378 self.
scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeRemove)
381 logging.error(
"Island processing failed")
383 qt.QApplication.restoreOverrideCursor()