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

**URL:** <https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [July 19, 2018, 8:41am UTC](https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521 "2018-07-19T08:41:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laura](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@Laura](https://discourse.slicer.org/u/Laura)\
**Post date:** [July 19, 2018, 8:41am UTC](https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521/1 "2018-07-19T08:41:05Z")

</div>

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  
Laura

---

<div class="post-metadata">

**Author:** ![jamesobutler](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jamesobutler/32/7511_2.png) [@jamesobutler](https://discourse.slicer.org/u/jamesobutler)\
**Post date:** [July 19, 2018, 12:05pm UTC](https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521/2 "2018-07-19T12:05:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Laura](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@Laura](https://discourse.slicer.org/u/Laura)\
**Post date:** [July 19, 2018, 12:13pm UTC](https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521/3 "2018-07-19T12:13:16Z")

</div>

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  
Laura

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 19, 2018, 12:32pm UTC](https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521/4 "2018-07-19T12:32:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Laura](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@Laura](https://discourse.slicer.org/u/Laura)\
**Post date:** [July 19, 2018, 12:43pm UTC](https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521/5 "2018-07-19T12:43:01Z")

</div>

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 :

 ![ircadvolinitial](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/7/d/7d28fb959fc95bf0dac2161e55c56e1dfdef29a1.jpeg)

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

 ![finalvol](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/5/e/5e6213f0a04e1bcc36f273f2dac3ea0af7e24de9.jpeg)

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)

 ![volfinalcolors0255](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/6/c62d4f49e13de83ce9174e05ff3bb19db4ca9e62.png)

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 !  
Laura

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 19, 2018, 1:21pm UTC](https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521/6 "2018-07-19T13:21:50Z")

</div>

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:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/9/a932e91c9f8b7738fd7f0db73c71e8970c20fb2f.jpeg)

Here’s the place to import a labelmap to a segmentation:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/c/ecc69dfb65ccf87a212891a0eb2696fc4cb5c87f.jpeg)

---

<div class="post-metadata">

**Author:** ![Laura](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@Laura](https://discourse.slicer.org/u/Laura)\
**Post date:** [July 19, 2018, 1:31pm UTC](https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521/7 "2018-07-19T13:31:38Z")

</div>

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 !  
Laura

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [July 19, 2018, 2:00pm UTC](https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521/8 "2018-07-19T14:00:07Z")

</div>

Glad to know this is progress for you 👍

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:

> [@Import labelmap to segmentation node - in batch processing](https://discourse.slicer.org/t/import-labelmap-to-segmentation-node-in-batch-processing/2360/10):
>
> There were a couple of problems: It is not enough to create the segmentation node but you need to also add to the scene. You can use slicer.mrmlScene.AddNewNodeByClass to create a node and add it to the scene in one step. slicer.util.loadVolume loads file as scalar volume, while ImportLabelmapToSegmentationNode expects a labelmap volume; you need to use slicer.util.loadLabelVolume instead Relative path was used for the input image. I’m not sure what is the working directory of Slicer. It is s…

---

<div class="post-metadata">

**Author:** ![Laura](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@Laura](https://discourse.slicer.org/u/Laura)\
**Post date:** [July 19, 2018, 2:50pm UTC](https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521/9 "2018-07-19T14:50:35Z")

</div>

Thank you so so much for your help !!!  
Laura

---

<div class="post-metadata">

**Author:** ![Laura](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@Laura](https://discourse.slicer.org/u/Laura)\
**Post date:** [July 20, 2018, 11:34am UTC](https://discourse.slicer.org/t/create-sphere-brush-to-edit-voxels-values-in-scanners-image-without-fixed-position/3521/10 "2018-07-20T11:34:48Z")

</div>

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()
> slicer.mrmlScene.AddNode(segmentationNode)
> segmentationNode.CreateDefaultDisplayNodes() # only needed for display
> segmentationNode.SetName("CorrectionSegmentationFoie")	
> segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(inputVolCor)
> segmentationLogic = slicer.modules.segmentations.logic()
> importlabelmaptocorrect = segmentationLogic.ImportLabelmapToSegmentationNode (labelmaptocorrect, segmentationNode)
> segmentationNode.CreateClosedSurfaceRepresentation()
> 
> # Etape 3 : Correction de la segmentation par les effets paint / erase 
> editorWidget = slicer.modules.segmenteditor.createNewWidgetRepresentation()
> editorWidget.setMRMLScene(slicer.app.mrmlScene())
> editorWidget.show()
> 
> labelmaptocorrect.GetImageData().Modified()
> labelmaptocorrect.Modified()
> 
> ```
> 
> 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 !  
Laura
