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()
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.
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.
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:
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?
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
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.
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.
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