Open in Colab

Edge Detection

In this tutorial we are going to learn how to detect edges in images with kornia.filters components.

%%capture
!pip install kornia
%%capture
!wget https://github.com/kornia/data/raw/main/doraemon.png
import cv2
from matplotlib import pyplot as plt
import numpy as np

import torch
import torchvision
import kornia as K

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

img_bgr: np.ndarray = cv2.imread('doraemon.png', cv2.IMREAD_COLOR)

Convert the numpy array to torch

x_bgr: torch.Tensor = K.utils.image_to_tensor(img_bgr)  # CxHxWx
x_bgr = x_bgr[None,...].float() / 255.

x_rgb: torch.Tensor = K.color.bgr_to_rgb(x_bgr)
x_gray = K.color.rgb_to_grayscale(x_rgb)
def imshow(input: torch.Tensor):
    out = torchvision.utils.make_grid(input, nrow=2, padding=5)
    out_np: np.ndarray = K.utils.tensor_to_image(out)
    plt.imshow(out_np)
    plt.axis('off')
    plt.show()
imshow(x_gray)
_images/filtering_edges_9_0.png

1st order derivates

grads: torch.Tensor = K.filters.spatial_gradient(x_gray, order=1)  # BxCx2xHxW
grads_x = grads[:, :, 0]
grads_y = grads[:, :, 1]
# Show first derivatives in x
imshow(1. - grads_x.clamp(0., 1.))
_images/filtering_edges_12_0.png
# Show first derivatives in y
imshow(1. - grads_y.clamp(0., 1.))
_images/filtering_edges_13_0.png

2nd order derivatives

grads: torch.Tensor = K.filters.spatial_gradient(x_gray, order=2)  # BxCx2xHxW
grads_x = grads[:, :, 0]
grads_y = grads[:, :, 1]
# Show second derivatives in x
imshow(1. - grads_x.clamp(0., 1.))
_images/filtering_edges_16_0.png
# Show second derivatives in y
imshow(1. - grads_y.clamp(0., 1.))
_images/filtering_edges_17_0.png

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 = K.filters.sobel(x_gray)
imshow(1. - x_sobel)
_images/filtering_edges_19_0.png

Laplacian edges

x_laplacian: torch.Tensor = K.filters.laplacian(x_gray, kernel_size=5)
imshow(1. - x_laplacian.clamp(0., 1.))
_images/filtering_edges_21_0.png

Canny edges

x_laplacian: torch.Tensor = K.filters.canny(x_gray)[0]
imshow(1. - x_laplacian.clamp(0., 1.))
_images/filtering_edges_23_0.png