Compute filter edgesΒΆ

In this tutorial we are going to learn how to compute the first order and second order derivatives of an image using 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/doraemon.png', 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_gray = kornia.rgb_to_grayscale(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)
    plt.imshow(out_np)
    plt.axis('off')

Show original

imshow(x_rgb)
../_images/sphx_glr_filter_edges_001.png

Out:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

Compute the 1st order derivates

grads: torch.Tensor = kornia.spatial_gradient(x_gray, order=1)  # BxCx2xHxW
grads_x = grads[:, :, 0]
grads_y = grads[:, :, 1]

Show first derivatives in x

imshow(grads_x)
../_images/sphx_glr_filter_edges_002.png

Out:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

Show first derivatives in y

imshow(grads_y)
../_images/sphx_glr_filter_edges_003.png

Out:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

Sobel Edges Once with the gradients in the two directions we can computet the Sobel edges. However, in kornia we already have it implemented.

x_sobel: torch.Tensor = kornia.sobel(x_gray)
imshow(x_sobel)
../_images/sphx_glr_filter_edges_004.png

Compute the 2nd order derivates

grads: torch.Tensor = kornia.spatial_gradient(x_gray, order=2)  # BxCx2xHxW
grads_x = grads[:, :, 0]
grads_y = grads[:, :, 1]

Show second derivatives in x

imshow(grads_x)
../_images/sphx_glr_filter_edges_005.png

Out:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

Show second derivatives in y

imshow(grads_y)
../_images/sphx_glr_filter_edges_006.png

Out:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

Comute Laplacian Edges

x_laplacian: torch.Tensor = kornia.laplacian(x_gray, kernel_size=5)
imshow(x_laplacian)
../_images/sphx_glr_filter_edges_007.png

Out:

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

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

Gallery generated by Sphinx-Gallery