%%capture
%matplotlib inline
!pip install kornia
!pip install kornia-rs
Obtaining Edges using the Canny operator
Basic
Edge Detection
kornia.filters
In this tutorial we show how easily one can apply the typical canny edge detection using Kornia
Preparation
We first install Kornia.
import kornia
kornia.__version__
'0.6.12-dev'
Now we download the example image.
import io
import requests
def download_image(url: str, filename: str = "") -> str:
= url.split("/")[-1] if len(filename) == 0 else filename
filename # Download
= io.BytesIO(requests.get(url).content)
bytesio # Save file
with open(filename, "wb") as outfile:
outfile.write(bytesio.getbuffer())
return filename
= "https://github.com/kornia/data/raw/main/paranoia_agent.jpg"
url download_image(url)
'paranoia_agent.jpg'
Example
We first import the required libraries and load the data.
import kornia
import matplotlib.pyplot as plt
import torch
# read the image with Kornia
= kornia.io.load_image("paranoia_agent.jpg", kornia.io.ImageLoadType.RGB32)[None, ...] # BxCxHxW
img_tensor = kornia.tensor_to_image(img_tensor)
img_array
"off")
plt.axis(
plt.imshow(img_array) plt.show()
To apply a filter, we create the Canny operator object and apply it to the data. It will provide the magnitudes as well as the edges after the hysteresis process. Note that the edges are a binary image which is not differentiable!
# create the operator
= kornia.filters.Canny()
canny
# blur the image
= canny(img_tensor) x_magnitude, x_canny
That’s it! We can compare the source image and the results from the magnitude as well as the edges:
# convert back to numpy
= kornia.tensor_to_image(x_magnitude.byte())
img_magnitude = kornia.tensor_to_image(x_canny.byte())
img_canny
# Create the plot
= plt.subplots(1, 3, figsize=(16, 16))
fig, axs = axs.ravel()
axs
0].axis("off")
axs[0].set_title("image source")
axs[0].imshow(img_array)
axs[
1].axis("off")
axs[1].set_title("canny magnitude")
axs[1].imshow(img_magnitude, cmap="Greys")
axs[
2].axis("off")
axs[2].set_title("canny edges")
axs[2].imshow(img_canny, cmap="Greys")
axs[
plt.show()
Note that our final result still recovers some edges whose magnitude is quite low. Let us increase the thresholds and compare the final edges.
# create the operator
= kornia.filters.Canny(low_threshold=0.4, high_threshold=0.5)
canny
# blur the image
= canny(img_tensor) _, x_canny_threshold
import torch.nn.functional as F
# convert back to numpy
= kornia.tensor_to_image(x_canny_threshold.byte())
img_canny_threshold
# Create the plot
= plt.subplots(1, 3, figsize=(16, 16))
fig, axs = axs.ravel()
axs
0].axis("off")
axs[0].set_title("image source")
axs[0].imshow(img_array)
axs[
1].axis("off")
axs[1].set_title("canny default")
axs[1].imshow(img_canny, cmap="Greys")
axs[
2].axis("off")
axs[2].set_title("canny defined thresholds")
axs[2].imshow(img_canny_threshold, cmap="Greys")
axs[
plt.show()