deface
deface copied to clipboard
Add soft blur option
I tested the soft-blur option using this code (OPZIONE 3):
Can you add this option (soft-blur) to main branch? The blur fades around the edge of the ellipse, making the image softer.
def draw_det(
[....]
elif replacewith == 'blur':
bf = 2 # fattore di blur
blurred_box = cv2.blur(
frame[y1:y2, x1:x2],
(max(1, (x2 - x1) // bf), max(1, (y2 - y1) // bf))
)
if ellipse:
h_roi, w_roi = y2 - y1, x2 - x1
yy, xx = np.meshgrid(np.linspace(-1, 1, h_roi), np.linspace(-1, 1, w_roi), indexing='ij')
rr = np.sqrt(xx**2 + yy**2)
# ==========================================================
# OPZIONE 0: Ellisse completamente sfocata (codice iniziale)
# mask = np.zeros_like(rr)
# cv2.ellipse(
# mask, (w_roi // 2, h_roi // 2),
# (w_roi // 2, h_roi // 2), 0, 0, 360, 1, -1
# )
# ==========================================================
# ==========================================================
# OPZIONE 1: Radiale lineare
# mask = np.clip(1 - rr, 0, 1)
# ==========================================================
# ==========================================================
# OPZIONE 2: Radiale quadratica
# mask = np.clip(1 - rr**2, 0, 1)
# ==========================================================
# ==========================================================
# OPZIONE 3: Plateau centrale (ATTIVA DI DEFAULT) - soft blur
inner_radius = 0.6
outer_radius = 1.0
mask = np.ones_like(rr)
mask[rr > inner_radius] = 1 - (rr[rr > inner_radius] - inner_radius) / (outer_radius - inner_radius)
mask = np.clip(mask, 0, 1)
# ==========================================================
mask = mask[:, :, np.newaxis] # broadcast su canali RGB
roibox = frame[y1:y2, x1:x2]
roibox = (blurred_box * mask + roibox * (1 - mask)).astype(np.uint8)
frame[y1:y2, x1:x2] = roibox
grazie