Merge pull request #26 from yvann-ba/incremental_expandrate_Int-to-float

Change incremental_expandrate to float in GrowMaskWithBlur for smoother animations
This commit is contained in:
Jukka Seppänen 2024-03-05 01:39:13 +02:00 committed by GitHub
commit df86fd77ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -726,7 +726,7 @@ class GrowMaskWithBlur:
"required": { "required": {
"mask": ("MASK",), "mask": ("MASK",),
"expand": ("INT", {"default": 0, "min": -MAX_RESOLUTION, "max": MAX_RESOLUTION, "step": 1}), "expand": ("INT", {"default": 0, "min": -MAX_RESOLUTION, "max": MAX_RESOLUTION, "step": 1}),
"incremental_expandrate": ("INT", {"default": 0, "min": 0, "max": 100, "step": 1}), "incremental_expandrate": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 100.0, "step": 0.1}),
"tapered_corners": ("BOOLEAN", {"default": True}), "tapered_corners": ("BOOLEAN", {"default": True}),
"flip_input": ("BOOLEAN", {"default": False}), "flip_input": ("BOOLEAN", {"default": False}),
"blur_radius": ("FLOAT", { "blur_radius": ("FLOAT", {
@ -739,7 +739,7 @@ class GrowMaskWithBlur:
"decay_factor": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}), "decay_factor": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.01}),
}, },
} }
CATEGORY = "KJNodes/masking" CATEGORY = "KJNodes/masking"
RETURN_TYPES = ("MASK", "MASK",) RETURN_TYPES = ("MASK", "MASK",)
@ -749,7 +749,7 @@ class GrowMaskWithBlur:
def expand_mask(self, mask, expand, tapered_corners, flip_input, blur_radius, incremental_expandrate, lerp_alpha, decay_factor): def expand_mask(self, mask, expand, tapered_corners, flip_input, blur_radius, incremental_expandrate, lerp_alpha, decay_factor):
alpha = lerp_alpha alpha = lerp_alpha
decay = decay_factor decay = decay_factor
if( flip_input ): if flip_input:
mask = 1.0 - mask mask = 1.0 - mask
c = 0 if tapered_corners else 1 c = 0 if tapered_corners else 1
kernel = np.array([[c, 1, c], kernel = np.array([[c, 1, c],
@ -758,17 +758,18 @@ class GrowMaskWithBlur:
growmask = mask.reshape((-1, mask.shape[-2], mask.shape[-1])) growmask = mask.reshape((-1, mask.shape[-2], mask.shape[-1]))
out = [] out = []
previous_output = None previous_output = None
current_expand = expand
for m in growmask: for m in growmask:
output = m.numpy() output = m.numpy()
for _ in range(abs(expand)): for _ in range(abs(round(current_expand))):
if expand < 0: if current_expand < 0:
output = scipy.ndimage.grey_erosion(output, footprint=kernel) output = scipy.ndimage.grey_erosion(output, footprint=kernel)
else: else:
output = scipy.ndimage.grey_dilation(output, footprint=kernel) output = scipy.ndimage.grey_dilation(output, footprint=kernel)
if expand < 0: if current_expand < 0:
expand -= abs(incremental_expandrate) # Use abs(growrate) to ensure positive change current_expand -= abs(incremental_expandrate)
else: else:
expand += abs(incremental_expandrate) # Use abs(growrate) to ensure positive change current_expand += abs(incremental_expandrate)
output = torch.from_numpy(output) output = torch.from_numpy(output)
if alpha < 1.0 and previous_output is not None: if alpha < 1.0 and previous_output is not None:
# Interpolate between the previous and current frame # Interpolate between the previous and current frame
@ -793,9 +794,7 @@ class GrowMaskWithBlur:
return (blurred, 1.0 - blurred) return (blurred, 1.0 - blurred)
else: else:
return (torch.stack(out, dim=0), 1.0 - torch.stack(out, dim=0),) return (torch.stack(out, dim=0), 1.0 - torch.stack(out, dim=0),)
class PlotNode: class PlotNode:
@classmethod @classmethod
def INPUT_TYPES(s): def INPUT_TYPES(s):