adds PencilSketch effect, mimics a drawn sketch

also extracts out gaussian_kernel func
This commit is contained in:
EllangoK
2023-04-03 17:01:00 -04:00
parent 99956683a0
commit 038a36d25e
5 changed files with 209 additions and 30 deletions
+8 -8
View File
@@ -36,17 +36,11 @@ class Glow:
glowing_image = torch.clamp(glowing_image, 0, 1)
return (glowing_image,)
def gaussian_kernel(self, kernel_size: int):
sigma = (kernel_size - 1) / 6
x, y = torch.meshgrid(torch.linspace(-1, 1, kernel_size), torch.linspace(-1, 1, kernel_size))
d = torch.sqrt(x * x + y * y)
g = torch.exp(-(d * d) / (2.0 * sigma * sigma))
return g / g.sum()
def gaussian_blur(self, image: torch.Tensor, kernel_size: int):
batch_size, height, width, channels = image.shape
kernel = self.gaussian_kernel(kernel_size).repeat(channels, 1, 1).unsqueeze(1)
sigma = (kernel_size - 1) / 6
kernel = gaussian_kernel(kernel_size, sigma).repeat(channels, 1, 1).unsqueeze(1)
image = image.permute(0, 3, 1, 2) # Torch wants (B, C, H, W) we use (B, H, W, C)
blurred = F.conv2d(image, kernel, padding=kernel_size // 2, groups=channels)
@@ -60,3 +54,9 @@ class Glow:
NODE_CLASS_MAPPINGS = {
"Glow": Glow,
}
def gaussian_kernel(kernel_size: int, sigma: float):
x, y = torch.meshgrid(torch.linspace(-1, 1, kernel_size), torch.linspace(-1, 1, kernel_size), indexing="ij")
d = torch.sqrt(x * x + y * y)
g = torch.exp(-(d * d) / (2.0 * sigma * sigma))
return g / g.sum()