Create sphere brush to edit voxels values in scanners image without fixed position

Good morning,

I would need some help to finish my project as an intern working on segmentation of an organ on 3D Slicer.
I have a segmented organ with white pixels over a black background (a vtkMRMLScalarVolumeNode).

I would like to add some missing white voxels to my segmented organ and erase others.
I have tried working on Segment editor but it doesn’t work as I need.

I am looking for another way to do this but I don’t know how to proceed :

  • how can I create a sphere brush (in 3D) in python which would be able to browse / scan my image (as a numpy array if necessary) thanks to mouse clicks’ of the user on the image
  • then, if the user clicks, tell to the sphere to turn the pixels of my image from their value to another one that I choose
    Does someone have an idea ?

I have read lots and lots of comments / topics / codes but I can’t find anything that I understand and able to add to my code easily (I am new on 3D Slicer)
Thanks a lot in advance for your help

What type of data are you working with? MRI/CT/Ultrasound?

Also you write

I have a segmented organ with white pixels over a black background

but what Segment Editor effect did you use to do this? It sounds like you have instead just manipulated your 3D volume with image processing.

The effects in Segment Editor are used to create a 3D segment object. This isn’t used to modify the pixel values of your original image data.

First, thanks a lot for your answer !

At the beginning I have a scanner volume that I upload in dicom
Then, I do some personalized image processing on my own in the python script and so at the end I have my segmented organ. In this segmented organ volume, my organ is white and all the other pixels that are not considered as my organ are black.

However, I would like to correct this segmentation by adding some pixels that have been forgotten to my segmented organ so put them at white.

On segment editor, I have tried “paint” but I don’t succeed in transferring the “painting area” into my organ segmented just to create only one volume image …

I have to admit that I am totally lost
Thanks in advance if you could help

Hi @Laura - maybe a few screen captures would help us understand your scenario better so we can help out. I suspect what you want to do is use the results of your image processing as a labelmap based segmentation in Slicer that you can then operate on using the other effects in the Segment Editor. Then you can access the results of editing via python to use for whatever other purposes you have in mind.

Thanks a lot !
I try to be clear but it is difficult when you work for 6 months on a python script to explain what you imagine easily, sorry !

This is the initial volume scanner that I have when I upload :

Then, I made some treatments to get that (with transparency first to see the result) :

The same image without the background original image is that : (where the organ has pixel 255 and the others pixels of the image are 0)

So, this is on this image that I would like to add white pixels (255) in some different voxels positions that I can’t defined…I would like that the user is able to click on voxel that he wants to convert as “voxels that belong to the organ segmented”

I hope that this is a little bit more clear with pictures !

Do you know a way to do that ?

Thanks a lot !

1 Like

Yes, that’s a huge help! I have a better idea now. What you can do is change your VolumeFinal into a segmentation first by making it a labelmap in the Volumes->Volume Information panel and then importing it into a Segmentation using the Segmentations module. Once you have that, you can use the Segment Editor to add and remove parts of the label. Then you can export it back to a labelmap or other things as needed. Hope that helps.

Here’s the step to convert scalar volume to labelmap:

Here’s the place to import a labelmap to a segmentation:


I think that it is what I want to do but I look for doing it through my python script so I wil tell you if I have problems to do it pedagogically for a surgeon user !
Thanks a lot, I would never think about doing these different steps in this order !

Glad to know this is progress for you :+1:

Once you know how you want things to work via the GUI, you can make them work via python scripting too. If you search this forum for keywords segmentation and python and labelmap you’ll find lots of snippets.

This might be what you need:

1 Like

Thank you so so much for your help !!!

Good afternoon,

I have almost finished to do this thanks to your explanation but I have a problem at the last step : to convert my labelmapvolume into a scalarvolumenode…
I have done this :

def CorrectionFinale(self, masterVolumeNode, inputVolCor):
# Etape 1 : creer un labelmap de mon volume final

volumesLogic = slicer.modules.volumes.logic()
vollabel= volumesLogic.CreateAndAddLabelVolume(slicer.mrmlScene, inputVolCor, "labelmapfoie" )
labelmaptocorrect = volumesLogic.CreateLabelVolumeFromVolume(slicer.mrmlScene, vollabel, inputVolCor)    

# Etape 2 : Importation du labelmap dans le module segmentation pour creer une segmentation
segmentationNode = slicer.vtkMRMLSegmentationNode()
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationLogic = slicer.modules.segmentations.logic()
importlabelmaptocorrect = segmentationLogic.ImportLabelmapToSegmentationNode (labelmaptocorrect, segmentationNode)

# Etape 3 : Correction de la segmentation par les effets paint / erase 
editorWidget = slicer.modules.segmenteditor.createNewWidgetRepresentation()


def ExportFinal(self, masterVolumeNode):
# Exporter le labelmap node de la segmentation
seg = slicer.util.getNode(‘CorrectionSegmentationFoie’)
labelmapVolumeNode = slicer.mrmlScene.AddNewNodeByClass(‘vtkMRMLLabelMapVolumeNode’,‘LabelFoiefinal’)

foiesegfin = slicer.modules.segmentations.logic().ExportAllSegmentsToLabelmapNode(seg, labelmapVolumeNode)
a = slicer.util.array('LabelFoiefinal')
volumeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLScalarVolumeNode',"VolumeFoieSegmente")
foieseg = slicer.util.updateVolumeFromArray(volumeNode, a)

To explain, my first function creates my labelmap from my segmented organ volume, then import this labelmap into the segmentation module and finally do the different corrections thanks to “Segment editor”.

My second function is here to convert my labelmap corrected by Segment Editor into volume : to do that, I use the “export” to labelmap to update this labelmap. Then, I convert into array (a) my last labelmap.
However, to convert from numpy array to vtkMRMLScalarVolumeNode, I don’t succeed…
Maybe I have mixed up the features ?

I have also a little additional question : how can I check the ‘sphere brush’ in my ‘Segment editor’ module and force my “MasterVolume” to be my “labelmapfoie” ?

Thanks a lot for your help !