Skip to content

Commit

Permalink
Fix in RandomFog
Browse files Browse the repository at this point in the history
  • Loading branch information
ternaus committed Oct 8, 2024
1 parent 6a8cb2b commit 95f44ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
18 changes: 12 additions & 6 deletions albumentations/augmentations/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 | None = None,
**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

0 comments on commit 95f44ab

Please sign in to comment.