stouputils.data_science.data_processing.image.noise module#
- noise_image(image: ndarray[Any, dtype[Any]], amount: float, ignore_dtype: bool = False) ndarray[Any, dtype[Any]] [source]#
Add Gaussian noise to an image.
- Parameters:
image (NDArray[Any]) – Image to add noise to
amount (float) – Amount of noise to add (between 0 and 1)
ignore_dtype (bool) – Ignore the dtype check
- Returns:
Noisy image
- Return type:
NDArray[Any]
>>> ## Basic tests >>> image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> noisy = noise_image(image.astype(np.uint8), 0.5) >>> noisy.shape == image.shape True >>> bool(np.all(noisy >= 0) and np.all(noisy <= 255)) True
>>> np.random.seed(0) >>> image = np.array([[128] * 3] * 3) >>> noise_image(image.astype(np.uint8), 0.1).tolist() [[172, 138, 152], [185, 175, 104], [152, 125, 126]]
>>> rgb = np.full((3,3,3), 128, dtype=np.uint8) >>> noisy_rgb = noise_image(rgb, 0.1) >>> noisy_rgb.shape == (3,3,3) True
>>> ## Test invalid inputs >>> noise_image("not an image", 0.5) Traceback (most recent call last): ... AssertionError: Image must be a numpy array
>>> noise_image(image.astype(np.uint8), "0.5") Traceback (most recent call last): ... AssertionError: amount must be a number, got <class 'str'>
>>> noise_image(image.astype(np.uint8), 1.5) Traceback (most recent call last): ... AssertionError: amount must be between 0 and 1, got 1.5