forked from comfyanonymous/ComfyUI
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Morphology nodes from kornia (comfyanonymous#2781)
* import kornia * Added morphology nodexs * Add kornia to requirements * fix choices * options, also move to postprocessors * fix placing and step
- Loading branch information
1 parent
caddef8
commit 6d8834f
Showing
4 changed files
with
52 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import torch | ||
import comfy.model_management | ||
|
||
from kornia.morphology import dilation, erosion, opening, closing, gradient, top_hat, bottom_hat | ||
|
||
|
||
class Morphology: | ||
@classmethod | ||
def INPUT_TYPES(s): | ||
return {"required": {"image": ("IMAGE",), | ||
"operation": (["erode", "dilate", "open", "close", "gradient", "bottom_hat", "top_hat"],), | ||
"kernel_size": ("INT", {"default": 3, "min": 3, "max": 999, "step": 1}), | ||
}} | ||
|
||
RETURN_TYPES = ("IMAGE",) | ||
FUNCTION = "process" | ||
|
||
CATEGORY = "image/postprocessing" | ||
|
||
def process(self, image, operation, kernel_size): | ||
device = comfy.model_management.get_torch_device() | ||
kernel = torch.ones(kernel_size, kernel_size, device=device) | ||
image_k = image.to(device).movedim(-1, 1) | ||
if operation == "erode": | ||
output = erosion(image_k, kernel) | ||
elif operation == "dilate": | ||
output = dilation(image_k, kernel) | ||
elif operation == "open": | ||
output = opening(image_k, kernel) | ||
elif operation == "close": | ||
output = closing(image_k, kernel) | ||
elif operation == "gradient": | ||
output = gradient(image_k, kernel) | ||
elif operation == "top_hat": | ||
output = top_hat(image_k, kernel) | ||
elif operation == "bottom_hat": | ||
output = bottom_hat(image_k, kernel) | ||
else: | ||
raise ValueError(f"Invalid operation {operation} for morphology. Must be one of 'erode', 'dilate', 'open', 'close', 'gradient', 'tophat', 'bottomhat'") | ||
img_out = output.to(comfy.model_management.intermediate_device()).movedim(1, -1) | ||
return (img_out,) | ||
|
||
NODE_CLASS_MAPPINGS = { | ||
"Morphology": Morphology, | ||
} | ||
|
||
NODE_DISPLAY_NAME_MAPPINGS = { | ||
"Morphology": "ImageMorphology", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ Pillow | |
scipy | ||
tqdm | ||
psutil | ||
kornia>=0.7.1 |