Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix random fog #1981

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions albumentations/augmentations/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
"""
Expand All @@ -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),
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions albumentations/augmentations/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
Loading