%%capture
!pip install kornia
!pip install kornia-rs
Filtering Operators
Basic
Filters
Blur
kornia.filters
In this tutorial we are going to learn how to apply blurring filters to images with
kornia.filters
components.
import io
import requests
def download_image(url: str, filename: str = "") -> str:
= url.split("/")[-1] if len(filename) == 0 else filename
filename # Download
= io.BytesIO(requests.get(url).content)
bytesio # Save file
with open(filename, "wb") as outfile:
outfile.write(bytesio.getbuffer())
return filename
= "https://github.com/kornia/data/raw/main/drslump.jpg"
url download_image(url)
'drslump.jpg'
import kornia as K
import torch
import torchvision
from matplotlib import pyplot as plt
We use Kornia to load an image to memory represented directly in a tensor
= K.io.load_image("doraemon.png", K.io.ImageLoadType.RGB32)[None, ...] # BxCxHxW
x_rgb: torch.Tensor
= K.color.rgb_to_grayscale(x_rgb) x_gray
def imshow(input: torch.Tensor):
if input.shape != x_rgb.shape:
input = K.geometry.resize(input, size=(x_rgb.shape[-2:]))
= torch.cat([x_rgb, input], dim=-1)
out = torchvision.utils.make_grid(out, nrow=2, padding=5)
out = K.utils.tensor_to_image(out)
out_np
plt.imshow(out_np)"off")
plt.axis( plt.show()
imshow(x_rgb)
Box Blur
= K.filters.box_blur(x_rgb, (9, 9))
x_blur: torch.Tensor imshow(x_blur)
Blur Pool
= K.filters.blur_pool2d(x_rgb, kernel_size=9)
x_blur: torch.Tensor imshow(x_blur)
Gaussian Blur
= K.filters.gaussian_blur2d(x_rgb, (11, 11), (11.0, 11.0))
x_blur: torch.Tensor imshow(x_blur)
Max Pool
= K.filters.max_blur_pool2d(x_rgb, kernel_size=11)
x_blur: torch.Tensor imshow(x_blur)
Median Blur
= K.filters.median_blur(x_rgb, (5, 5))
x_blur: torch.Tensor imshow(x_blur)
Motion Blur
= K.filters.motion_blur(x_rgb, 9, 90.0, 1)
x_blur: torch.Tensor imshow(x_blur)