Overlay color onto vtkMRMLScalarVolumeNode image

For a Slicer module I am developing, the user selects which image they want to use as an input by selecting its vtkMRMLScalarVolumeNode from a dropdown menu.

Then, the user presses a button to colorize the tumor on that image. Pressing this button calls a function that takes the 3D numpy array from the vtkMRMLScalarVolumeNode, creates a 4D numpy array where the 4th dimension contains RGB color values, converts the 4D numpy array to: np.dtype([(‘R’, ‘u1’), (‘G’, ‘u1’), (‘B’, ‘u1’)]), and saves the final 3D RGB image as a NIFTI file.

Then the user can drag the NIFTI file into Slicer and see something like the image below.

sercolor

Instead of creating a .nii file that contains both the background image from the input vtkMRMLScalarVolumeNode and a blue/green/purple region on top of the tumor, I would prefer to continue displaying the input image in its original vtkMRMLScalarVolumeNode and display the blue/green/purple region as a separate object that is overlayed on top of the input image. In addition, I would like this blue/green/purple region to remain visible even after the user selects a different vtkMRMLScalarVolumeNode from the dropdown menu.

Is it possible to make this change that I want, and if so, could you please include some example lines of code that would help me figure out how to incorporate this change into my Python scripted module?

Thanks,
Rohan Nadkarni

You can display the grayscale image as background volume and the color image as foreground volume using slicer.util.setSliceViewerLayers().

Andras,

Thanks for the suggestion. However, I’m having trouble adding the color image to a vtkMRMLScalarVolumeNode.

By default, this numpy image (img_np_rgb) has dimensions of 512x152x168x3. I think the vtkMRMLScalarVolumeNode expects a 3D input, so trying to add this 4D image to the node results in an error.

In order to make it possible to save the 3D color image as a NIFTI, I had to do this:

rgb_dtype = np.dtype([(‘R’,‘u1’),(‘G’,‘u1’),(‘B’,‘u1’)])
rgb_typed = img_np_rgb.view(rgb_dtype).reshape(shape_3d)

where shape_3d is the shape of the original image without color added.

When I try to add rgb_typed to the vtkMRMLScalarVolumeNode, I also get an error.

What is the correct way to reformat img_np_rgb so that it can be added to a vtkMRMLScalarVolumeNode?

Thanks,
Rohan

You can create a “vtkMRMLVectorVolumeNode” and set its voxels from a numpy array using slicer.util.updateVolumeFromArray() convenience function.

After incorporating your suggestions from both comments, I’ve resolved this issue.
Thanks for your help Andras!