# Overlay color onto vtkMRMLScalarVolumeNode image

**URL:** https://discourse.slicer.org/t/overlay-color-onto-vtkmrmlscalarvolumenode-image/12481
**Category:** Development
**Created:** [July 10, 2020, 5:29pm UTC](https://discourse.slicer.org/t/overlay-color-onto-vtkmrmlscalarvolumenode-image/12481 "2020-07-10T17:29:03Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![rohan\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohan_n/32/78474_2.png) [@rohan\_n](https://discourse.slicer.org/u/rohan_n)
#### Post date: [July 10, 2020, 5:29pm UTC](https://discourse.slicer.org/t/overlay-color-onto-vtkmrmlscalarvolumenode-image/12481/1 "2020-07-10T17:29:04Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/6/86b095a584dccda1f28ab18699c731419e294c69.png)

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

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [July 10, 2020, 5:34pm UTC](https://discourse.slicer.org/t/overlay-color-onto-vtkmrmlscalarvolumenode-image/12481/2 "2020-07-10T17:34:24Z")

</div>

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

---

<div class="post-metadata">

### Author: ![rohan\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohan_n/32/78474_2.png) [@rohan\_n](https://discourse.slicer.org/u/rohan_n)
#### Post date: [July 10, 2020, 7:15pm UTC](https://discourse.slicer.org/t/overlay-color-onto-vtkmrmlscalarvolumenode-image/12481/3 "2020-07-10T19:15:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [July 10, 2020, 8:12pm UTC](https://discourse.slicer.org/t/overlay-color-onto-vtkmrmlscalarvolumenode-image/12481/4 "2020-07-10T20:12:42Z")

</div>

You can create a “vtkMRMLVectorVolumeNode” and set its voxels from a numpy array using [`slicer.util.updateVolumeFromArray()`](https://github.com/Slicer/Slicer/blob/b13a49465eec642a3bd9c2d5c50b2afb57088259/Base/Python/slicer/util.py#L1091-L1125) convenience function.

---

<div class="post-metadata">

### Author: ![rohan\_n](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rohan_n/32/78474_2.png) [@rohan\_n](https://discourse.slicer.org/u/rohan_n)
#### Post date: [July 10, 2020, 9:55pm UTC](https://discourse.slicer.org/t/overlay-color-onto-vtkmrmlscalarvolumenode-image/12481/5 "2020-07-10T21:55:33Z")

</div>

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