How to clear segmentation?

Hi, is there an easy way to clear a segmentation from Python? (Or from the widget, by the way.) I’m now doing this in a module that automatically clears a segmentation after saving it. But this looks like a hack rather than the proper way to clear the segmentation contents.

num_segments = selectedSegmentation.GetSegmentation().GetNumberOfSegments()
for i in range(num_segments):
  segmentId = selectedSegmentation.GetSegmentation().GetNthSegmentID(i)
  labelMapRep = selectedSegmentation.GetBinaryLabelmapRepresentation(segmentId)
  labelMapRep.Initialize()
  labelMapRep.Modified()
  selectedSegmentation.Modified()
1 Like

I don’t think we ever had a use case where you have to actually clear a segmentation. The python code is fine I think - hack or not - in the absence of a way doing this in the UI.

What you could do is have a segmentation with the empty segments pre-populated (I think this is why you want to clear them so that you have the segments with the same name, color, etc.), then clone the segmentation for each segmenation run. You can clone in the Data module from the right-click menu.

You can clear the segmentation in many different ways (remove segment, remove representation, make all voxels 0 value, make the extent empty, …). It would be interesting to know what is the use case so that we can find the most suitable solution.

I have a sequence of changing image, and I would like to create a matching sequence of segmentation. I go to the first item in the image sequence, paint a segmentation, then record it in another sequence browser. Then, I would like to iterate this process through items of the image sequence. But I have hundreds of items in a sequence, so I would like to automate the process. I cannot delete the segments or segmentation because I need to keep it in the segmentation sequence browser as a proxy node.
Unfortunately I’m having issues with the code above. So I was hoping there is a more appropriate way to clear the segments.

We ended up using code in “Logical operators” effect:

Just for completeness of information, I started this thread because I has problems with the cleared segments. I couldn’t repaint on them. But I figured out later that the issue was not in the code that cleared the segments. I’m now use the code Andras quoted, but just FYI the first code also works.

In the latest nightly, there is now an option to clear selected segments from the context menu in the segments table:
image

Great, thanks! Can I be lazy and ask you to please paste here a link to the code that other modules and scripts can call to use this function?

I used the same approach as you to reset the segment from the GUI:

masterRepresentation.Initialized();
masterRepresentation.Modified();

I could add a helper method to the module logic that would expose the same function to python if you think it would be useful.

That’s OK. I was just wondering if there is another function that would be preferred. This works fine.

Hi Lassoan. I am an absolute beginner with 3DSlicer. Is there a way to clear ONLY the content of a segment node without removing the node itself, using Python? All of the solutions I have found so far, remove the node and the content altogether, as a result, the user will have to keep creating the segmentation nodes in the GUI over and over! Many thanks for your help. Here is how I get access to the segmentation node, the content of which I would like to clear:

segmentationNode = slicer.util.getNode(‘Segmentation’)

I tried bing chat with the question how can I clear the contents of a segmentation node in 3D Slicer via python without removing the node and suggested answer seems to work?

segmentationNode = getNode('Segmentation')
segmentationNode.GetSegmentation().RemoveAllSegments()

Is this what you want to do?

This solution removes the entire segmentation node! I just need to clear the content of it and keep the node itself in the GUI of 3DSlicer. Does this make sense? Thanks

No, this only removes all the segments within the segmentation node. Does not remove the segmentation node. Give it a try.

I did give it a try but it removed the node completely. I tried it in the Python console of 3DSlicer. The segmentation node disappeared altogether.

Regards
Mehran

Thats not what I see on my end. Here is what I did:

I loaded MRHead and created a segmentation with 4 segments as shown.

then when I executed the code,

segmentationNode = getNode('MR-head_Seg*')
segmentationNode.GetSegmentation().RemoveAllSegments()

it simply removed the four segments, but the segmentation node called (MR-head-segmentation) remained (see the output of print(segmentatioNode)). Is this not what you want? If not, I am not sure if I am following what you are trying to do.

Oh my apologies! Apparently,

My previous in-complete message was sent by accident!

Many thanks for all the visuals and details. Please let me correct my question. What I am trying to do is to clear the content of the segments, without removing the segments themselves! Sorry for the incorrect question at the beginning, as I am new with 3DSlicer.

The reason I would like to erase the content, is that the user will push a button to save the segmentations, and then another set of DICOM files will be loaded automatically for the user’s annotation. If the segments from the previous round are fully removed, then the user is forced to re-create them from the GUI.

So, this is why I would like to keep the segments but ONLY clear their content.

It is interesting that from the GUI, you can easily do this by right-clicking on the segment and clear the content of the segment! But I cannot find, for the life of me, how I can do this using Python.

Your help will be much appreciated.

Regards
Mehran

to clear the contents of a segment within a segmentation node, this seems to work

segmentationNode = getNode('Segmentation')
segmentation = segmentationNode.GetSegmentation()
segmentId = segmentation.GetSegmentIdBySegmentName('Segment_1')
segment = segmentation.GetSegment(segmentId)
segment.RemoveAllRepresentations()

This works in so far as clearing the content. However, I cannot do any segmentation in the GUI after the content of the segmentation is cleared! The second I click on the brush to annotate the Dicom files I get the following error:

[VTK] vtkSegmentationModifier::SetBinaryLabelmapToSegment: Failed to get binary labelmap representation in segmentation

[Qt] void __cdecl qSlicerSegmentEditorAbstractEffect::modifySegmentByLabelmap(class vtkMRMLSegmentationNode *,const char *,class vtkOrientedImageData *,enum qSlicerSegmentEditorAbstractEffect::ModificationMode,const int ,bool) : Failed to add modifier labelmap to selected segment