mirror of
https://github.com/iperov/DeepFaceLive
synced 2025-08-14 02:37:01 -07:00
add xlib.avecl
This commit is contained in:
parent
932edfe875
commit
0058474da7
56 changed files with 5569 additions and 0 deletions
43
xlib/avecl/_internal/op/binary_morph.py
Normal file
43
xlib/avecl/_internal/op/binary_morph.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
from ..Tensor import Tensor
|
||||
from .binary_dilate_circle import binary_dilate_circle
|
||||
from .binary_erode_circle import binary_erode_circle
|
||||
from .gaussian_blur import gaussian_blur
|
||||
from .pad import pad
|
||||
|
||||
|
||||
def binary_morph(input_t : Tensor, erode_dilate : int, blur : float, fade_to_border : bool = False, dtype=None) -> Tensor:
|
||||
"""
|
||||
Apply optional binary erode/dilate and optional blur.
|
||||
|
||||
input_t (...,H,W) tensor. Non zero values will be treated as 1.
|
||||
|
||||
erode_dilate int >= 0 amount of pixels to dilate
|
||||
|
||||
blur float >= 0 amount of pixels to blur
|
||||
|
||||
fade_to_border(False) clip the image in order
|
||||
to fade smoothly to the border with specified blur amount
|
||||
"""
|
||||
x = input_t
|
||||
|
||||
H,W = input_t.shape[-2:]
|
||||
|
||||
x = pad(x, (...,(H,H),(W,W)), mode='constant', constant_value=0)
|
||||
|
||||
if erode_dilate > 0:
|
||||
x = binary_erode_circle(x, radius=1, iterations=max(1,erode_dilate//2))
|
||||
elif erode_dilate < 0:
|
||||
x = binary_dilate_circle(x, radius=1, iterations=max(1,-erode_dilate//2) )
|
||||
|
||||
if fade_to_border:
|
||||
h_clip_size = H + blur // 2
|
||||
w_clip_size = W + blur // 2
|
||||
x[...,:h_clip_size,:] = 0
|
||||
x[...,-h_clip_size:,:] = 0
|
||||
x[...,:,:w_clip_size] = 0
|
||||
x[...,:,-w_clip_size:] = 0
|
||||
|
||||
if blur > 0:
|
||||
x = gaussian_blur(x, blur * 0.250, dtype=dtype)
|
||||
|
||||
return x[...,H:-H,W:-W]
|
Loading…
Add table
Add a link
Reference in a new issue