%%capture
%matplotlib inline
!pip install kornia
!pip install kornia-rs
Sharpen image using unsharp mask
Basic
Filters
kornia.filters
In this tutorial we are going to learn how to use the unsharp mask
We first install kornia
import kornia
import matplotlib.pyplot as plt
kornia.__version__
'0.6.12'
Downloading 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/squirrel.jpg"
url download_image(url)
'squirrel.jpg'
# Read the image with Kornia
= kornia.io.load_image("squirrel.jpg", kornia.io.ImageLoadType.RGB32)[None, ...] # BxCxHxW data
We create Unsharp Mask filter object and apply it to data. The unsharp mask filter is initialized with the format kornia.filters.UnsharpMask(kernel_size, sigma)
. You can tune these parametres and experiment!
= kornia.filters.UnsharpMask((9, 9), (2.5, 2.5))
sharpen = sharpen(data)
sharpened_tensor = (sharpened_tensor - data).abs() difference
# Converting the sharpened tensor to image
= kornia.utils.tensor_to_image(sharpened_tensor)
sharpened_image = kornia.utils.tensor_to_image(difference) difference_image
So, let us understand how we arrived till here.
- In the unsharp mask technique, first a gaussian blur is applied to the data.
- Then the blur is subtracted from the orignal data.
- The resultant is added to the origanl data.
- So, what do we get? Sharpened data!
# To display the input image, sharpened image and the difference image
= plt.subplots(1, 3, figsize=(16, 10))
fig, axs = axs.ravel()
axs
0].axis("off")
axs[0].set_title("image source")
axs[0].imshow(kornia.tensor_to_image(data))
axs[
1].axis("off")
axs[1].set_title("sharpened")
axs[1].imshow(sharpened_image)
axs[
2].axis("off")
axs[2].set_title("difference")
axs[2].imshow(difference_image)
axs[ plt.show()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).