Image visualisation & resizing

Note

Some function of this module need Napari, it is not a Biom3d dependency and need to be installed manually.

Module to visualize and resize images or plot.

biom3d.utils.image.display_mesh(mesh: Poly3DCollection, xlim: tuple[int, int], ylim: tuple[int, int], zlim: tuple[int, int], save: bool = False) None[source]

Plot a 3D volume from a 3D mesh using matplotlib.

Parameters:
  • mesh (Poly3DCollection) – 3D numpy array representing the volume to display. Expected shape: (Z, Y, X).

  • xlim (tuple of int) – Limits for the x-axis (min, max).

  • ylim (tuple of int) – Limits for the y-axis (min, max).

  • zlim (tuple of int) – Limits for the z-axis (min, max).

  • save (bool, default=False) – If True, saves the plot as “voxel.png”. Otherwise, shows the plot.

Return type:

None

biom3d.utils.image.display_voxels(image: ndarray, xlim: tuple[int, int], ylim: tuple[int, int], zlim: tuple[int, int], save: bool = False) None[source]

Plot a 3D volume from a 3D image using matplotlib.

Parameters:
  • image (numpy.ndarray) – 3D numpy array representing the volume to display. Expected shape: (Z, Y, X).

  • xlim (tuple of int) – Limits for the x-axis (min, max).

  • ylim (tuple of int) – Limits for the y-axis (min, max).

  • zlim (tuple of int) – Limits for the z-axis (min, max).

  • save (bool, default=False) – If True, saves the plot as “voxel.png”. Otherwise, shows the plot.

Return type:

None

biom3d.utils.image.napari_viewer(img: ndarray, pred: ndarray) None[source]

Open image and prediction with Napari.

Parameters:
  • img (numpy.ndarray) – Image data.

  • pred (numpy.ndarray) – Predicted mask (or just mask).

Return type:

None

biom3d.utils.image.resize_3d(img: ndarray, output_shape: tuple[int] | list[int] | ndarray[int], order: int = 3, is_msk: bool = False, monitor_anisotropy: bool = True, anisotropy_threshold: int = 3) ndarray[source]

Resize a 3D image given an output shape.

Parameters:
  • img (numpy.ndarray) – Image to resample, expected shape (C, W, H, D) where C is the channel dimension.

  • output_shape (tuple, list or numpy.ndarray) – Desired output shape. Must be of shape (C, W, H, D) or (W, H, D), (C,H,D) or (H,D) and match the dimensionality.

  • order (int, default=3) – Interpolation order. Use 3 for smooth images, 0 for masks.

  • is_msk (bool, default=False) – Whether the input is a mask. If True, uses nearest-neighbor-like interpolation.

  • monitor_anisotropy (bool, default=True) – Whether to check for axis anisotropy and adapt resizing accordingly.

  • anisotropy_threshold (int, default=3) – Threshold to detect anisotropy. If the ratio between largest and smallest spatial axis exceeds this, anisotropy is triggered.

Raises:
  • AssertionError – If image not in 4D.

  • AssertionError – If output shape not in 3D or 4D.

Returns:

new_img – Resized image.

Return type:

numpy.ndarray

biom3d.utils.image.resize_segmentation(segmentation: ndarray, new_shape: tuple[int, ...], order: int = 3) ndarray[source]

Resize a segmentation map using one-hot encoding to avoid interpolation artifacts.

Copied and adapted from the batch_generator library (Fabian Isensee).

Parameters:
  • segmentation (numpy.ndarray) – The segmentation map to resize. Can be 2D or 3D.

  • new_shape (tuple of int) – The desired output shape. Must match the dimensionality of segmentation.

  • order (int, default=3) – The interpolation order. Use 0 for nearest neighbor (recommended for labels), higher for smoother interpolation.

Raises:

AssertionError – If segmentaion shape and new shape don’t the same number of dimensions.

Returns:

reshaped – The resized segmentation map. Same dtype as input.

Return type:

numpy.ndarray