Skip to content

Commit

Permalink
Speed up in elastic
Browse files Browse the repository at this point in the history
  • Loading branch information
ternaus committed Jan 30, 2025
1 parent 2dac564 commit 91c7c7e
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions albumentations/augmentations/geometric/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1599,18 +1599,23 @@ def generate_displacement_fields(
size=(1 if same_dxdy else 2, *image_shape[:2]),
).astype(np.float32)

# Apply Gaussian blur if needed using fast OpenCV operations
# # Apply Gaussian blur if needed using fast OpenCV operations
if kernel_size != (0, 0):
# Use faster OpenCV operations for Gaussian blur
for i in range(fields.shape[0]):
# Use cv2.BORDER_REPLICATE to avoid edge artifacts
cv2.GaussianBlur(
fields[i],
kernel_size,
sigma,
dst=fields[i],
borderType=cv2.BORDER_REPLICATE,
)
# Reshape to 2D array (combining first dimension with height)
shape = fields.shape
fields = fields.reshape(-1, shape[-1])

# Apply blur to all fields at once
cv2.GaussianBlur(
fields,
kernel_size,
sigma,
dst=fields,
borderType=cv2.BORDER_REPLICATE,
)

# Restore original shape
fields = fields.reshape(shape)

# Scale by alpha inplace
fields *= alpha
Expand Down

0 comments on commit 91c7c7e

Please sign in to comment.