stouputils.data_science.data_processing.image.random_erase module#

random_erase_image(image: ndarray[Any, dtype[Any]], erase_factor: float, ignore_dtype: bool = False) ndarray[Any, dtype[Any]][source]#

Randomly erase a rectangle in the image.

Parameters:
  • image (NDArray[Any]) – Image to apply random erase

  • erase_factor (float) – Factor determining the size of the rectangle to erase

  • ignore_dtype (bool) – Ignore the dtype check

Returns:

Image with random erasing applied

Return type:

NDArray[Any]

>>> ## Basic tests
>>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> erased = random_erase_image(image.astype(np.uint8), 0.5)
>>> erased.shape == image.shape
True
>>> np.random.seed(42)
>>> img = np.ones((5,5), dtype=np.uint8) * 255
>>> erased = random_erase_image(img, 0.4)
>>> bool(np.any(erased == 0))  # Should have some erased pixels
True
>>> rgb = np.full((3,3,3), 128, dtype=np.uint8)
>>> erased_rgb = random_erase_image(rgb, 0.3)
>>> erased_rgb.shape == (3,3,3)
True
>>> ## Test invalid inputs
>>> random_erase_image("not an image", 0.5)
Traceback (most recent call last):
        ...
AssertionError: Image must be a numpy array
>>> random_erase_image(image.astype(np.uint8), "0.5")
Traceback (most recent call last):
        ...
AssertionError: erase_factor must be a number, got <class 'str'>