Edge Detection

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

Edgar Riba

Published

July 6, 2021

Open in google colab

Open in HF Spaces

%%capture
!pip install kornia
!pip install kornia-rs
import io

import requests


def download_image(url: str, filename: str = "") -> str:
    filename = url.split("/")[-1] if len(filename) == 0 else filename
    # Download
    bytesio = io.BytesIO(requests.get(url).content)
    # Save file
    with open(filename, "wb") as outfile:
        outfile.write(bytesio.getbuffer())

    return filename


url = "https://github.com/kornia/data/raw/main/doraemon.png"
download_image(url)
'doraemon.png'
import cv2
import kornia as K
import numpy as np
import torch
import torchvision
from matplotlib import pyplot as plt

We use Kornia to load an image to memory represented in a torch.tensor

x_rgb: torch.Tensor = K.io.load_image("doraemon.png", K.io.ImageLoadType.RGB32)[None, ...]  # BxCxHxW

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)

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.0 - grads_x.clamp(0.0, 1.0))

# Show first derivatives in y
imshow(1.0 - grads_y.clamp(0.0, 1.0))

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.0 - grads_x.clamp(0.0, 1.0))

# Show second derivatives in y
imshow(1.0 - grads_y.clamp(0.0, 1.0))

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.0 - x_sobel)

Laplacian edges

x_laplacian: torch.Tensor = K.filters.laplacian(x_gray, kernel_size=5)
imshow(1.0 - x_laplacian.clamp(0.0, 1.0))

Canny edges

x_laplacian: torch.Tensor = K.filters.canny(x_gray)[0]
imshow(1.0 - x_laplacian.clamp(0.0, 1.0))