ToFloat
Targets:
image
volume
Image Types:uint8, uint16, uint32, float32
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.
Arguments
max_valuefloat | 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
- uint16: 65535
- uint32: 4294967295
- float32: 1.0 Default: None.
pfloat
1
Probability of applying the transform. Default: 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
>>>
# Convert uint16 image to float with custom max_value
>>> image = np.random.randint(0, 4096, (100, 100, 3), dtype=np.uint16)
>>> transform = A.ToFloat(max_value=4095)
>>> float_image = transform(image=image)['image']
>>> assert float_image.dtype == np.float32
>>> assert 0 <= float_image.min() <= float_image.max() <= 1.0Returns
np.ndarray
Image in floating point representation, with values in range [0, 1.0].
Notes
- If the input image is already float32 with values in [0, 1], it will be returned unchanged.
- For integer types (uint8, uint16, uint32), 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.
See Also
FromFloat: The inverse operation, converting from float back to the original data type.