%%capture
!pip install kornia
!pip install kornia-rs
Color space conversion
Basic
Color spaces
kornia.color
In this tutorial we are going to learn how to convert image from different color spaces using
kornia.color
.
Explanation
Images are asumed to be loaded either in RGB or Grayscale space.
- We will use OpenCV to load images.
- Convert from BGR to RGB (note that OpenCV loads images in BGR format).
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/simba.png"
url download_image(url)
'simba.png'
import kornia as K
import numpy as np
import torch
import torchvision
from matplotlib import pyplot as plt
# read the image with Kornia
= K.io.load_image("simba.png", K.io.ImageLoadType.RGB32) # CxHxW
img_tensor = K.tensor_to_image(img_tensor)
img_array
"off")
plt.axis(
plt.imshow(img_array) plt.show()
Alternatively we can use use kornia.color
to perform the color transformation.
- Convert the tensor to RGB
- Convert back the tensor to numpy for visualisation.
= img_tensor
x_rgb: torch.Tensor
# to BGR
= K.color.rgb_to_bgr(x_rgb)
x_bgr: torch.Tensor
# convert back to numpy and visualize
= K.tensor_to_image(x_bgr)
img_np: np.array
plt.imshow(img_np)"off"); plt.axis(
Using kornia
we easily perform color transformation in batch mode.
def imshow(input: torch.Tensor):
= torchvision.utils.make_grid(input, nrow=2, padding=5)
out: torch.Tensor = K.tensor_to_image(out)
out_np: np.array
plt.imshow(out_np)"off")
plt.axis(
plt.show()
# create a batch of images
= torch.stack([x_bgr, K.geometry.hflip(x_bgr), K.geometry.vflip(x_bgr), K.geometry.rot180(x_bgr)])
xb_bgr imshow(xb_bgr)
# convert to back to RGB
= K.color.bgr_to_rgb(xb_bgr)
xb_rgb imshow(xb_rgb)
# convert to grayscale
# NOTE: image comes in torch.uint8, and kornia assumes floating point type
= K.color.rgb_to_grayscale(xb_rgb)
xb_gray imshow(xb_gray)
# convert to HSV
= K.color.rgb_to_hsv(xb_rgb)
xb_hsv imshow(xb_hsv)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).