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 of given size (cached). Args: size: Size of the matrix (2, 4, 8, or 16). Returns: 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 to n discrete levels. Args: value: Input value in [0, 1] range. n_levels: Number of discrete levels. Returns: 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 an array to n discrete levels efficiently using vectorized operations. Args: arr: Input array in [0, 1] range. n_levels: Number of discrete levels. Returns: 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
)Apply random dithering optimized for uint8 images. Args: img: Input uint8 image with shape (H, W, C) in [0, 255] range. n_colors: Number of colors per channel after quantization. noise_range: Range of noise to add (min_noise, max_noise) in [0, 1] range. random_generator: Random number generator for reproducible results. Returns: 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
)Apply random dithering for float32 images. Args: img: Input float32 image with shape (H, W, C) in [0, 1] range. n_colors: Number of colors per channel after quantization. noise_range: Range of noise to add (min_noise, max_noise). random_generator: Random number generator for reproducible results. Returns: 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
)Apply ordered dithering optimized for uint8 images. Args: img: Input uint8 image with shape (H, W, C). n_colors: Number of colors per channel. matrix_size: Size of Bayer matrix (2, 4, 8, or 16). Returns: 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
)Apply ordered dithering using Bayer matrix. Args: img: Input image in [0, 1] range with shape (H, W, C). n_colors: Number of colors per channel. matrix_size: Size of Bayer matrix (2, 4, 8, or 16). Returns: 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
)Apply error diffusion dithering. Args: img: Input image in [0, 1] range with shape (H, W, C). n_colors: Number of colors per channel. algorithm: Error diffusion algorithm name. serpentine: Use serpentine (back-and-forth) scanning. Returns: 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 to an image. Args: img: Input image in [0, 1] range with shape (H, W, C). method: Dithering method to use. n_colors: Number of colors per channel. color_mode: How to handle colors ("grayscale", "per_channel", "rgb"). **kwargs: Additional parameters for specific methods. Returns: 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 | - | - |