Plot
nanodrr.plot
¶
plot_drr
¶
plot_drr(
img: Float[Tensor, "B C H W"],
mask: Bool[Tensor, "B C H W"] | None = None,
title: list[str] | None = None,
ticks: bool = True,
axs: list[Axes] | None = None,
cmap: str = "gray",
mask_cmap: str | Colormap = "Set2",
mask_n_colors: int = 7,
interior_alpha: float = 0.3,
edge_alpha: float = 1.0,
edge_width: int = 1,
**imshow_kwargs
) -> list[Axes]
Plot a batch of DRR images, optionally with a segmentation mask overlay.
Renders each image by summing across channels, simulating X-ray intensity
accumulation along a ray. A segmentation mask can be overlaid in two ways:
passed explicitly via mask, or derived automatically when img has
more than one channel (where channel 0 is background and channels 1+ are
labeled structures). These two modes are mutually exclusive.
When a mask is rendered, channel 0 is always dropped. It is assumed to represent background. Each remaining channel is drawn with a distinct color, a translucent interior fill, and an opaque boundary edge detected via morphological erosion.
| PARAMETER | DESCRIPTION |
|---|---|
img
|
Batch of DRR images with shape
TYPE:
|
mask
|
Explicit segmentation mask with shape
TYPE:
|
title
|
Per-image labels of length |
ticks
|
Whether to display 1-indexed pixel coordinate ticks. If
TYPE:
|
axs
|
Pre-existing axes to plot into. Must have length
TYPE:
|
cmap
|
Colormap for the DRR image. Defaults to
TYPE:
|
mask_cmap
|
Colormap used to assign colors to segmentation channels.
Colors are sampled evenly and cycled if the number of channels
exceeds
TYPE:
|
mask_n_colors
|
Number of evenly spaced colors to sample from
TYPE:
|
interior_alpha
|
Opacity of the filled mask interior, in
TYPE:
|
edge_alpha
|
Opacity of the mask boundary, in
TYPE:
|
edge_width
|
Boundary thickness in pixels. Controls the erosion kernel
size as
TYPE:
|
**imshow_kwargs
|
Additional keyword arguments forwarded to
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Axes]
|
List of |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/nanodrr/plot/imshow.py
overlay
¶
overlay(
moving: Float[Tensor, "B C H W"],
fixed: Float[Tensor, "B C H W"],
title: list[str] | None = None,
ticks: bool = True,
axs: list[Axes] | None = None,
blur_kernel: int = 3,
canny_low: int = 0,
canny_high: int = 100,
edge_color: tuple[float, float, float] = (1.0, 0.0, 0.0),
edge_alpha: float = 1.0,
edge_detection_size: int = 200,
) -> list[Axes]
Overlay moving image edges on fixed images for registration assessment.
Edges are detected using Canny at a fixed resolution for threshold consistency, then upscaled with bilinear interpolation for anti-aliased rendering.
| PARAMETER | DESCRIPTION |
|---|---|
moving
|
Moving images, shape (B, C, H, W)
TYPE:
|
fixed
|
Fixed images, shape (B, C, H, W)
TYPE:
|
title
|
Optional titles for each image in batch |
ticks
|
Whether to show pixel coordinate ticks
TYPE:
|
axs
|
Optional pre-existing axes to plot on
TYPE:
|
blur_kernel
|
Gaussian blur kernel size (must be odd)
TYPE:
|
canny_low
|
Canny lower threshold
TYPE:
|
canny_high
|
Canny upper threshold
TYPE:
|
edge_color
|
RGB color tuple for edges, values in [0, 1] |
edge_alpha
|
Edge opacity in [0, 1]
TYPE:
|
edge_detection_size
|
Resolution for Canny detection
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Axes]
|
List of matplotlib Axes objects |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If input shapes don't match or parameters are invalid |
Source code in src/nanodrr/plot/imshow.py
animate
¶
animate(
moving_img: Float[Tensor, "B C H W"],
moving_mask: Bool[Tensor, "B C H W"] | None = None,
out: str | Path | None = None,
fixed_img: Float[Tensor, "1 C H W"] | None = None,
fixed_mask: Bool[Tensor, "1 C H W"] | None = None,
titles: list[str] | None = None,
ticks: bool = True,
fps: int = 20,
pause: float = 1.0,
blur_kernel: int = 3,
canny_low: int = 0,
canny_high: int = 100,
edge_color: tuple[float, float, float] = (1.0, 0.0, 0.0),
edge_alpha: float = 1.0,
edge_detection_size: int = 200,
verbose: bool = True,
**kwargs
) -> Path | None
Create an animated GIF from a batch of DRR images.
Renders a sequence of DRR images as an animated GIF, with optional
side-by-side comparison against a fixed reference image. When out is
None, displays the animation inline in Jupyter notebooks.
Multi-channel images are automatically converted to single-channel with segmentation masks extracted from channels 1+ (channel 0 is background).
When fixed_img is provided, a third column is rendered showing the
moving image edges overlaid on the fixed image via overlay.
| PARAMETER | DESCRIPTION |
|---|---|
moving_img
|
Batch of moving DRR images.
TYPE:
|
moving_mask
|
Optional segmentation mask for moving images.
TYPE:
|
out
|
Output file path, or |
fixed_img
|
Optional fixed reference image for comparison.
TYPE:
|
fixed_mask
|
Optional segmentation mask for fixed image.
TYPE:
|
titles
|
Optional per-frame titles of length |
ticks
|
Whether to show pixel coordinate ticks.
TYPE:
|
fps
|
Frames per second for playback.
TYPE:
|
pause
|
Pause duration in seconds at the end of the loop.
TYPE:
|
blur_kernel
|
Gaussian blur kernel size applied before Canny edge detection.
TYPE:
|
canny_low
|
Lower hysteresis threshold for Canny edge detection.
TYPE:
|
canny_high
|
Upper hysteresis threshold for Canny edge detection.
TYPE:
|
edge_color
|
RGB color of the overlaid edges. |
edge_alpha
|
Opacity of the overlaid edges, in
TYPE:
|
verbose
|
Whether to display rendering progress.
TYPE:
|
**kwargs
|
Additional arguments forwarded to
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
Path | None
|
Path to saved file if |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
Source code in src/nanodrr/plot/gif.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |