diff --git a/albumentations/augmentations/functional.py b/albumentations/augmentations/functional.py index c782b9f25..f58c7cfdd 100644 --- a/albumentations/augmentations/functional.py +++ b/albumentations/augmentations/functional.py @@ -688,7 +688,7 @@ def add_fog( fog_intensity: float, alpha_coef: float, fog_particle_positions: list[tuple[int, int]], - random_state: np.random.RandomState | None = None, + random_state: np.random.RandomState, ) -> np.ndarray: """Add fog to the input image. @@ -697,7 +697,7 @@ def add_fog( fog_intensity (float): Intensity of the fog effect, between 0 and 1. alpha_coef (float): Base alpha (transparency) value for fog particles. fog_particle_positions (list[tuple[int, int]]): List of (x, y) coordinates for fog particles. - random_state (np.random.RandomState | None): If specified, this will be random state used + random_state (np.random.RandomState): Random state used Returns: np.ndarray: Image with added fog effect. """ @@ -706,13 +706,19 @@ def add_fog( fog_layer = np.zeros((height, width, num_channels), dtype=np.uint8) - max_fog_radius = int( - min(height, width) * 0.1 * fog_intensity, - ) # Maximum radius scales with image size and intensity + max_value = MAX_VALUES_BY_DTYPE[np.uint8] + + max_fog_radius = max( + 2, + int( + min(height, width) * 0.1 * fog_intensity, + ), + ) for x, y in fog_particle_positions: - radius = random_utils.randint(max_fog_radius // 2, max_fog_radius, random_state=random_state) - color = 255 if num_channels == 1 else (255,) * num_channels + min_radius = max(1, max_fog_radius // 2) + radius = random_utils.randint(min_radius, max_fog_radius, random_state=random_state) + color = max_value if num_channels == 1 else (max_value,) * num_channels cv2.circle( fog_layer, center=(x, y), @@ -725,7 +731,7 @@ def add_fog( fog_layer = cv2.GaussianBlur(fog_layer, (25, 25), 0) # Blend the fog layer with the original image - alpha = np.mean(fog_layer, axis=2, keepdims=True) / 255 * alpha_coef * fog_intensity + alpha = np.mean(fog_layer, axis=2, keepdims=True) / max_value * alpha_coef * fog_intensity fog_image = img * (1 - alpha) + fog_layer * alpha return fog_image.astype(np.uint8) diff --git a/albumentations/augmentations/transforms.py b/albumentations/augmentations/transforms.py index 90538c7f4..5c2c62291 100644 --- a/albumentations/augmentations/transforms.py +++ b/albumentations/augmentations/transforms.py @@ -1108,10 +1108,10 @@ def apply( img: np.ndarray, particle_positions: np.ndarray, intensity: float, - random_seed: int, + random_state: np.random.RandomState, **params: Any, ) -> np.ndarray: - return fmain.add_fog(img, intensity, self.alpha_coef, particle_positions, np.random.RandomState(random_seed)) + return fmain.add_fog(img, intensity, self.alpha_coef, particle_positions, random_state) def get_params_dependent_on_data(self, params: dict[str, Any], data: dict[str, Any]) -> dict[str, Any]: # Select a random fog intensity within the specified range @@ -1159,7 +1159,7 @@ def get_params_dependent_on_data(self, params: dict[str, Any], data: dict[str, A return { "particle_positions": particle_positions, "intensity": intensity, - "random_seed": random_utils.get_random_seed(), + "random_state": random_utils.get_random_state(), } def get_transform_init_args_names(self) -> tuple[str, str]: