|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | +import multiprocessing as mp |
| 4 | +import torch |
| 5 | + |
| 6 | +class KuwaharaBlur: |
| 7 | + def __init__(self): |
| 8 | + pass |
| 9 | + |
| 10 | + @classmethod |
| 11 | + def INPUT_TYPES(s): |
| 12 | + return { |
| 13 | + "required": { |
| 14 | + "image": ("IMAGE",), |
| 15 | + "blur_radius": ("INT", { |
| 16 | + "default": 3, |
| 17 | + "min": 0, |
| 18 | + "max": 31, |
| 19 | + "step": 1 |
| 20 | + }), |
| 21 | + "method": (["mean", "gaussian"],), |
| 22 | + }, |
| 23 | + } |
| 24 | + |
| 25 | + RETURN_TYPES = ("IMAGE",) |
| 26 | + FUNCTION = "apply_kuwahara_filter" |
| 27 | + |
| 28 | + CATEGORY = "postprocessing" |
| 29 | + |
| 30 | + def apply_kuwahara_filter(self, image: np.ndarray, blur_radius: int, method: str): |
| 31 | + if blur_radius == 0: |
| 32 | + return (image,) |
| 33 | + |
| 34 | + out = torch.zeros_like(image) |
| 35 | + batch_size, height, width, channels = image.shape |
| 36 | + |
| 37 | + for b in range(batch_size): |
| 38 | + image = image[b].cpu().numpy() * 255.0 |
| 39 | + image = image.astype(np.uint8) |
| 40 | + |
| 41 | + out[b] = torch.from_numpy(kuwahara(image, method=method, radius=blur_radius)) / 255.0 |
| 42 | + |
| 43 | + return (out,) |
| 44 | + |
| 45 | +def kuwahara(orig_img, method="mean", radius=3, sigma=None): |
| 46 | + if method == "gaussian" and sigma is None: |
| 47 | + sigma = -1 |
| 48 | + |
| 49 | + image = orig_img.astype(np.float32, copy=False) |
| 50 | + avgs = np.empty((4, *image.shape), dtype=image.dtype) |
| 51 | + stddevs = np.empty((4, *image.shape[:2]), dtype=image.dtype) |
| 52 | + image_2d = cv2.cvtColor(orig_img, cv2.COLOR_BGR2GRAY).astype(image.dtype, copy=False) |
| 53 | + avgs_2d = np.empty((4, *image.shape[:2]), dtype=image.dtype) |
| 54 | + |
| 55 | + squared_img = image_2d ** 2 |
| 56 | + |
| 57 | + if method == "mean": |
| 58 | + kxy = np.ones(radius + 1, dtype=image.dtype) / (radius + 1) |
| 59 | + elif method == "gaussian": |
| 60 | + kxy = cv2.getGaussianKernel(2 * radius + 1, sigma, ktype=cv2.CV_32F) |
| 61 | + kxy /= kxy[radius:].sum() |
| 62 | + klr = np.array([kxy[:radius+1], kxy[radius:]]) |
| 63 | + kindexes = [[1, 1], [1, 0], [0, 1], [0, 0]] |
| 64 | + |
| 65 | + shift = [(0, 0), (0, radius), (radius, 0), (radius, radius)] |
| 66 | + |
| 67 | + for k in range(4): |
| 68 | + if method == "mean": |
| 69 | + kx, ky = kxy, kxy |
| 70 | + else: |
| 71 | + kx, ky = klr[kindexes[k]] |
| 72 | + cv2.sepFilter2D(image, -1, kx, ky, avgs[k], shift[k]) |
| 73 | + cv2.sepFilter2D(image_2d, -1, kx, ky, avgs_2d[k], shift[k]) |
| 74 | + cv2.sepFilter2D(squared_img, -1, kx, ky, stddevs[k], shift[k]) |
| 75 | + stddevs[k] = stddevs[k] - avgs_2d[k] ** 2 |
| 76 | + |
| 77 | + indices = np.argmin(stddevs, axis=0) |
| 78 | + filtered = np.take_along_axis(avgs, indices[None,...,None], 0).reshape(image.shape) |
| 79 | + |
| 80 | + return filtered.astype(orig_img.dtype) |
| 81 | + |
| 82 | +NODE_CLASS_MAPPINGS = { |
| 83 | + "KuwaharaBlur": KuwaharaBlur |
| 84 | +} |
0 commit comments