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
/home/docs/checkouts/readthedocs.org/user_builds/kornia-tutorials/envs/latest/lib/python3.7/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm

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/536848675d046c37486f52f944b9f3efe16f472953b7aa15353bfa23d7acacdb.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/c4674803de24e625de1f1add31670f4a296eb8b593ff2bf324f223519fcdd145.png
# Show first derivatives in y
imshow(1. - grads_y.clamp(0., 1.))
_images/30273ea01ebcbeec92130cc0532ceef7ac48cfc6a003b2b521e882157b1920e3.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/34637c080369c4114ef8398b29577645c8f23eef16b685184b8f2ba0a29cdedf.png
# Show second derivatives in y
imshow(1. - grads_y.clamp(0., 1.))
_images/53570eedeeddd28090ab02d041565959b6c6392b60b87575b813f1ae1a23c900.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/27031781f7af5250aa917a79ecbffd213979d435acd1031f23baf9705dad8419.png

Laplacian edges#

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

Canny edges#

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