stouputils.data_science.data_processing.image.threshold module#

threshold_image(image: ndarray[Any, dtype[Any]], thresholds: list[float], ignore_dtype: bool = False) ndarray[Any, dtype[Any]][source]#

Apply multi-level threshold to an image.

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

  • threshold (list[float]) – List of threshold values (between 0 and 1)

  • ignore_dtype (bool) – Ignore the dtype check

Returns:

Multi-level thresholded image

Return type:

NDArray[Any]

>>> ## Basic tests
>>> image = np.array([[100, 150, 200], [50, 125, 175], [25, 75, 225]])
>>> threshold_image(image.astype(np.uint8), [0.3, 0.6]).tolist()
[[85, 85, 170], [0, 85, 170], [0, 0, 170]]
>>> rgb = np.random.randint(0, 256, (3,3,3), dtype=np.uint8)
>>> thresh_rgb = threshold_image(rgb, [0.3, 0.6])
>>> thresh_rgb.shape == rgb.shape
True
>>> ## Test invalid inputs
>>> threshold_image("not an image", [0.5])
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array
>>> threshold_image(image.astype(np.uint8), [1.5])
Traceback (most recent call last):
        ...
AssertionError: threshold values must be between 0 and 1, got [1.5]