Stay updated

News & Insights
transform
utils
transforms
API Documentation | Albumentations

albumentations.augmentations.transforms3d.functional


Module containing functional implementations of 3D transformations. This module provides a collection of utility functions for manipulating and transforming 3D volumetric data (such as medical imaging volumes). The functions here implement the core algorithms for operations like padding, cropping, rotation, and other spatial manipulations specifically designed for 3D data.

adjust_padding_by_position3dfunction

Adjust padding values based on desired position for 3D data.

Parameters

NameTypeDefaultDescription
paddingslist[tuple[int, int]]-List of tuples containing padding pairs for each dimension [(d_pad), (h_pad), (w_pad)]
position
One of:
  • 'center'
  • 'random'
-Position of the image after padding.
py_randomrandom.Random-Random number generator

Returns

  • tuple[int, int, int, int, int, int]: Final padding values (d_front, d_back, h_top, h_bottom, w_left, w_right)

create_shape_groups_3dfunction

Groups 3D tiles by their shape and stores the indices for each shape.

Parameters

NameTypeDefaultDescription
tilesnp.ndarray--

crop3dfunction

Crop 3D volume using coordinates.

Parameters

NameTypeDefaultDescription
volumenp.ndarray-Input volume with shape (z, y, x) or (z, y, x, channels)
crop_coordstuple[int, int, int, int, int, int]-(z_min, z_max, y_min, y_max, x_min, x_max) coordinates for cropping

Returns

  • np.ndarray: Cropped volume with same number of dimensions as input

cutout3dfunction

Cut out holes in 3D volume and fill them with a given value.

Parameters

NameTypeDefaultDescription
volumenp.ndarray-Input volume with shape (depth, height, width) or (depth, height, width, channels)
holesnp.ndarray-Array of holes with shape (num_holes, 6). Each hole is represented as [z1, y1, x1, z2, y2, x2]
fill
One of:
  • tuple[float, ...]
  • float
-Value to fill the holes

Returns

  • np.ndarray: Volume with holes filled with the given value

filter_keypoints_in_holes3dfunction

Filter out keypoints that are inside any of the 3D holes.

Parameters

NameTypeDefaultDescription
keypointsnp.ndarray-Array of keypoints with shape (num_keypoints, 3+). The first three columns are x, y, z coordinates.
holesnp.ndarray-Array of holes with shape (num_holes, 6). Each hole is represented as [z1, y1, x1, z2, y2, x2].

Returns

  • np.ndarray: Array of keypoints that are not inside any hole.

keypoints_rot90function

Rotate keypoints 90 degrees k times around the specified axes.

Parameters

NameTypeDefaultDescription
keypointsnp.ndarray-Array of keypoints with shape (num_keypoints, 3+). The first three columns are x, y, z coordinates.
kint-Number of times to rotate by 90 degrees.
axestuple[int, int]-Axes to rotate around.
volume_shapetuple[int, int, int]-Shape of the volume (depth, height, width).

Returns

  • np.ndarray: Rotated keypoints with same shape as input.

pad_3d_with_paramsfunction

Pad 3D volume with given parameters.

Parameters

NameTypeDefaultDescription
volumenp.ndarray-Input volume with shape (depth, height, width) or (depth, height, width, channels)
paddingtuple[int, int, int, int, int, int]-Padding values in format: (depth_front, depth_back, height_top, height_bottom, width_left, width_right) where: - depth_front/back: padding at start/end of depth axis (z) - height_top/bottom: padding at start/end of height axis (y) - width_left/right: padding at start/end of width axis (x)
value
One of:
  • tuple[float, ...]
  • float
-Value to fill the padding

Returns

  • np.ndarray: Padded volume with same number of dimensions as input

Notes

The padding order matches the volume dimensions (depth, height, width). For each dimension, the first value is padding at the start (smaller indices), and the second value is padding at the end (larger indices).

shuffle_tiles_within_shape_groups_3dfunction

Shuffles indices within each group of similar shapes and creates a list where each index points to the index of the tile it should be mapped to.

Parameters

NameTypeDefaultDescription
shape_groupsdict[tuple[int, int, int], list[int]]-Dictionary mapping shapes to list of tile indices with that shape
random_generatornp.random.Generator-Random number generator for shuffling

Returns

  • : List where index i contains the new position for tile i

split_uniform_grid_3dfunction

Splits a 3D volume shape into a uniform grid specified by the grid dimensions.

Parameters

NameTypeDefaultDescription
volume_shapetuple[int, int, int]-The shape of the volume as (depth, height, width).
gridtuple[int, int, int]-The grid size as (depth_slices, rows, columns).
random_generatornp.random.Generator-The random generator to use for shuffling the splits.

Returns

  • np.ndarray: An array containing the tiles' coordinates in the format

swap_tiles_on_keypoints_3dfunction

Swap the positions of 3D keypoints based on a tile mapping.

Parameters

NameTypeDefaultDescription
keypointsnp.ndarray-A 2D numpy array of shape (N, 3+) where N is the number of keypoints. Each row represents a keypoint's (x, y, z) coordinates plus other data.
tilesnp.ndarray-A 2D numpy array of shape (M, 6) where M is the number of tiles. Each row represents a tile's (z_start, y_start, x_start, z_end, y_end, x_end).
mappingnp.ndarray-A 1D numpy array of shape (M,) where M is the number of tiles. Each element i contains the index of the tile that tile i should be swapped with.

Returns

  • np.ndarray: A 2D numpy array of the same shape as the input keypoints, containing the new positions

swap_tiles_on_volumefunction

Swap tiles on the 3D volume according to the mapping.

Parameters

NameTypeDefaultDescription
volumenp.ndarray-Input volume with shape (D, H, W) or (D, H, W, C).
tilesnp.ndarray-Array of tiles with each tile as [z_start, y_start, x_start, z_end, y_end, x_end].
mappinglist[int]-List of new tile indices. Must have the same length as tiles.

Returns

  • np.ndarray: Output volume with tiles swapped according to the random shuffle.

Notes

This implementation uses a loop rather than vectorized operations because tiles may have variable sizes in the general case (e.g., when the volume dimensions aren't evenly divisible by the grid size). Advanced indexing with variable-sized slices isn't possible in NumPy, making this loop-based approach the most efficient solution.

transform_cubefunction

Transform cube by index (0-47)

Parameters

NameTypeDefaultDescription
cubenp.ndarray-Input array with shape (D, H, W) or (D, H, W, C)
indexint-Integer from 0 to 47 specifying which transformation to apply

Returns

  • np.ndarray: Transformed cube with same shape as input

transform_cube_keypointsfunction

Transform keypoints according to the cube transformation specified by index.

Parameters

NameTypeDefaultDescription
keypointsnp.ndarray-Array of keypoints with shape (num_keypoints, 3+). The first three columns are x, y, z coordinates.
indexint-Integer from 0 to 47 specifying which transformation to apply.
volume_shapetuple[int, int, int]-Shape of the volume (depth, height, width).

Returns

  • np.ndarray: Transformed keypoints with same shape as input.