Filtering OperationsΒΆ

In this tutorial you are going to learn how to use the different filtering operations found in kornia.filters.

from matplotlib import pyplot as plt
import cv2
import numpy as np

import torch
import kornia
import torchvision

We use OpenCV to load an image to memory represented in a numpy.ndarray

img_bgr: np.ndarray = cv2.imread('./data/drslump.jpg', cv2.IMREAD_COLOR)

Convert the numpy array to torch

x_bgr: torch.Tensor = kornia.image_to_tensor(img_bgr)
x_rgb: torch.Tensor = kornia.bgr_to_rgb(x_bgr)

Create batch and normalize

x_rgb = x_rgb.expand(2, -1, -1, -1)  # 4xCxHxW
x_rgb = x_rgb.float() / 255.

def imshow(input: torch.Tensor):
    out: torch.Tensor = torchvision.utils.make_grid(input, nrow=2, padding=1)
    out_np: np.ndarray = kornia.tensor_to_image(out)

Show original


Box Blur

x_blur: torch.Tensor = kornia.box_blur(x_rgb, (9, 9))

Median Blur

x_blur: torch.Tensor = kornia.median_blur(x_rgb, (5, 5))

Gaussian Blur

x_blur: torch.Tensor = kornia.gaussian_blur2d(x_rgb, (11, 11), (11., 11.))

Total running time of the script: ( 0 minutes 6.126 seconds)

Gallery generated by Sphinx-Gallery