Image type conversions#
Convert images between different numeric datatypes.
NumPy array conversion utilities for integer and floating-point image data.
- DTYPE_ALIASES: dict[str, str] = {'double': 'float64', 'float': 'float32', 'half': 'float16', 'int': 'uint8'}#
Short-name aliases for dtype strings accepted throughout the Toolbox (e.g. Image(…, dtype=’float’), VideoFile(…, dtype=’float’)). NumPy’s own np.dtype(‘float’) resolves to float64, not float32 – these aliases exist specifically so ‘float’/’int’ mean the Toolbox’s own default single-precision/8-bit types, not NumPy’s C-double/C-long defaults. Single source of truth: resolve any dtype string through this table (dtype_alias.get(dtype, dtype)) before passing it to np.dtype().
- float_image(image: ndarray, floatclass: DTypeLike = 'float32', maxintval: int | None = None) ndarray[source][source]#
Convert image to float type
- Parameters:
- 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
floatclasswith 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
maxintvalto [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.
Examples
>>> 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
See also
- int_image(image: ndarray, intclass: DTypeLike = 'uint8', maxintval: int | None = None) ndarray[source][source]#
Convert image to integer type
- Parameters:
- 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. Values outside [0.0, 1.0] are clipped first, so 1.2 gives the maximum value and -0.5 gives zero. Without this, they would wrap around to an unrelated value.an integer class, the pixels are scaled and cast to
intclass. The scale factor is the ratio ofmaxintvalto the maximum positive value ofinclass.the boolean class, False is mapped to zero and True is mapped to the maximum positive value of
inclass.
Examples
>>> 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) >>> im = np.array([-0.5, 0.5, 1.2]) >>> int_image(im) # values outside 0 to 1 are clipped array([ 0, 128, 255], dtype=uint8)
Note
Works for greyscale or color (arbitrary number of planes) image
References
See also