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)
185 threshold = vtk.vtkImageThreshold()
186 threshold.SetInputData(selectedSegmentLabelmap)
187 threshold.ThresholdBetween(0, 0)
188 threshold.SetInValue(0)
189 threshold.SetOutValue(0)
191 emptyLabelmap = slicer.vtkOrientedImageData()
192 emptyLabelmap.ShallowCopy(threshold.GetOutput())
193 emptyLabelmap.CopyDirections(selectedSegmentLabelmap)
194 self.
scriptedEffect.modifySegmentByLabelmap(segmentationNode, selectedSegmentID, emptyLabelmap,
195 slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
197 for i
in range(labelValues.GetNumberOfTuples()):
198 if maxNumberOfSegments > 0
and i >= maxNumberOfSegments:
203 labelValue = int(labelValues.GetTuple1(i))
204 segment = selectedSegment
205 segmentID = selectedSegmentID
207 segment = slicer.vtkSegment()
208 name = baseSegmentName +
"_" + str(i + 1)
209 segment.SetName(name)
210 segment.AddRepresentation(
211 slicer.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName(),
212 selectedSegment.GetRepresentation(slicer.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName()))
213 segmentation.AddSegment(segment)
214 segmentID = segmentation.GetSegmentIdBySegment(segment)
215 segment.SetLabelValue(segmentation.GetUniqueLabelValueForSharedLabelmap(selectedSegmentID))
217 threshold = vtk.vtkImageThreshold()
218 threshold.SetInputData(islandMath.GetOutput())
219 if not split
and maxNumberOfSegments <= 0:
221 threshold.ThresholdByLower(0)
222 threshold.SetInValue(0)
223 threshold.SetOutValue(1)
226 threshold.ThresholdBetween(labelValue, labelValue)
227 threshold.SetInValue(1)
228 threshold.SetOutValue(0)
231 modificationMode = slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeAdd
233 modificationMode = slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet
236 modifierImage = slicer.vtkOrientedImageData()
237 modifierImage.DeepCopy(threshold.GetOutput())
238 selectedSegmentLabelmapImageToWorldMatrix = vtk.vtkMatrix4x4()
239 selectedSegmentLabelmap.GetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
240 modifierImage.SetGeometryFromImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
244 self.
scriptedEffect.modifySegmentByLabelmap(segmentationNode, segmentID, modifierImage, modificationMode)
246 if not split
and maxNumberOfSegments <= 0:
250 qt.QApplication.restoreOverrideCursor()
252 def processInteractionEvents(self, callerInteractor, eventId, viewWidget):
253 import vtkSegmentationCorePython
as vtkSegmentationCore
262 if viewWidget.className() !=
"qMRMLSliceWidget":
266 eventId != vtk.vtkCommand.LeftButtonPressEvent
267 or callerInteractor.GetShiftKey()
268 or callerInteractor.GetControlKey()
269 or callerInteractor.GetAltKey()
274 confirmedEditingAllowed = self.
scriptedEffect.confirmCurrentSegmentVisible()
277 or confirmedEditingAllowed == self.
scriptedEffect.ConfirmedWithDialog
291 segmentationNode = self.
scriptedEffect.parameterSetNode().GetSegmentationNode()
292 visibleSegmentIds = vtk.vtkStringArray()
293 segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(visibleSegmentIds)
294 if visibleSegmentIds.GetNumberOfValues() == 0:
295 logging.info(
"Island operation skipped: there are no visible segments")
301 qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
305 if operationName == ADD_SELECTED_ISLAND:
306 inputLabelImage = slicer.vtkOrientedImageData()
307 if not segmentationNode.GenerateMergedLabelmapForAllSegments(inputLabelImage,
308 vtkSegmentationCore.vtkSegmentation.EXTENT_UNION_OF_SEGMENTS_PADDED,
309 None, visibleSegmentIds):
310 logging.error(
"Failed to apply island operation: cannot get list of visible segments")
311 qt.QApplication.restoreOverrideCursor()
314 selectedSegmentLabelmap = self.
scriptedEffect.selectedSegmentLabelmap()
318 thresh = vtk.vtkImageThreshold()
319 thresh.SetInputData(selectedSegmentLabelmap)
320 thresh.ThresholdByLower(0)
321 thresh.SetInValue(backgroundValue)
322 thresh.SetOutValue(labelValue)
323 thresh.SetOutputScalarType(selectedSegmentLabelmap.GetScalarType())
326 import vtkSegmentationCorePython
as vtkSegmentationCore
328 inputLabelImage = slicer.vtkOrientedImageData()
329 inputLabelImage.ShallowCopy(thresh.GetOutput())
330 selectedSegmentLabelmapImageToWorldMatrix = vtk.vtkMatrix4x4()
331 selectedSegmentLabelmap.GetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
332 inputLabelImage.SetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
334 xy = callerInteractor.GetEventPosition()
335 ijk = self.
xyToIjk(xy, viewWidget, inputLabelImage, segmentationNode.GetParentTransformNode())
336 pixelValue = inputLabelImage.GetScalarComponentAsFloat(ijk[0], ijk[1], ijk[2], 0)
339 floodFillingFilter = vtk.vtkImageThresholdConnectivity()
340 floodFillingFilter.SetInputData(inputLabelImage)
341 seedPoints = vtk.vtkPoints()
342 origin = inputLabelImage.GetOrigin()
343 spacing = inputLabelImage.GetSpacing()
344 seedPoints.InsertNextPoint(origin[0] + ijk[0] * spacing[0], origin[1] + ijk[1] * spacing[1], origin[2] + ijk[2] * spacing[2])
345 floodFillingFilter.SetSeedPoints(seedPoints)
346 floodFillingFilter.ThresholdBetween(pixelValue, pixelValue)
348 if operationName == ADD_SELECTED_ISLAND:
349 floodFillingFilter.SetInValue(1)
350 floodFillingFilter.SetOutValue(0)
351 floodFillingFilter.Update()
353 modifierLabelmap.DeepCopy(floodFillingFilter.GetOutput())
354 self.
scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeAdd)
356 elif pixelValue != 0:
357 if operationName == KEEP_SELECTED_ISLAND:
358 floodFillingFilter.SetInValue(1)
359 floodFillingFilter.SetOutValue(0)
361 floodFillingFilter.SetInValue(1)
362 floodFillingFilter.SetOutValue(0)
364 floodFillingFilter.Update()
366 modifierLabelmap.DeepCopy(floodFillingFilter.GetOutput())
368 if operationName == KEEP_SELECTED_ISLAND:
369 self.
scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
371 self.
scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeRemove)
374 logging.error(
"Island processing failed")
376 qt.QApplication.restoreOverrideCursor()