Color adjustmentΒΆ

In this tutorial we are going to learn how to adjust the color in image batches.

from matplotlib import pyplot as plt
import cv2
import numpy as np

import torch
import kornia
import torchvision

We use OpenCV to load an image to memory represented in a numpy.ndarray

img_bgr: np.ndarray = cv2.imread('./data/ninja_turtles.jpg', cv2.IMREAD_COLOR)

Convert the numpy array to torch

x_bgr: torch.Tensor = kornia.image_to_tensor(img_bgr)
x_rgb: torch.Tensor = kornia.bgr_to_rgb(x_bgr)

Create batch and normalize

x_rgb = x_rgb.expand(4, -1, -1, -1)  # 4xCxHxW
x_rgb = x_rgb.float() / 255.


def imshow(input: torch.Tensor):
    out: torch.Tensor = torchvision.utils.make_grid(input, nrow=2, padding=5)
    out_np: np.ndarray = kornia.tensor_to_image(out)
    plt.imshow(out_np)
    plt.axis('off')

Show original

imshow(x_rgb)
../_images/sphx_glr_color_adjust_001.png

Adjust Brightness

x_brightness: torch.Tensor = kornia.adjust_brightness(x_rgb, 0.6)
imshow(x_brightness)
../_images/sphx_glr_color_adjust_002.png

Adjust Contrast

x_contrast: torch.Tensor = kornia.adjust_contrast(x_rgb, 0.2)
imshow(x_contrast)
../_images/sphx_glr_color_adjust_003.png

Adjust Gamma

x_gamma: torch.Tensor = kornia.adjust_gamma(x_rgb, gamma=3., gain=1.5)
imshow(x_gamma)
../_images/sphx_glr_color_adjust_004.png

Adjust Saturation

x_saturated: torch.Tensor = kornia.adjust_saturation(x_rgb, 0.2)
imshow(x_saturated)
../_images/sphx_glr_color_adjust_005.png

Adjust Hue

x_hue: torch.Tensor = kornia.adjust_hue(x_rgb, 0.5)
imshow(x_hue)
../_images/sphx_glr_color_adjust_006.png

Total running time of the script: ( 0 minutes 3.931 seconds)

Gallery generated by Sphinx-Gallery