Image patch generation

In this tutorial we are going to learn how to generate image patches using kornia.geometry components.

Edgar Riba


August 28, 2021

Open in google colab

!pip install kornia
!pip install kornia-rs
import io

import requests

def download_image(url: str, filename: str = "") -> str:
    filename = url.split("/")[-1] if len(filename) == 0 else filename
    # Download
    bytesio = io.BytesIO(requests.get(url).content)
    # Save file
    with open(filename, "wb") as outfile:

    return filename


First load libraries and images

%matplotlib inline
import kornia as K
import matplotlib.pyplot as plt
import torch
def imshow(image: torch.tensor, height: int = 10, width: int = 10):
    """Utility function to plot images."""
    plt.figure(figsize=(height, width))

Load and show the original image


timg: torch.Tensor ="img1.ppm",[None, ...]  # BxCxHxW

imshow(timg, 10, 10)

In the following section we are going to take the original image and generate random crops of a given size.

random_crop = K.augmentation.RandomCrop((64, 64))

patch =[random_crop(timg) for _ in range(15)], dim=-1)

imshow(patch[0], 22, 22)

Next, we will show how to crop patches and apply forth and back random geometric transformations.

# transform a patch

random_crop = K.augmentation.RandomCrop((64, 64))
random_affine = K.augmentation.RandomAffine([-15, 15], [0.0, 0.25])

# crop
patch = random_crop(timg)

# transform and retrieve transformation
patch_affine = random_affine(patch)
transformation = random_affine.get_transformation_matrix(patch)

# invert patch
_, _, H, W = patch.shape
patch_inv = K.geometry.warp_perspective(patch_affine, torch.inverse(transformation), (H, W))

# visualise - (original, transformed, reconstructed)
patches_vis =[patch, patch_affine, patch_inv], dim=-1)
imshow(patches_vis, 15, 15)