# how to 3d volume render numpy array

**URL:** https://discourse.vtk.org/t/how-to-3d-volume-render-numpy-array/4261
**Category:** Support
**Created:** [September 21, 2020, 8:04am UTC](https://discourse.vtk.org/t/how-to-3d-volume-render-numpy-array/4261 "2020-09-21T08:04:51Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![YolkandWhite](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yolkandwhite/32/2256_2.png) [@YolkandWhite](https://discourse.vtk.org/u/YolkandWhite)
#### Post date: [September 21, 2020, 8:04am UTC](https://discourse.vtk.org/t/how-to-3d-volume-render-numpy-array/4261/1 "2020-09-21T08:04:51Z")

</div>

I’m trying to volume render an 3d numpy array with vtk python.

I used to render it by getting the stack of images,  
but I’m trying to volume render with an 3d numpy array instead of stack of images.

this picture below is my first try and it doesn’t look good.

 ![스크린샷 2020-09-21 오후 4.59.48](https://discourse.vtk.org/uploads/default/original/2X/0/08675f2dcb910cc1539de6737bd86de6dee47999.jpeg)

```
data = box # box is 3d array

imdata = vtk.vtkImageData()

depthArray = numpy_support.numpy_to_vtk(data.ravel(), deep=True, array_type=vtk.VTK_DOUBLE)

imdata.GetPointData().SetScalars(depthArray)

volumeMapper.SetInputData(imdata)

```

I think these are something that needs to be fixed.

My input is not Image, It’s an 3d array.

What should I fix it?

---

<div class="post-metadata">

### Author: ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)
#### Post date: [September 21, 2020, 10:02am UTC](https://discourse.vtk.org/t/how-to-3d-volume-render-numpy-array/4261/2 "2020-09-21T10:02:02Z")

</div>

I am not famiiar with `numpy_support` so someone else may help you better to fix the issue on your code.  
However, to pass a numpy array as point data (in 0-copy), I use the following snippet in VTK python:

```auto
floatData = np.array([0, 1, 0.5], 'f') # my numpy array

floatArray = vtk.vtkFloatArray() # the VTK array
floatArray.SetName("My array")
floatArray.SetNumberOfComponents(1)
floatArray.SetArray(floatData, 3, False) # False because we do not take the ownership

mydata.GetPointData().SetScalars(floatArray); # give the array to the dataset

```

Hope that helps

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [September 21, 2020, 7:07pm UTC](https://discourse.vtk.org/t/how-to-3d-volume-render-numpy-array/4261/3 "2020-09-21T19:07:57Z")

</div>

You have forgot to set up appropriate transfer functions. Specifying scalar opacity transfer function is a minimum, for nicer rendering you probably also want color transfer function. Check out VTK examples (like [this](https://lorensen.github.io/VTKExamples/site/Python/VolumeRendering/SimpleRayCast/)) and documentation for details. Determining transfer functions using Python scripting is tedious, so I would recommend Paraview’s transfer function editor for general engineering visualization and 3D Slicer’s volume rendering module for medical image visualization.

---

<div class="post-metadata">

### Author: ![YolkandWhite](https://discourse.vtk.org/user_avatar/discourse.vtk.org/yolkandwhite/32/2256_2.png) [@YolkandWhite](https://discourse.vtk.org/u/YolkandWhite)
#### Post date: [September 22, 2020, 1:08am UTC](https://discourse.vtk.org/t/how-to-3d-volume-render-numpy-array/4261/4 "2020-09-22T01:08:58Z")

</div>

Yes I did set the color transfer function and opacity.

I haven’t show the whole code yet.

below is my whole code.

```
from vtk.util import numpy_support
import vtk
import numpy as np

box = []
for i in range(1536):
    loader = np.loadtxt("/Users/nyh/Documents/RP_lab/XRF/nparray/"+str(i+1)+".npy")
    box.append(loader)
box = np.stack(box)    

data = box

imdata = vtk.vtkImageData()
depthArray = numpy_support.numpy_to_vtk(data.ravel(), deep=True, array_type=vtk.VTK_DOUBLE)

imdata.SetDimensions(data.shape)
imdata.SetSpacing([1,1,1])
imdata.SetOrigin([0,0,0])
imdata.GetPointData().SetScalars(depthArray)

colorFunc = vtk.vtkColorTransferFunction()
# colorFunc.AddRGBPoint(1, 1, 0.0, 0.0) # Red
colorFunc.AddRGBPoint(2, 0.0, 1, 0.0) # Green

opacity = vtk.vtkPiecewiseFunction()

volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorFunc)
volumeProperty.SetScalarOpacity(opacity)
volumeProperty.SetInterpolationTypeToLinear()
volumeProperty.SetIndependentComponents(2)

volumeMapper = vtk.vtkOpenGLGPUVolumeRayCastMapper()
volumeMapper.SetInputData(imdata)
volumeMapper.SetBlendModeToMaximumIntensity()

volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)

ren = vtk.vtkRenderer()
ren.AddVolume(volume)
ren.SetBackground(0,0,0)

renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(900, 900)

interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renWin)

interactor.Initialize()
renWin.Render()
interactor.Start()

```

When I visualize the array data with pyvista, it looks like this.

 ![py](https://discourse.vtk.org/uploads/default/original/2X/8/82b5ae9829d51f469a325270685ea28404fbe093.jpeg)

every slice of array has a circular data inside.  
but I don’t see any circles with VTK.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [September 22, 2020, 5:16am UTC](https://discourse.vtk.org/t/how-to-3d-volume-render-numpy-array/4261/5 "2020-09-22T05:16:18Z")

</div>

It looks like your scalar opacity transfer function is incorrect. It is easier to learn how to create good transfer functions by using a GUI (Paraview, 3D Slicer, etc).

---

<div class="post-metadata">

### Author: ![xavArtley](https://discourse.vtk.org/user_avatar/discourse.vtk.org/xavartley/32/2381_2.png) [@xavArtley](https://discourse.vtk.org/u/xavArtley)
#### Post date: [September 25, 2020, 5:21am UTC](https://discourse.vtk.org/t/how-to-3d-volume-render-numpy-array/4261/6 "2020-09-25T05:21:09Z")

</div>

Did you take care about order c or fortran?  
Try data.ravel(order=‘F’)
