Files
2026-01-28 19:33:35 +13:00

37 lines
1.1 KiB
Python

"""
Antialiasing and feathering
"""
import numpy as np
import cv2
def apply_antialiasing(mask: np.ndarray, feather_amount: float = 0.0) -> np.ndarray:
"""
Apply Gaussian blur for antialiasing/feathering.
Args:
mask: Binary mask (0-255)
feather_amount: Amount of feathering to apply (0.0 = no blur, max 100.0)
Returns:
Smoothed mask (0-255)
"""
# If feather_amount is 0 or invalid, return mask unchanged
if feather_amount <= 0.0 or not isinstance(feather_amount, (int, float)) or not np.isfinite(feather_amount):
return mask
# Clamp feather_amount to valid range
feather_amount = float(np.clip(feather_amount, 0.0, 100.0))
# Calculate kernel size: 6 * sigma + 1, ensure it's odd and >= 3
kernel_size = int(6 * feather_amount + 1)
if kernel_size % 2 == 0:
kernel_size += 1
kernel_size = max(3, kernel_size) # Minimum kernel size for cv2.GaussianBlur
# Use feather_amount as sigma
sigma = float(feather_amount)
return cv2.GaussianBlur(mask, (kernel_size, kernel_size), sigma)