albumentations.augmentations.pixel.dithering_functional
Functional implementations of dithering algorithms for color depth reduction.
Members
- functiongenerate_bayer_matrix
- functionquantize_value
- functionquantize_array
- functionrandom_dither_uint8
- functionrandom_dither
- functionordered_dither_uint8
- functionordered_dither
- functionerror_diffusion_dither
- functionapply_dithering
generate_bayer_matrixfunction
generate_bayer_matrix(
size: int
)Generate Bayer threshold matrix for ordered dithering (cached). size in {2,4,8,16}. Returns matrix in [0, 1]. Tiled to image size in ordered-dither paths. Args: size (int): Size of the matrix (2, 4, 8, or 16). Returns: np.ndarray: Bayer matrix normalized to [0, 1] range.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| size | int | - | - |
quantize_valuefunction
quantize_value(
value: float,
n_levels: int
)Quantize a single value in [0, 1] to n_levels discrete levels. Used in error diffusion and other dithering paths. Returns value in [0, 1]. Args: value (float): Input value in [0, 1] range. n_levels (int): Number of discrete levels. Returns: float: Quantized value in [0, 1] range.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| value | float | - | - |
| n_levels | int | - | - |
quantize_arrayfunction
quantize_array(
arr: np.ndarray,
n_levels: int
)Quantize array in [0, 1] to n_levels levels (vectorized). Returns float32. Used when full-image quantization is needed without error diffusion. Args: arr (np.ndarray): Input array in [0, 1] range. n_levels (int): Number of discrete levels. Returns: np.ndarray: Quantized array in [0, 1] range.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| arr | np.ndarray | - | - |
| n_levels | int | - | - |
random_dither_uint8function
random_dither_uint8(
img: ImageUInt8,
n_colors: int,
noise_range: tuple[float, float],
random_generator: np.random.Generator
)Random dithering for uint8: add noise then quantize with LUT. n_colors, noise_range, random_generator. Fast path for Dithering transform when input is uint8. Args: img (ImageUInt8): Input uint8 image with shape (H, W, C) in [0, 255] range. n_colors (int): Number of colors per channel after quantization. noise_range (tuple[float, float]): Range of noise to add (min_noise, max_noise) in [0, 1] range. random_generator (np.random.Generator): Random number generator for reproducible results. Returns: ImageUInt8: Dithered uint8 image in [0, 255] range.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| img | ImageUInt8 | - | - |
| n_colors | int | - | - |
| noise_range | tuple[float, float] | - | - |
| random_generator | np.random.Generator | - | - |
random_ditherfunction
random_dither(
img: ImageFloat32,
n_colors: int,
noise_range: tuple[float, float],
random_generator: np.random.Generator
)Random dithering for float32: add noise then quantize to n_colors. noise_range and random_generator control the dither. Float32 input and output in [0, 1]. Args: img (ImageFloat32): Input float32 image with shape (H, W, C) in [0, 1] range. n_colors (int): Number of colors per channel after quantization. noise_range (tuple[float, float]): Range of noise to add (min_noise, max_noise). random_generator (np.random.Generator): Random number generator for reproducible results. Returns: ImageFloat32: Dithered float32 image in [0, 1] range.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| img | ImageFloat32 | - | - |
| n_colors | int | - | - |
| noise_range | tuple[float, float] | - | - |
| random_generator | np.random.Generator | - | - |
ordered_dither_uint8function
ordered_dither_uint8(
img: ImageUInt8,
n_colors: int,
matrix_size: int = 4
)Ordered (Bayer) dithering for uint8: threshold vs tiled Bayer. n_colors, matrix_size (2,4,8,16). Fast path for Dithering transform. Args: img (ImageUInt8): Input uint8 image with shape (H, W, C). n_colors (int): Number of colors per channel. matrix_size (int): Size of Bayer matrix (2, 4, 8, or 16). Returns: ImageUInt8: Dithered uint8 image.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| img | ImageUInt8 | - | - |
| n_colors | int | - | - |
| matrix_size | int | 4 | - |
ordered_ditherfunction
ordered_dither(
img: ImageFloat32,
n_colors: int,
matrix_size: int = 4
)Ordered (Bayer) dithering for float32: add Bayer threshold pattern then quantize. matrix_size in {2,4,8,16}. Float32 images in [0, 1]. Args: img (ImageFloat32): Input image in [0, 1] range with shape (H, W, C). n_colors (int): Number of colors per channel. matrix_size (int): Size of Bayer matrix (2, 4, 8, or 16). Returns: ImageFloat32: Dithered image in [0, 1] range.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| img | ImageFloat32 | - | - |
| n_colors | int | - | - |
| matrix_size | int | 4 | - |
error_diffusion_ditherfunction
error_diffusion_dither(
img: ImageType,
n_colors: int,
algorithm: str = floyd_steinberg,
serpentine: bool = False
)Error diffusion dithering (e.g. Floyd-Steinberg): quantize and diffuse error. algorithm, serpentine. Per-channel processing. Args: img (ImageType): Input image in [0, 1] range with shape (H, W, C). n_colors (int): Number of colors per channel. algorithm (str): Error diffusion algorithm name. serpentine (bool): Use serpentine (back-and-forth) scanning. Returns: ImageType: Dithered image in [0, 1] range.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| img | ImageType | - | - |
| n_colors | int | - | - |
| algorithm | str | floyd_steinberg | - |
| serpentine | bool | False | - |
apply_ditheringfunction
apply_dithering(
img: ImageType,
method: str,
n_colors: int,
color_mode: str = per_channel,
**kwargs: Any
)Apply dithering (ordered, random, or error_diffusion) to image. method, n_colors, color_mode, **kwargs. Entry point for Dithering transform. Args: img (ImageType): Input image in [0, 1] range with shape (H, W, C). method (str): Dithering method to use. n_colors (int): Number of colors per channel. color_mode (str): How to handle colors ("grayscale", "per_channel", "rgb"). **kwargs (Any): Additional parameters for specific methods. Returns: ImageType: Dithered image in [0, 1] range with shape (H, W, C).
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| img | ImageType | - | - |
| method | str | - | - |
| n_colors | int | - | - |
| color_mode | str | per_channel | - |
| **kwargs | Any | - | - |