Kornia

0.8.2 · active · verified Fri Apr 10

Kornia is an open-source differentiable computer vision library built on PyTorch, providing a rich set of differentiable image processing and geometric vision algorithms. It seamlessly integrates into AI workflows for tasks like image transformations, augmentations, and AI-driven image processing. Currently at version 0.8.2, Kornia is actively maintained with regular updates and is shifting towards end-to-end vision models.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic Kornia usage: creating a dummy image tensor, converting it to grayscale, and applying a Gaussian blur filter. It highlights common imports and tensor operations. For actual image loading, libraries like Pillow or OpenCV are typically used, followed by `K.image_to_tensor`.

import torch
import kornia as K
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

# Simulate loading an image or create a dummy tensor
# In a real scenario, replace this with actual image loading (e.g., with Pillow/OpenCV)
# and conversion using K.image_to_tensor.
# Example with a dummy tensor:
image_tensor = torch.rand(1, 3, 256, 256, dtype=torch.float32)

# Convert image to grayscale
grayscale_tensor = K.color.rgb_to_grayscale(image_tensor)

# Apply a Gaussian blur
blurred_tensor = K.filters.gaussian_blur2d(grayscale_tensor, kernel_size=(7, 7), sigma=(1.5, 1.5))

print(f"Original image shape: {image_tensor.shape}")
print(f"Grayscale image shape: {grayscale_tensor.shape}")
print(f"Blurred image shape: {blurred_tensor.shape}")

# --- Optional: Visualization (requires matplotlib and numpy) ---
# Convert tensors to NumPy arrays for display
# You might need to detach from GPU and move to CPU if applicable:
# grayscale_np = K.tensor_to_image(grayscale_tensor.detach().cpu())
# blurred_np = K.tensor_to_image(blurred_tensor.detach().cpu())

# fig, axs = plt.subplots(1, 2, figsize=(10, 5))
# axs[0].imshow(grayscale_np)
# axs[0].set_title('Grayscale Image')
# axs[0].axis('off')
# axs[1].imshow(blurred_np)
# axs[1].set_title('Blurred Image')
# axs[1].axis('off')
# plt.show()

view raw JSON →