From 1655b8d475f3fc23357ddd66464c1d2e3a36b12d Mon Sep 17 00:00:00 2001 From: pk Date: Sun, 8 Jun 2025 09:40:05 +0200 Subject: [PATCH] add more AQnodes --- nodes/filters/AQ_filters.py | 71 ++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/nodes/filters/AQ_filters.py b/nodes/filters/AQ_filters.py index 535120f..17f9dbe 100644 --- a/nodes/filters/AQ_filters.py +++ b/nodes/filters/AQ_filters.py @@ -1,5 +1,5 @@ import torch - +import cv2 class AQ_BatchAverageImage: @classmethod @@ -82,6 +82,75 @@ class AQ_ColorMatchImage: t[i, :, :, c] /= blurred[i, :, :, c] t[i, :, :, c] *= blurred_ref[i, :, :, c] + t = t - 0.1 torch.clamp(torch.lerp(images, t, factor), 0, 1) return (t,) +# guided filter a tensor image batch in format [B x H x W x C] on H/W (spatial, per-image, per-channel) +def guided_filter_tensor(ref, images, d, s): + try: + from cv2.ximgproc import guidedFilter + except ImportError: + print("\033[33mUnable to import guidedFilter, make sure you have only opencv-contrib-python or run the import_error_install.bat script\033[m") + + if d > 100: + np_img = ( + torch.nn.functional.interpolate( + images.detach().clone().movedim(-1, 1), + scale_factor=0.1, + mode="bilinear", + ) + .movedim(1, -1) + .cpu() + .numpy() + ) + np_ref = ( + torch.nn.functional.interpolate( + ref.detach().clone().movedim(-1, 1), scale_factor=0.1, mode="bilinear" + ) + .movedim(1, -1) + .cpu() + .numpy() + ) + for index, image in enumerate(np_img): + np_img[index] = guidedFilter(np_ref[index], image, d // 20 * 2 + 1, s) + return torch.nn.functional.interpolate( + torch.from_numpy(np_img).movedim(-1, 1), + size=(images.shape[1], images.shape[2]), + mode="bilinear", + ).movedim(1, -1) + else: + np_img = images.detach().clone().cpu().numpy() + np_ref = ref.cpu().numpy() + for index, image in enumerate(np_img): + np_img[index] = guidedFilter(np_ref[index], image, d, s) + return torch.from_numpy(np_img) + + +# gaussian blur a tensor image batch in format [B x H x W x C] on H/W (spatial, per-image, per-channel) +def cv_blur_tensor(images, dx, dy): + if min(dx, dy) > 100: + np_img = ( + torch.nn.functional.interpolate( + images.detach().clone().movedim(-1, 1), + scale_factor=0.1, + mode="bilinear", + ) + .movedim(1, -1) + .cpu() + .numpy() + ) + for index, image in enumerate(np_img): + np_img[index] = cv2.GaussianBlur( + image, (dx // 20 * 2 + 1, dy // 20 * 2 + 1), 0 + ) + return torch.nn.functional.interpolate( + torch.from_numpy(np_img).movedim(-1, 1), + size=(images.shape[1], images.shape[2]), + mode="bilinear", + ).movedim(1, -1) + else: + np_img = images.detach().clone().cpu().numpy() + for index, image in enumerate(np_img): + np_img[index] = cv2.GaussianBlur(image, (dx, dy), 0) + return torch.from_numpy(np_img)