Creating a sicer module: Overlap RGB arrays

Operating system:Windows
Slicer version: 4.8.1

Hi all,
I am creating a module using python.

First, I have an image as inputVolume, which I have converted into an array to know some information such as dimensions:

nodeInput = self.inputSelector.currentNode ()
volume = slicer.util.arrayFromVolume(nodeInput)
numGrids = len(volume)

Second, I created an RGB array, initialized as:

image = np.zeros ([256,256,4], dtype =np.uint8);
GridsImage = [image] * numGrids;

As a result, I have 3 numpy RGB arrays: GridImage [0], GridImage [1] and GridImage [2]. Each array is completed by reading from a .txt file.

Now I want to overlap, the RGB arrays in the input image as the output volume. Any help or sugggestion?

How can I overlap a numpy RGB array into a input volume?

Thanks you in advance

In slice views, you can overlay two scalar volumes by choosing one of them as foreground and the other as background volume. From Python, you can use slicer.util.setSliceViewerLayers() method to choose volumes and opacity of the overlaid volume.

To create a RGB color volume from a numpy array, use slicer.util.updateVolumeFromArray like this:

volumeNodeRGB = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLVectorVolumeNode")
import numpy as np
voxels = np.random.rand(20,30,40,3)
slicer.util.updateVolumeFromArray(volumeNodeRGB, voxels)
slicer.util.setSliceViewerLayers(background=volumeNodeRGB, fit=True)
1 Like

Thanks for answering.

I’m trying other alternatives, since I have problems overlaying both volumes in the correct position.

Also, fit = True argument is not defined in my program version.

I’m trying to create a color image of the arrayRGB and overlay it with the input volume.

I have tried with this:

voxelsRGB = np.zeros ([256, 256, 1, 4], dtype = np.uint8)
nodeOutput = self.outputSelector.currentNode ()
sitkImageOutput = sitk.GetImageFromArray (voxelsRGB)
volumeNode = sitkUtils.PushVolumeToSlicer (sitkImageOutput, None)
slicer.util.setSliceViewerLayers (background = volumeNode)
sitkUtils.PushVolumeToSlicer (sitkImageOutput, outputVolumeNode)
nodeOutput.SetOrigin (nodeInput.GetOrigin ())
nodeOutput.SetSpacing (nodeInput.GetSpacing ())
slicer.util.updateVolumeFromArray (nodeOutput, voxelsRGB)

However, I can’t import stikUtils library.

What I actually want to do is to read a set of coordinates from a text file and paint the corresponding pixel in red, blue or green. The color of the pixel is read from another text file.
Is there any other way to color a specific pixel?

Any help is appreciated

Please use latest stable version (4.10.1).

This is deprecated. Please use slicer.util helper functions instead.

This sounds terribly inefficient. How many points do you have? Do you really want to color single voxels like this or actually you would like to place a colored markers at each of the listed positions?