Visualizing the 3D geometry of the X-ray detector in DiffDRR can be a helpful sanity check and is useful for debugging. We enable visualization of the DiffDRR setup using PyVista. The dependencies are pyvista, trame, and vtk.
The 3D visualization functions in DiffDRR perform the following steps:
Extract a mesh from your CT volume
Plot a pyramid frustum to visualize the camera pose
Plot the detector plane with the DRR embedded as a texture
Draw the principal ray from the X-ray source to the detector plane
We currently support the following backends for extracting meshes from CT scans:
As of DiffDRR v0.4.0, we also support the rendering of 3D labelmaps (e.g., segmentations of CT scans with TotalSegmentator).
Tip
To use surface_nets to extract a mesh, ensure you have installed pyvista>=0.43 and vtk>=9.3. Otherwise, you can use marching_cubes, which is slower and produces meshes with holes.
# Read in the CT volumesubject = load_example_ct()# Make a mesh from the CT volumect = drr_to_mesh(subject, "surface_nets", threshold=225, verbose=True)
# Make a mesh from the camera and detector planecamera, detector, texture, principal_ray = img_to_mesh(drr, pose)# Make the plotplotter = pyvista.Plotter()plotter.add_mesh(ct)plotter.add_mesh(camera, show_edges=True, line_width=1.5)plotter.add_mesh(principal_ray, color="lime", line_width=3)plotter.add_mesh(detector, texture=texture)# Render the plotplotter.add_axes()plotter.add_bounding_box()# plotter.show() # If running Jupyter locally# plotter.show(jupyter_backend="server") # If running Jupyter remotelyplotter.export_html("render.html")
from IPython.display import IFrameIFrame("render.html", height=500, width=749)