133 minimumSize: if 0 then it means that all islands are kept, regardless of size
134 maxNumberOfSegments: if 0 then it means that all islands are kept, regardless of how many
137 qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
139 self.scriptedEffect.saveStateForUndo()
142 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
144 castIn = vtk.vtkImageCast()
145 castIn.SetInputData(selectedSegmentLabelmap)
146 castIn.SetOutputScalarTypeToUnsignedInt()
150 islandMath = vtkITK.vtkITKIslandMath()
151 islandMath.SetInputConnection(castIn.GetOutputPort())
152 islandMath.SetFullyConnected(
False)
153 islandMath.SetMinimumSize(minimumSize)
156 islandImage = slicer.vtkOrientedImageData()
157 islandImage.ShallowCopy(islandMath.GetOutput())
158 selectedSegmentLabelmapImageToWorldMatrix = vtk.vtkMatrix4x4()
159 selectedSegmentLabelmap.GetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
160 islandImage.SetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
162 islandCount = islandMath.GetNumberOfIslands()
163 islandOrigCount = islandMath.GetOriginalNumberOfIslands()
164 ignoredIslands = islandOrigCount - islandCount
165 logging.info(
"%d islands created (%d ignored)" % (islandCount, ignoredIslands))
167 baseSegmentName =
"Label"
168 selectedSegmentID = self.scriptedEffect.parameterSetNode().GetSelectedSegmentID()
169 segmentationNode = self.scriptedEffect.parameterSetNode().GetSegmentationNode()
170 with slicer.util.NodeModify(segmentationNode):
171 segmentation = segmentationNode.GetSegmentation()
172 selectedSegment = segmentation.GetSegment(selectedSegmentID)
173 selectedSegmentName = selectedSegment.GetName()
174 if selectedSegmentName
is not None and selectedSegmentName !=
"":
175 baseSegmentName = selectedSegmentName
177 labelValues = vtk.vtkIntArray()
178 slicer.vtkSlicerSegmentationsModuleLogic.GetAllLabelValues(labelValues, islandImage)
182 threshold = vtk.vtkImageThreshold()
183 threshold.SetInputData(selectedSegmentLabelmap)
184 threshold.ThresholdBetween(0, 0)
185 threshold.SetInValue(0)
186 threshold.SetOutValue(0)
188 emptyLabelmap = slicer.vtkOrientedImageData()
189 emptyLabelmap.ShallowCopy(threshold.GetOutput())
190 emptyLabelmap.CopyDirections(selectedSegmentLabelmap)
191 self.scriptedEffect.modifySegmentByLabelmap(segmentationNode, selectedSegmentID, emptyLabelmap,
192 slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
194 for i
in range(labelValues.GetNumberOfTuples()):
195 if (maxNumberOfSegments > 0
and i >= maxNumberOfSegments):
200 labelValue = int(labelValues.GetTuple1(i))
201 segment = selectedSegment
202 segmentID = selectedSegmentID
204 segment = slicer.vtkSegment()
205 name = baseSegmentName +
"_" + str(i + 1)
206 segment.SetName(name)
207 segment.AddRepresentation(slicer.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName(),
208 selectedSegment.GetRepresentation(slicer.vtkSegmentationConverter.GetSegmentationBinaryLabelmapRepresentationName()))
209 segmentation.AddSegment(segment)
210 segmentID = segmentation.GetSegmentIdBySegment(segment)
211 segment.SetLabelValue(segmentation.GetUniqueLabelValueForSharedLabelmap(selectedSegmentID))
213 threshold = vtk.vtkImageThreshold()
214 threshold.SetInputData(islandMath.GetOutput())
215 if not split
and maxNumberOfSegments <= 0:
217 threshold.ThresholdByLower(0)
218 threshold.SetInValue(0)
219 threshold.SetOutValue(1)
222 threshold.ThresholdBetween(labelValue, labelValue)
223 threshold.SetInValue(1)
224 threshold.SetOutValue(0)
227 modificationMode = slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeAdd
229 modificationMode = slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet
232 modifierImage = slicer.vtkOrientedImageData()
233 modifierImage.DeepCopy(threshold.GetOutput())
234 selectedSegmentLabelmapImageToWorldMatrix = vtk.vtkMatrix4x4()
235 selectedSegmentLabelmap.GetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
236 modifierImage.SetGeometryFromImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
240 self.scriptedEffect.modifySegmentByLabelmap(segmentationNode, segmentID, modifierImage, modificationMode)
242 if not split
and maxNumberOfSegments <= 0:
246 qt.QApplication.restoreOverrideCursor()
249 import vtkSegmentationCorePython
as vtkSegmentationCore
258 if viewWidget.className() !=
"qMRMLSliceWidget":
261 if eventId != vtk.vtkCommand.LeftButtonPressEvent
or callerInteractor.GetShiftKey()
or callerInteractor.GetControlKey()
or callerInteractor.GetAltKey():
265 confirmedEditingAllowed = self.scriptedEffect.confirmCurrentSegmentVisible()
266 if confirmedEditingAllowed == self.scriptedEffect.NotConfirmed
or confirmedEditingAllowed == self.scriptedEffect.ConfirmedWithDialog:
279 segmentationNode = self.scriptedEffect.parameterSetNode().GetSegmentationNode()
280 visibleSegmentIds = vtk.vtkStringArray()
281 segmentationNode.GetDisplayNode().GetVisibleSegmentIDs(visibleSegmentIds)
282 if visibleSegmentIds.GetNumberOfValues() == 0:
283 logging.info(
"Island operation skipped: there are no visible segments")
286 self.scriptedEffect.saveStateForUndo()
289 qt.QApplication.setOverrideCursor(qt.Qt.WaitCursor)
291 operationName = self.scriptedEffect.parameter(
"Operation")
293 if operationName == ADD_SELECTED_ISLAND:
294 inputLabelImage = slicer.vtkOrientedImageData()
295 if not segmentationNode.GenerateMergedLabelmapForAllSegments(inputLabelImage,
296 vtkSegmentationCore.vtkSegmentation.EXTENT_UNION_OF_SEGMENTS_PADDED,
297 None, visibleSegmentIds):
298 logging.error(
'Failed to apply island operation: cannot get list of visible segments')
299 qt.QApplication.restoreOverrideCursor()
302 selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()
306 thresh = vtk.vtkImageThreshold()
307 thresh.SetInputData(selectedSegmentLabelmap)
308 thresh.ThresholdByLower(0)
309 thresh.SetInValue(backgroundValue)
310 thresh.SetOutValue(labelValue)
311 thresh.SetOutputScalarType(selectedSegmentLabelmap.GetScalarType())
314 import vtkSegmentationCorePython
as vtkSegmentationCore
315 inputLabelImage = slicer.vtkOrientedImageData()
316 inputLabelImage.ShallowCopy(thresh.GetOutput())
317 selectedSegmentLabelmapImageToWorldMatrix = vtk.vtkMatrix4x4()
318 selectedSegmentLabelmap.GetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
319 inputLabelImage.SetImageToWorldMatrix(selectedSegmentLabelmapImageToWorldMatrix)
321 xy = callerInteractor.GetEventPosition()
322 ijk = self.xyToIjk(xy, viewWidget, inputLabelImage, segmentationNode.GetParentTransformNode())
323 pixelValue = inputLabelImage.GetScalarComponentAsFloat(ijk[0], ijk[1], ijk[2], 0)
326 floodFillingFilter = vtk.vtkImageThresholdConnectivity()
327 floodFillingFilter.SetInputData(inputLabelImage)
328 seedPoints = vtk.vtkPoints()
329 origin = inputLabelImage.GetOrigin()
330 spacing = inputLabelImage.GetSpacing()
331 seedPoints.InsertNextPoint(origin[0] + ijk[0] * spacing[0], origin[1] + ijk[1] * spacing[1], origin[2] + ijk[2] * spacing[2])
332 floodFillingFilter.SetSeedPoints(seedPoints)
333 floodFillingFilter.ThresholdBetween(pixelValue, pixelValue)
335 if operationName == ADD_SELECTED_ISLAND:
336 floodFillingFilter.SetInValue(1)
337 floodFillingFilter.SetOutValue(0)
338 floodFillingFilter.Update()
339 modifierLabelmap = self.scriptedEffect.defaultModifierLabelmap()
340 modifierLabelmap.DeepCopy(floodFillingFilter.GetOutput())
341 self.scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeAdd)
343 elif pixelValue != 0:
345 if operationName == KEEP_SELECTED_ISLAND:
346 floodFillingFilter.SetInValue(1)
347 floodFillingFilter.SetOutValue(0)
349 floodFillingFilter.SetInValue(1)
350 floodFillingFilter.SetOutValue(0)
352 floodFillingFilter.Update()
353 modifierLabelmap = self.scriptedEffect.defaultModifierLabelmap()
354 modifierLabelmap.DeepCopy(floodFillingFilter.GetOutput())
356 if operationName == KEEP_SELECTED_ISLAND:
357 self.scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
359 self.scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeRemove)
362 logging.error(
'Island processing failed')
364 qt.QApplication.restoreOverrideCursor()