Stay updated

News & Insights
utils

albumentations.augmentations.other.type_transform


Convert an image from floating point representation to the specified data type.

Members

FromFloatclass

FromFloat(
    dtype: 'uint8' = uint8,
    max_value: float | None,
    p: float = 1.0
)

Convert an image from floating point representation to the specified data type. This transform is designed to convert images from a normalized floating-point representation (typically with values in the range [0, 1]) to other data types, scaling the values appropriately.

Parameters

NameTypeDefaultDescription
dtype'uint8'uint8The desired output data type. Only 'uint8' is supported. Default: 'uint8'.
max_value
One of:
  • float
  • None
-The maximum value for the output dtype. If None, 255 for uint8.
pfloat1.0Probability of applying the transform. Default: 1.0.

Examples

>>> import numpy as np
>>> import albumentations as A
>>> transform = A.FromFloat(dtype='uint8', max_value=None, p=1.0)
>>> image = np.random.rand(100, 100, 3).astype(np.float32)  # Float image in [0, 1] range
>>> result = transform(image=image)
>>> uint8_image = result['image']
>>> assert uint8_image.dtype == np.uint8
>>> assert uint8_image.min() >= 0 and uint8_image.max() <= 255

Notes

- This is the inverse transform for ToFloat. - Input images are expected to be in floating point format with values in the range [0, 1]. - Output is uint8 with values scaled to [0, 255]. - The transform uses the `from_float` function internally, which ensures output values are within the valid range for the specified dtype.

ToFloatclass

ToFloat(
    max_value: float | None,
    p: float = 1.0
)

Convert the input image to a floating-point representation. This transform divides pixel values by `max_value` to get a float32 output array where all values lie in the range [0, 1.0]. It's useful for normalizing image data before feeding it into neural networks or other algorithms that expect float input.

Parameters

NameTypeDefaultDescription
max_value
One of:
  • float
  • None
-The maximum possible input value. If None, the transform will try to infer the maximum value by inspecting the data type of the input image: - uint8: 255 - float32: 1.0 Default: None.
pfloat1.0Probability of applying the transform. Default: 1.0.

Returns

  • np.ndarray: Image in floating point representation, with values in range [0, 1.0].

Examples

>>> import numpy as np
>>> import albumentations as A
>>>
# Convert uint8 image to float
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> transform = A.ToFloat(max_value=None)
>>> float_image = transform(image=image)['image']
>>> assert float_image.dtype == np.float32
>>> assert 0 <= float_image.min() <= float_image.max() <= 1.0
>>>

Notes

- If the input image is already float32 with values in [0, 1], it will be returned unchanged. - For uint8, the function will scale the values to [0, 1] range. - The output will always be float32, regardless of the input type. - This transform is often used as a preprocessing step before applying other transformations or feeding the image into a neural network.