%%capture
!pip install kornia
!pip install kornia-rs
Image Enhancement
Basic
kornia.enhance
In this tutorial we are going to learn how to tweak image properties using the compoments from
kornia.enhance
.
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/ninja_turtles.jpg") download_image(
'ninja_turtles.jpg'
import kornia as K
import numpy as np
import torch
import torchvision
from matplotlib import pyplot as plt
def imshow(input: torch.Tensor):
= torchvision.utils.make_grid(input, nrow=2, padding=5)
out: torch.Tensor = K.utils.tensor_to_image(out)
out_np: np.ndarray
plt.imshow(out_np)"off")
plt.axis( plt.show()
We use Kornia to load an image to memory represented as a tensor
= K.io.load_image("ninja_turtles.jpg", K.io.ImageLoadType.RGB32)[None, ...] x_rgb
Create batch
= x_rgb.expand(4, -1, -1, -1) # 4xCxHxW x_rgb
imshow(x_rgb)
Adjust brightness
= K.enhance.adjust_brightness(x_rgb, torch.linspace(0.2, 0.8, 4))
x_out: torch.Tensor imshow(x_out)
Adjust Contrast
= K.enhance.adjust_contrast(x_rgb, torch.linspace(0.5, 1.0, 4))
x_out: torch.Tensor imshow(x_out)
Adjust Saturation
= K.enhance.adjust_saturation(x_rgb, torch.linspace(0.0, 1.0, 4))
x_out: torch.Tensor imshow(x_out)
Adjust Gamma
= K.enhance.adjust_gamma(x_rgb, torch.tensor([0.2, 0.4, 0.5, 0.6]))
x_out: torch.Tensor imshow(x_out)
Adjust Hue
= K.enhance.adjust_hue(x_rgb, torch.linspace(0.0, 3.14159, 4))
x_out: torch.Tensor imshow(x_out)