Trying to update a slicer volume node from SimpleITKImage

Operating system: Window 7
Slicer version: 4.8.1

Hi All,

I am a newbie. I am trying to develop a segmentation module using python. The main function takes as input a vtkMRMLLabelMapVolumeNode and a vtkMRMLScalarVolumeNode as well as an ‘outputVolumeNode’ which is a newly created (in slicer gui) vtkMRMLLabelMapVolumeNode. The algorithm produces a new label map volume (a numpy array) based on the input volume and labelmap and is trying to push the new label map volume into the output node as so:

sitkImageOutput = sitk.GetImageFromArray(numpyArray)
sitkUtils.PushVolumeToSlicer(sitkImageOutput,outputVolumeNode)

But nothing happens - the outputVolumeNode inside slicer remains empty. Is there an update function or something i am missing?

Thanks

You could play with the SimpleFilters module and try using the same approach as is used there:

numpyArray does not specify image geometry (origin, spacing, axis directions). You have to call outim.CopyInformation(some_reference_image) or set the image geometry some other way.

What is empty? Does not show up in slice viewer? If you go to Volumes module, what do you see in Volume Information section? If you open the Histogram at the bottom of the module GUI, do you see a histogram there?

Maybe you have just forgot to show the created volume in the slice viewers. This code works for me:

import numpy as np
import sitkUtils
numpyArray = np.random.randint(0,10,[30,40,50])

sitkImageOutput = sitk.GetImageFromArray(numpyArray)
volumeNode = sitkUtils.PushVolumeToSlicer(sitkImageOutput,None)
slicer.util.setSliceViewerLayers(background=volumeNode)

Eventually setting origin and spacing got the code working like so:

  sitkUtils.PushVolumeToSlicer(sitkImageOutput,outputVolumeNode)
  outputVolumeNode.SetOrigin(inputVolumeNode.GetOrigin())
  outputVolumeNode.SetSpacing(inputVolumeNode.GetSpacing())
1 Like

Thanks for the update. Yes, numpy arrays cannot store image geometry, so you have to define the image geometry by setting origin, spacing, and axis directions in your SimpleITK image or Slicer volume node.

Thank you, I was able to get it working eventually with

slicernb.MatplotlibDisplay(matplotlib.pyplot)

1 Like