# How to render a volume by vector?

**URL:** <https://discourse.slicer.org/t/how-to-render-a-volume-by-vector/17526>\
**Category:** Support\
**Tags:** visualization\
**Created:** [May 8, 2021, 2:30pm UTC](https://discourse.slicer.org/t/how-to-render-a-volume-by-vector/17526 "2021-05-08T14:30:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raykie](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Raykie](https://discourse.slicer.org/u/Raykie)\
**Post date:** [May 8, 2021, 2:30pm UTC](https://discourse.slicer.org/t/how-to-render-a-volume-by-vector/17526/1 "2021-05-08T14:30:55Z")

</div>

Generally, the volume rendering works based on the intensity of a voxel. What if I need to do it based on a vector (k1, k2)? Thanks!  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/2/f225377ad14df4947b45c7851bfd6aab3e92fb58.jpeg)

---

<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:** [May 10, 2021, 4:44am UTC](https://discourse.slicer.org/t/how-to-render-a-volume-by-vector/17526/2 "2021-05-10T04:44:45Z")

</div>

If k1 and k2 are intensity and gradient then you can use VTK’s 2D transfer function (an image that assigns RGBA value for coordinate pairs). See description of the transfer function [here](https://blog.kitware.com/2d-transfer-function-support-in-gpuvolumemapper/#:~:text=A%202D%20transfer%20function%20is%20defined%20as%20a,backend%29.%20More%20information%20can%20be%20found%20in%20TestGPURayCastTransfer2D). You can set the 2D transfer function by typing this into the Python console:

```python
slicer.mrmlScene.GetFirstNodeByClass("vtkMRMLVolumePropertyNode").GetVolumeProperty().SetTransferFunction2D(image)

```

If k1 and k2 are arbitrary scalar components then you can apply your 2D transfer function to the volume to get an RGBA volume (e.g., using numpy) and use that as input for volume rendering.  
To render an RGBA volume, you need to [turn off independent component volume property option](https://discourse.slicer.org/t/merge-colored-images-and-show-them-as-1-volume/6472/6)).

---

<div class="post-metadata">

**Author:** ![Raykie](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Raykie](https://discourse.slicer.org/u/Raykie)\
**Post date:** [May 19, 2021, 10:00am UTC](https://discourse.slicer.org/t/how-to-render-a-volume-by-vector/17526/3 "2021-05-19T10:00:17Z")

</div>

Really thanks for your reply! I followed your suggestion but failed to render the RGBA volume.

The k1 k2 volume shape is (128, 128, 112), so I simply generate a new volume whose shape is (128, 128, 112, 4) by following codes, and saved it as a NIfTI file:

```auto
    arrRGBA = np.empty(arrk1.shape+(4,), dtype=np.float)
    arrRGBA[..., 0] = ((arrk1-k1min)/(k1max-k1min)) * 255
    arrRGBA[..., 1] = ((arrk2-k2min)/(k2max-k2min)) * 255
    arrRGBA[..., 2] = np.zeros(arrk1.shape, dtype=np.float)
    arrRGBA[..., 3] = ((arrGrad-gradMin)/(gradMax-gradMin)) * 1

```

Then I turn off the independent component volume property option by copy-pasting your code:  
`getNode('VolumeProperty').GetVolumeProperty().SetIndependentComponents(0)`

As a result, it still rendered it as scalar rather than a RGBA volume. I think I must be wrong with something but I am not sure.

Any way, thanks very much!

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/3/63f5a4674abd225ebe5c61f5f811b5ce4ad359a6.jpeg)

---

<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:** [May 19, 2021, 12:10pm UTC](https://discourse.slicer.org/t/how-to-render-a-volume-by-vector/17526/4 "2021-05-19T12:10:21Z")

</div>

I would recommend to first try the method that I linked above that worked for many people. Then, you can compare the volume that works with the one that you generate using numpy. If volume dimensions, scalar type, etc. are all the same then probabably the issue is how you configure volume rendering.

If you share an example nifti volume that you generate then I can have a quick look, too.

---

<div class="post-metadata">

**Author:** ![Raykie](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Raykie](https://discourse.slicer.org/u/Raykie)\
**Post date:** [May 19, 2021, 12:24pm UTC](https://discourse.slicer.org/t/how-to-render-a-volume-by-vector/17526/5 "2021-05-19T12:24:18Z")

</div>

Here is the link for downloading. Thanks!  
[Example nifti volume](https://drive.google.com/file/d/1J7MO-z5FYlnk12rCI-SXu7n5nSCg6-a9/view?usp=sharing)

---

<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:** [May 19, 2021, 3:53pm UTC](https://discourse.slicer.org/t/how-to-render-a-volume-by-vector/17526/6 "2021-05-19T15:53:35Z")

</div>

Slicer loads this volume as a single-component scalar volume.

It may be because it has wrong data type: it uses datatype=64 (double) instead of 2304 (RGBA). RGBA volumes must use 8 bits for each component. If you fix these issues and Slicer still loads the volume as a single-component scalar volume then you can save the volume in nrrd file instead, or don’t save it to file and load it update a volume with the new voxel array, or use VTK filters as in the example linked above.

---

<div class="post-metadata">

**Author:** ![Raykie](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Raykie](https://discourse.slicer.org/u/Raykie)\
**Post date:** [May 28, 2021, 8:33am UTC](https://discourse.slicer.org/t/how-to-render-a-volume-by-vector/17526/7 "2021-05-28T08:33:09Z")

</div>

I transfer the dtype to 8 bits unsigned integer and saved it as Nrrd file, but the result is not as expected. Slicer still renders it as single component scalar volume.

I put the Nrrd file [here](https://drive.google.com/file/d/1n9cIxJ76jxM9GAKQi1tiY7vLTL3v8Q6-/view?usp=sharing) so that you can check it if you are free.

Thanks for all the help!
