Volume rendering


In [1]:
import k3d
import numpy as np
import SimpleITK as sitk

im_sitk = sitk.ReadImage('./CT/heart.mhd')
img  = sitk.GetArrayFromImage(im_sitk)
size = np.array(im_sitk.GetSize()) * np.array(im_sitk.GetSpacing())
im_sitk.GetSize()


Out[1]:
(512, 512, 317)

In [2]:
# to reduce example size in widget state
img[img<150] = 0
img[img>750] = 1000

In [3]:
volume = k3d.volume(
    img.astype(np.float32), 
    alpha_coef=1000,
    shadow='dynamic',
    samples=600,
    shadow_res=128,
    shadow_delay=50,
    color_range=[150,750], 
    color_map=(np.array(k3d.colormaps.matplotlib_color_maps.Gist_heat).reshape(-1,4) 
               * np.array([1,1.75,1.75,1.75])).astype(np.float32),
    compression_level=9
)

volume.transform.bounds = [-size[0]/2,size[0]/2,
                           -size[1]/2,size[1]/2,
                           -size[2]/2,size[2]/2]

plot = k3d.plot(camera_auto_fit=False)
plot += volume
plot.lighting = 2
plot.display()



In [ ]: