<jupyter_start><jupyter_text>Sharpen image using unsharp mask We first install kornia<jupyter_code>%%capture
%matplotlib inline
!pip install kornia
import kornia
kornia.__version__
import cv2
import numpy as np
import torch
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Downloading the example image.<jupyter_code>!wget -q https://github.com/kornia/data/raw/main/squirrel.jpg
# Read the image with OpenCV
img: np.ndarray = cv2.imread('squirrel.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#convert image to tensor
data: torch.tensor = kornia.utils.image_to_tensor(img, keepdim=False)
data = data.float() / 255.<jupyter_output><empty_output><jupyter_text>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!<jupyter_code>sharpen = kornia.filters.UnsharpMask((9,9), (2.5,2.5))
sharpened_tensor = sharpen(data)
difference = (sharpened_tensor - data).abs()
# Converting the sharpened tensor to image
sharpened_image = kornia.utils.tensor_to_image(sharpened_tensor) 
difference_image = kornia.utils.tensor_to_image(difference)<jupyter_output><empty_output><jupyter_text>So, let us understand how we arrived till here.1. In the unsharp mask technique, first a gaussian blur is applied to the data.2. Then the blur is subtracted from the orignal data.3. The resultant is added to the origanl data.4. So, what do we get? Sharpened data!<jupyter_code># To display the input image, sharpened image and the difference image
fig, axs = plt.subplots(1, 3, figsize=(16, 10))
axs = axs.ravel()

axs[0].axis('off')
axs[0].set_title('image source')
axs[0].imshow(img)

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)
plt.show()<jupyter_output>Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
{'fraction_non_alphanumeric': 0.11225490196078432, 'fraction_numerical': 0.01911764705882353, 'mean_word_length': 6.368231046931408, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '125430', 'license': 'Apache-2.0', 'n_tokens_mistral': 753, 'n_tokens_neox': 712, 'n_words': 195, 'path': 'source/unsharp_mask.ipynb', 'repo_name': 'oskarflordal/tutorials', 'size': '2040'}