Segmentation using thresholding using Python

Hey there,
I’m back with more silly questions
using the examples of using segment editor have been really helpful, the issue I have been running into, however, is using an image that is not the sample image.

Just dragging in an image and avoiding anything that uses ‘masterVolumeNode’ was my original plan, but I am unable to apply the threshold on the image of interest if I do it that way.Same with pulling in the image and setting it as the master volume manually on segment editor. So I did some digging to try and find out how to load in my own image the correct way and found this:

masterVolumeNode = slicer.util.loadVolume('C:/Users/m222222/Dropbox/BRAIN_MASK.nii.gz’, returnNode=True)

(the path is similar to my actual path)
and it doesn’t complain at me, but it also doesn’t open the file, and when I tried to run things anyways, after
‘segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)’
I got an error that I needed a VTK object, and after
‘segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)’
it also said that I needed a VTK object

if I run it without those 2 lines, it complains at the end on ‘effect.self().onApply()’ and says…

Traceback (most recent call last):
File “”, line 1, in
File “/Applications/Slicer.app/Contents/lib/Slicer-4.10/qt-scripted-modules/SegmentEditorEffects/SegmentEditorEffects/SegmentEditorThresholdEffect.py”, line 351, in onApply
modifierLabelmap.GetImageToWorldMatrix(originalImageToWorldMatrix)
AttributeError: ‘NoneType’ object has no attribute ‘GetImageToWorldMatrix’

To be honest, I really don’t know what I’m doing on the coding front, so even simple things like loading images are rough, which is why I’m here.

I’m just confused and would like to know the correct way to load a master volume so the sample works with a novel image.

Thank you,
Jazlynn

This example should work as is, just replace this line:

masterVolumeNode = sampleDataLogic.downloadMRBrainTumor1()

by this, if you use recent 4.11 version of Slicer:

masterVolumeNode = slicer.util.loadVolume('C:/Users/m222222/Dropbox/BRAIN_MASK.nii.gz')

by this, if you use 4.10 version:

[success, masterVolumeNode] = slicer.util.loadVolume('C:/Users/m222222/Dropbox/BRAIN_MASK.nii.gz', returnNode=True)

that makes a lot of sense, thank you

I replaced the first line with

[success, masterVolumeNode] = slicer.util.loadVolume(“C:/Users/m214492/Dropbox/CNV Modeling/Brain for Gustavo/BRAIN_MASK.nii.gz”, returnNode=True)

because it gave a syntax error the ran with one quote, and everything seemed to work until I got to that apply line

effect.self().onApply()

Where the callback was:

Traceback (most recent call last):
File “”, line 1, in
File “/Applications/Slicer.app/Contents/lib/Slicer-4.10/qt-scripted-modules/SegmentEditorEffects/SegmentEditorEffects/SegmentEditorThresholdEffect.py”, line 351, in onApply
modifierLabelmap.GetImageToWorldMatrix(originalImageToWorldMatrix)
AttributeError: ‘NoneType’ object has no attribute ‘GetImageToWorldMatrix’

I’m really not sure why or what it going on internally in slicer when this happens, but it runs with the sample

sorry for all of the questions

best,
Jazlynn

I think it may have something to do with the fact that in the sample, slicer actually opens the file, and you can see it in your 3 colored windows, but when I set my master volume, the file doesn’t come up visibly in slicer. I’m not sure if that could be the issue or not.

Does the example that I linked above works? If yes then try to follow that example more closely, make modifications step-by-step, and see which change causes the problem.

The sample that is provided works without an issue, and slicer doesn’t throw up an error until the thresholding line (after going through the code step by step)

effect.self().onApply()

So there must be an issue earlier although there was no complaint from slicer. I believe that is has to do with loading the master image; when I set the master image to be my novel image, the image is not shown in the colored ‘slices’ windows. However, when the sample image is loaded, it appears in the colored ‘slices’ windows.

The exact code I have been running is:
The master volume is a mask, saved as a nifty file.

[success, masterVolumeNode] = slicer.util.loadVolume(“C:/Users/m22222/Dropbox/BRAIN_MASK.nii.gz”, returnNode=True)

segmentationNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentationNode”)
segmentationNode.CreateDefaultDisplayNodes() # only needed for display
segmentationNode.SetReferenceImageGeometryParameterFromVolumeNode(masterVolumeNode)
addedSegmentID = segmentationNode.GetSegmentation().AddEmptySegment(“skin”)

segmentEditorWidget = slicer.qMRMLSegmentEditorWidget()
segmentEditorWidget.setMRMLScene(slicer.mrmlScene)
segmentEditorNode = slicer.mrmlScene.AddNewNodeByClass(“vtkMRMLSegmentEditorNode”)
segmentEditorWidget.setMRMLSegmentEditorNode(segmentEditorNode)
segmentEditorWidget.setSegmentationNode(segmentationNode)
segmentEditorWidget.setMasterVolumeNode(masterVolumeNode)

segmentEditorWidget.setActiveEffectByName(“Threshold”)
effect = segmentEditorWidget.activeEffect()
effect.setParameter(“MinimumThreshold”,“1”)
effect.setParameter(“MaximumThreshold”,“1”)
effect.self().onApply()

segmentEditorWidget = None
slicer.mrmlScene.RemoveNode(segmentEditorNode)

segmentationNode.CreateClosedSurfaceRepresentation()

What is the output of print(master VolumeNode)?

The output is just ‘none’

It means the image loading has failed. The application log may contain more details (menu: Help / Report a bug). Is the file path correct? Can you load the image by using “Add data”?

When I use the ‘add data’ option I get

[INFO][VTK] 02.08.2019 10:18:59 [vtkMRMLVolumeArchetypeStorageNode (0x7f9436777de0)] (/Volumes/Dashboards/Stable/Slicer-4102/Libs/MRML/Core/vtkMRMLVolumeArchetypeStorageNode.cxx:465) - Loaded volume from file: /Users/m214492/Dropbox/CNV Modeling/Brain for Gustavo/BRAIN_MASK.nii.gz. Dimensions: 270x320x76. Number of components: 1. Pixel type: float.
[DEBUG][Qt] 02.08.2019 10:18:59 [] (unknown:0) - “Volume” Reader has successfully read the file “/Users/m214492/Dropbox/CNV Modeling/Brain for Gustavo/BRAIN_MASK.nii.gz” “[0.29s]”

in the application log

When I enter it using the CLI I get

[DEBUG][Qt] 02.08.2019 10:18:00 [] (unknown:0) - Python console user input: [success, masterVolumeNode] = slicer.util.loadVolume(“C:/Users/m214492/Dropbox/CNV Modeling/Brain for Gustavo/BRAIN_MASK.nii.gz”, returnNode=True)

Path looks different:
/Users/m214492/Dropbox/CNV Modeling/Brain for Gustavo/BRAIN_MASK.nii.gz
is not the same as
C:/Users/m214492/Dropbox/CNV Modeling/Brain for Gustavo/BRAIN_MASK.nii.gz

Drive name (such as C:) is only used on Windows machines.

Oh my goodness you have just saved my life, thank you so much

A post was split to a new topic: Change segment color and opacity