Segmentation (Extract Skin Py)

Hi,

Thanks @lassoan for your previously answers

I’m always in troubles with ExtractSkin

I tryed to import my own CT scan but it doesn’t work as your example datas

Further I will need to load DICOM.

masterVolumeNode = slicer.util.loadVolume(‘/…/myCT.nrrd’, returnNode=True)
masterVolumeNode = slicer.mrmlScene.AddNewNodeByClass(“vtkmasterVolumeNode”)

#Create segmentation
segmentationNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentationNode”)
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment(“bone”)

#Create segment editor to get access to effects
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentEditorNode”)
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)

Occuring errors for all effects on apply line

#Thresholding
modifierLabelmap.GetImageToWorldMatrix(originalImageToWorldMatrix)
AttributeError: ‘NoneType’ object has no attribute ‘GetImageToWorldMatrix’

smoothing
modifierLabelmap.DeepCopy(smoothingFilter.GetOutput())
AttributeError: ‘NoneType’ object has no attribute ‘DeepCopy’

Here you load a volume into masterVolumeNode then immediately overwrite it with an empty volume, which breaks everything else.

1 Like

Ok thanks a lot !

Another bug, I tried to test différent parameters combinations like this :

THRESHOLD_LIST = [item,item]
SMOOTH_SIZE = [item,item]
MARGIN_SIZE = [item,item]

for t in THRESHOLD_LIST
…for m in MARGIN_SIZE:
…for s in SMOOTH_SIZE:
…# Thresholding effect
…# Margin effect effect
…# Smoothing effect
…# Test

To obtain results like this :
Threshold, Margin size, Smoothing, Result
t,m,s, resultat1
t,m,s, resultat2
t,m,s, resultat3 …

But computer is overheating !!! it’s very very very long, any idea ?

Probably you try to store results of tens or hundreds of iterations in the scene. During this you run out of physical memory and everything slows down due to starting swapping data between RAM and physical disk. To avoid this, at the end of each iteration, write the results to file then delete the result nodes (or reuse them in the next iteration).

Ok not so much iterations in my case, I need your help.
I tryed it without saving any results, just for testing iteration process.
First iteration takes 18 seconds, the second 1min14 ! and after I stop it before overheating

def run(self, inputVolume):
"""

SEUIL = [0.97,0.90]
SMOO_SIZE = [3,7]
SMOO_METHOD = "MEDIAN"
MARGIN_SIZE = [3]
MARGIN_METHOD = "Shrink"
SEUIL_ISLAND_SIZE = 500
masterVolumeNode = inputVolume

for iSEUIL in SEUIL:
  for iMARGIN_SIZE in MARGIN_SIZE:
    for iSMOO_SIZE in SMOO_SIZE:

      # Create segmentation
      segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
      segmentationNode.CreateDefaultDisplayNodes() # only needed for display
      segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
      addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment("skin")
      # Create segment editor to get access to effects
      segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
      segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
      segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
      segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
      segmentEditorWidget.setSegmentationNode(segmentationNode)
      segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)
      
      # UH Range
      rangeUH = masterVolumeNode.GetImageData().GetScalarRange()

      # Thresholding
      segmentEditorWidget.setActiveEffectByName("Threshold")
      effect = segmentEditorWidget.activeEffect()
      effect.setParameter("MinimumThreshold",rangeUH[1]*iSEUIL)
      effect.setParameter("MaximumThreshold",rangeUH[1])
      effect.self().onApply()

      # Margin
      segmentEditorWidget.setActiveEffectByName("Margin")
      effect = segmentEditorWidget.activeEffect()
      effect.setParameter("Operation", MARGIN_METHOD)
      effect.setParameter("MarginSize", iMARGIN_SIZE)
      effect.self().onApply()

      # Smoothing
      segmentEditorWidget.setActiveEffectByName("Smoothing")
      effect = segmentEditorWidget.activeEffect()
      effect.setParameter("SmoothingMethod", SMOO_METHOD)
      effect.setParameter("KernelSizeMm", iSMOO_SIZE)
      effect.self().onApply()

      # Islands
      segmentEditorWidget.setActiveEffectByName("Islands")
      effect = segmentEditorWidget.activeEffect()
      effect.setParameter("Minimumsize", SEUIL_ISLAND_SIZE)
      effect.setParameter("Operation", "SPLIT_ISLANDS_TO_SEGMENTS")
      effect.self().onApply()
     
      # Measures
      # [...]

      # Clean up
      segmentEditorWidget = None
      slicer.mrmlScene.RemoveNode(segmentEditorNode)
      slicer.mrmlScene.RemoveNode(segmentationNode)

      qt.QMessageBox.information(slicer.util.mainWindow(), 'Slicer Python - Result', str)

This confirms that the problem is that you run out of RAM.

Your script still does not free up memory because you need to delete display nodes and all filters. It could be simpler to get it right, if you create nodes and filters once and then update them in each iteration.

1 Like