# How to render a volume by vector?

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! 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. You can set the 2D transfer function by typing this into the Python console:

``````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).

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:

``````    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)
``````

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!

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.