machinevisiontoolbox.Image.to_float

Image.to_float(floatclass='float32')[source]

Image as float NumPy array

Parameters:

floatclass (str) – ‘single’, ‘double’, ‘float32’ [default], ‘float64’

Returns:

Image with floating point pixel types

Return type:

Image

Return a NumPy array with pixels converted to the floating point class floatclass and the values span the range 0 to 1. For the case where the input image is:

  • an integer class, the pixel values are scaled from an input range spanning zero to the maximum positive value of the integer class to [0.0, 1.0]

  • a floating class, the pixels are cast to change type but not their value.

  • boolean class, False is mapped to 0.0 and True is mapped to 1.0.

Example:

>>> from machinevisiontoolbox import Image
>>> img = Image([[50, 100], [150, 200]])
>>> img
Image: 2 x 2 (uint8)
>>> img.to_float()
array([[0.1961, 0.3922],
       [0.5882, 0.7843]], dtype=float32)
>>> img = Image([[0.0, 0.3], [0.5, 1.0]])
>>> img
Image: 2 x 2 (float32)
>>> img.to_float('float64')
array([[0. , 0.3],
       [0.5, 1. ]])
>>> img = Image([[False, True], [True, False]])
>>> img.to_float()
array([[0., 1.],
       [1., 0.]], dtype=float32)
Note:

Works for greyscale or color (arbitrary number of planes) image

Seealso:

to_int cast like