albumentations.augmentations.utils


Module containing utility functions for augmentation operations. This module provides a collection of helper functions and utilities used throughout the augmentation pipeline. It includes functions for image loading, type checking, error handling, mathematical operations, and decorators that add functionality to other functions in the codebase. These utilities help ensure consistent behavior and simplify common operations across different augmentation transforms.

Members

PCAclass

PCA(
    n_components: int | None = None
)

Parameters

NameTypeDefaultDescription
n_components
One of:
  • int
  • None
None-

angle_2pi_rangefunction

angle_2pi_range(
    func: Callable[Concatenate[np.ndarray, P], np.ndarray]
)

Decorator to normalize angle values to the range [0, 2π). This decorator wraps a function that processes keypoints, ensuring that angle values (stored in the 4th column, index 3) are normalized to the range [0, 2π) after the wrapped function executes.

Parameters

NameTypeDefaultDescription
funcCallable[Concatenate[np.ndarray, P], np.ndarray]-Function that processes keypoints and returns a numpy array. The function should take a keypoints array as its first parameter.

Returns

  • Callable: Wrapped function that normalizes angles after processing keypoints.

check_rangefunction

check_range(
    value: tuple[float, float],
    lower_bound: float,
    upper_bound: float,
    name: str | None
)

Checks if the given value is within the specified bounds

Parameters

NameTypeDefaultDescription
valuetuple[float, float]-The value to check and convert. Can be a single float or a tuple of floats.
lower_boundfloat-The lower bound for the range check.
upper_boundfloat-The upper bound for the range check.
name
One of:
  • str
  • None
-The name of the parameter being checked. Used for error messages.

handle_empty_arrayfunction

handle_empty_array(
    param_name: str
)

Parameters

NameTypeDefaultDescription
param_namestr--

non_rgb_errorfunction

non_rgb_error(
    image: np.ndarray
)

Check if the input image is RGB and raise a ValueError if it's not. This function is used to ensure that certain transformations are only applied to RGB images. It provides helpful error messages for grayscale and multi-spectral images.

Parameters

NameTypeDefaultDescription
imagenp.ndarray-The input image to check. Expected to be a numpy array representing an image.

Example

>>> import numpy as np
>>> rgb_image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> non_rgb_error(rgb_image)  # No error raised
>>>
>>> grayscale_image = np.random.randint(0, 256, (100, 100), dtype=np.uint8)
>>> non_rgb_error(grayscale_image)  # Raises ValueError with conversion instructions
>>>
>>> multispectral_image = np.random.randint(0, 256, (100, 100, 5), dtype=np.uint8)
>>> non_rgb_error(multispectral_image)  # Raises ValueError stating incompatibility

Notes

- RGB images are expected to have exactly 3 channels. - Grayscale images (1 channel) will trigger an error with conversion instructions. - Multi-spectral images (more than 3 channels) will trigger an error stating incompatibility.

read_bgr_imagefunction

read_bgr_image(
    path: str | Path
)

Read an image in BGR format from the specified path.

Parameters

NameTypeDefaultDescription
path
One of:
  • str
  • Path
-Path to the image file.

Returns

  • np.ndarray: Image in BGR format as a numpy array.

read_grayscalefunction

read_grayscale(
    path: str | Path
)

Read a grayscale image from the specified path.

Parameters

NameTypeDefaultDescription
path
One of:
  • str
  • Path
-Path to the image file.

Returns

  • np.ndarray: Grayscale image as a numpy array.

read_rgb_imagefunction

read_rgb_image(
    path: str | Path
)

Read an image in RGB format from the specified path. This function reads an image in BGR format using OpenCV and then converts it to RGB format.

Parameters

NameTypeDefaultDescription
path
One of:
  • str
  • Path
-Path to the image file.

Returns

  • np.ndarray: Image in RGB format as a numpy array.