Image type conversions

Convert images between different numeric datatypes.

machinevisiontoolbox.base.types.int_image(image, intclass='uint8', maxintval=None)[source]

Convert image to integer type

Parameters:
  • image (ndarray(H,W), ndarray(H,W,P)) – input image

  • intclass (str) – integer class to convert to, the name of any integer class supported by NumPy, defaults to 'uint8'

  • maxintval (int) – maximum value of integer, defaults to maximum positive value of image datatype

Returns:

image with integer pixel types

Return type:

ndarray(H,W), ndarray(H,W,P)

Return a copy of the image as a NumPy array with pixel values scaled and converted to the integer class intclass. If the input image is:

  • a floating point class, the pixel values are scaled from an input range of [0.0, 1.0] to a range spanning zero to the maximum positive value of intclass.

  • an integer class, the pixels are scaled and cast to intclass. The scale factor is the ratio of maxintval to the maximum positive value of inclass.

  • the boolean class, False is mapped to zero and True is mapped to the maximum positive value of inclass.

Example:

>>> from machinevisiontoolbox import int_image
>>> import numpy as np
>>> im = np.array([[1,2],[3,4]], 'uint8')
>>> int_image(im, 'int16')
array([[128, 256],
       [385, 513]], dtype=int16)
>>> im = np.array([[False, True],[True, False]])
>>> int_image(im)
array([[  0, 255],
       [255,   0]], dtype=uint8)

Note

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

References:
  • Robotics, Vision & Control for Python, Section 10.1, P. Corke, Springer 2023.

Seealso:

float_image

machinevisiontoolbox.base.types.float_image(image, floatclass='float32', maxintval=None)[source]

Convert image to float type

Parameters:
  • image (ndarray(H,W), ndarray(H,W,P)) – input image

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

  • maxintval (int) – maximum value of integer, defaults to maximum positive value of image datatype

Returns:

image with floating point pixel types

Return type:

ndarray(H,W), ndarray(H,W,P)

Return a copy of the image as a NumPy array with pixels scaled and converted to the float class floatclass with pixel values spanning the range 0.0 to 1.0. If the input image is:

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

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

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

Example:

>>> from machinevisiontoolbox import float_image
>>> import numpy as np
>>> im = np.array([[1,2],[3,4]], 'uint8')
>>> float_image(im)
array([[0.0039, 0.0078],
       [0.0118, 0.0157]], dtype=float32)
>>> im = np.array([[False, True],[True, False]])
>>> float_image(im)
array([[0., 1.],
       [1., 0.]], dtype=float32)

Note

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

References:
  • Robotics, Vision & Control for Python, Section 10.1, P. Corke, Springer 2023.

Seealso:

int_image