mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2025-01-11 02:15:17 +00:00
Add an experimental node to sharpen latents.
Can be used with LatentApplyOperationCFG for interesting results.
This commit is contained in:
parent
c695c4af7f
commit
f71cfd2687
@ -221,6 +221,55 @@ class LatentOperationTonemapReinhard:
|
|||||||
return normalized_latent * new_magnitude
|
return normalized_latent * new_magnitude
|
||||||
return (tonemap_reinhard,)
|
return (tonemap_reinhard,)
|
||||||
|
|
||||||
|
class LatentOperationSharpen:
|
||||||
|
@classmethod
|
||||||
|
def INPUT_TYPES(s):
|
||||||
|
return {"required": {
|
||||||
|
"sharpen_radius": ("INT", {
|
||||||
|
"default": 9,
|
||||||
|
"min": 1,
|
||||||
|
"max": 31,
|
||||||
|
"step": 1
|
||||||
|
}),
|
||||||
|
"sigma": ("FLOAT", {
|
||||||
|
"default": 1.0,
|
||||||
|
"min": 0.1,
|
||||||
|
"max": 10.0,
|
||||||
|
"step": 0.1
|
||||||
|
}),
|
||||||
|
"alpha": ("FLOAT", {
|
||||||
|
"default": 0.1,
|
||||||
|
"min": 0.0,
|
||||||
|
"max": 5.0,
|
||||||
|
"step": 0.01
|
||||||
|
}),
|
||||||
|
}}
|
||||||
|
|
||||||
|
RETURN_TYPES = ("LATENT_OPERATION",)
|
||||||
|
FUNCTION = "op"
|
||||||
|
|
||||||
|
CATEGORY = "latent/advanced/operations"
|
||||||
|
EXPERIMENTAL = True
|
||||||
|
|
||||||
|
def op(self, sharpen_radius, sigma, alpha):
|
||||||
|
def sharpen(latent, **kwargs):
|
||||||
|
luminance = (torch.linalg.vector_norm(latent, dim=(1)) + 1e-6)[:,None]
|
||||||
|
normalized_latent = latent / luminance
|
||||||
|
channels = latent.shape[1]
|
||||||
|
|
||||||
|
kernel_size = sharpen_radius * 2 + 1
|
||||||
|
kernel = comfy_extras.nodes_post_processing.gaussian_kernel(kernel_size, sigma, device=luminance.device)
|
||||||
|
center = kernel_size // 2
|
||||||
|
|
||||||
|
kernel *= alpha * -10
|
||||||
|
kernel[center, center] = kernel[center, center] - kernel.sum() + 1.0
|
||||||
|
|
||||||
|
padded_image = torch.nn.functional.pad(normalized_latent, (sharpen_radius,sharpen_radius,sharpen_radius,sharpen_radius), 'reflect')
|
||||||
|
sharpened = torch.nn.functional.conv2d(padded_image, kernel.repeat(channels, 1, 1).unsqueeze(1), padding=kernel_size // 2, groups=channels)[:,:,sharpen_radius:-sharpen_radius, sharpen_radius:-sharpen_radius]
|
||||||
|
|
||||||
|
return luminance * sharpened
|
||||||
|
return (sharpen,)
|
||||||
|
|
||||||
NODE_CLASS_MAPPINGS = {
|
NODE_CLASS_MAPPINGS = {
|
||||||
"LatentAdd": LatentAdd,
|
"LatentAdd": LatentAdd,
|
||||||
"LatentSubtract": LatentSubtract,
|
"LatentSubtract": LatentSubtract,
|
||||||
@ -231,4 +280,5 @@ NODE_CLASS_MAPPINGS = {
|
|||||||
"LatentApplyOperation": LatentApplyOperation,
|
"LatentApplyOperation": LatentApplyOperation,
|
||||||
"LatentApplyOperationCFG": LatentApplyOperationCFG,
|
"LatentApplyOperationCFG": LatentApplyOperationCFG,
|
||||||
"LatentOperationTonemapReinhard": LatentOperationTonemapReinhard,
|
"LatentOperationTonemapReinhard": LatentOperationTonemapReinhard,
|
||||||
|
"LatentOperationSharpen": LatentOperationSharpen,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user