Volume Slice 1

Render slices through a volume, by uploading to a 2D texture. Simple and … slow.

volume slice1
Imageio: 'stent.npz' was not found on your computer; downloading it now.
Try 1. Download from https://github.com/imageio/imageio-binaries/raw/master/images/stent.npz (805 kB)
Downloading: 8192/824612 bytes (1.0%)824612/824612 bytes (100.0%)
  Done
File saved as /home/docs/.imageio/images/stent.npz.

import imageio.v3 as iio
from wgpu.gui.auto import WgpuCanvas, run
import pygfx as gfx


canvas = WgpuCanvas()
renderer = gfx.renderers.WgpuRenderer(canvas)
scene = gfx.Scene()

vol = iio.imread("imageio:stent.npz").astype("float32") / 2000
nslices = vol.shape[0]
index = nslices // 2
im = vol[index].copy()

tex = gfx.Texture(im, dim=2)

geometry = gfx.plane_geometry(200, 200, 12, 12)
material = gfx.MeshBasicMaterial(map=tex)
plane = gfx.Mesh(geometry, material)
scene.add(plane)

camera = gfx.OrthographicCamera(200, 200)


@renderer.add_event_handler("wheel")
def handle_event(event):
    global index
    index = index + int(event.dy / 90)
    index = max(0, min(nslices - 1, index))
    im = vol[index]
    tex.data[:] = im
    tex.update_range((0, 0, 0), tex.size)
    canvas.request_draw()


if __name__ == "__main__":
    canvas.request_draw(lambda: renderer.render(scene, camera))
    run()

Total running time of the script: (0 minutes 0.648 seconds)

Gallery generated by Sphinx-Gallery