machinevisiontoolbox.Image.to_int

Image.to_int(intclass='uint8')[source]

Image as integer NumPy array

Parameters:

intclass (str, optional) – name of NumPy supported integer class, default is ‘uint8’

Returns:

NumPy array with integer values

Return type:

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

Return a NumPy array with pixels converted to the integer class intclass. For the case where the input image is:

  • a floating point class, the pixel values are scaled from an input range of [0,1] to a range spanning zero to the maximum positive value of the output integer class.

  • an integer class, then the pixels are scaled and cast to intclass. The scale factor is the ratio of the maximum value of the input and output integer classes.

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

Example:

>>> from machinevisiontoolbox import Image
>>> img = Image([[5_000, 10_000], [30_000, 60_000]])
>>> img
Image: 2 x 2 (uint16)
>>> img.to_int('uint8')
array([[ 19,  38],
       [116, 233]], dtype=uint8)
>>> img.to_int('uint32')
array([[ 327685000,  655370000],
       [1966110000, 3932220000]], dtype=uint32)
>>> img = Image([[0.0, 0.3], [0.5, 1.0]])
>>> img
Image: 2 x 2 (float32)
>>> img.to_int('uint8')
array([[  0,  76],
       [128, 255]], dtype=uint8)
>>> img = Image([[False, True], [True, False]])
>>> img
Image: 2 x 2 (bool)
>>> img.to_int('uint8')
array([[  0, 255],
       [255,   0]], dtype=uint8)
>>> img.to_int('uint16')
array([[    0, 65535],
       [65535,     0]], dtype=uint16)
Note:

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

Seealso:

to_float cast like