Edit Segmentation masks

Hello everyone! hope you are doing well.

Well. I’m trying to assign my own segmentation algorithm to the input images and put the outoput back in segment editor to edit it. It would help me to improve my model.
I already installed jupyter extension, and followed the scripts commands Script repository — 3D Slicer documentation , but when I tried to edit the imported segmentation, it doesn’t works.
The code I used:

volume = slicer.util.getNode('patient_1') # imported manually
input_images = slicer.util.arrayFromVolume(volume) # (n, 512,512)
new_masks = np.array(mask_list) # generated from region growing # (n, 512,512)
'''   Create Segmentation Node in scene   '''
segmentationNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentationNode")
'''   create a display   '''
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(volume)
lung_segment = segmentationNode.GetSegmentation().AddEmptySegment("lung_segment")
segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(volume)
segmentId = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName('lung_segment')
slicer.util.updateSegmentBinaryLabelmapFromArray(new_masks, segmentationNode, segmentId, volume)

It’s working, but I don’t know how to set the segmentation mask editable. Any suggestion?

Thanks in advance

Imported segmentation

Trying to edit with Paint:

Ps.: The Volume is a collection of images that I imported with the option single file unchecked.

Slicer Version: 4.13

When you scroll up a little bit, do you see the “Select master volume to enable editing” message at the master volume selector? If yes, then you need to select a volume there that will be used as master volume.

1 Like

Thanks for replying

Yes, I selected, look:

Some masks don’t look good enough, I’d like to edit them using the paint tool.

Maybe the color has something to do with it? I must put at the same level? but how can I choose the same color for both?

Since the lung segment is green, it should appear in green in the slice views. So, it seems that the imported segmentation is invalid. Most likely something is wrong with mask_list. You can try to set the array like this (using simple thresholding at 50) and see if everything works well in that case:

import numpy as np
new_masks = np.zeros(input_images.shape)
new_masks[input_images>50] = 1

If everything is good then you can fix the issue by making your array more similar to the correct new_masks array content (shape, data type, …).

1 Like

It worked, but something is still missing.
Look how curious. I can edit just where I painted.
For example, in this image, when I try to use the eraser to remove the white area, it doesn’t works

But When I paint it, then I can erase.

But the eraser just works on the area I painted :thinking:

Thank you very much

Don’t worry, your solution worked.

Thanks again

The issue was that updateSegmentBinaryLabelmapFromArray expected the label value to be 1. I"ll make the function more robust so that it works well with any nonzero label value.

1 Like