stouputils.data_science.data_processing.image.normalize module#

normalize_image(image: ndarray[Any, dtype[Any]], a: float | int = 0, b: float | int = 255, method: int = 32, ignore_dtype: bool = False) ndarray[Any, dtype[Any]][source]#

Normalize an image to the range 0-255.

Parameters:
  • image (NDArray[Any]) – Image to normalize

  • a (float | int) – Minimum value (default: 0)

  • b (float | int) – Maximum value (default: 255)

  • method (int) – Normalization method (default: cv2.NORM_MINMAX)

  • ignore_dtype (bool) – Ignore the dtype check

Returns:

Normalized image

Return type:

NDArray[Any]

>>> ## Basic tests
>>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=np.uint8)
>>> normalized = normalize_image(image)
>>> normalized.tolist()
[[0, 32, 64], [96, 128, 159], [191, 223, 255]]
>>> normalized.shape == image.shape
True
>>> normalized.dtype == image.dtype
True
>>> ## Test invalid inputs
>>> normalize_image("not an image")
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array
>>> normalize_image(image, a="not an integer")
Traceback (most recent call last):
        ...
AssertionError: a must be a float or an integer
>>> normalize_image(image, b="not an integer")
Traceback (most recent call last):
        ...
AssertionError: b must be a float or an integer