%%capture
!pip install kornia
!pip install kornia-rsImage patch generation
Intermediate
Patches
kornia.geometry
In this tutorial we are going to learn how to generate image patches using
kornia.geometry components.
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:
outfile.write(bytesio.getbuffer())
return filename
download_image("https://github.com/kornia/data/raw/main/homography/img1.ppm")'img1.ppm'
First load libraries and images
%matplotlib inline
import kornia as K
import matplotlib.pyplot as plt
import torchdef imshow(image: torch.tensor, height: int = 10, width: int = 10):
"""Utility function to plot images."""
plt.figure(figsize=(height, width))
plt.imshow(K.tensor_to_image(image))
plt.axis("off")
plt.show()Load and show the original image
torch.manual_seed(0)
timg: torch.Tensor = K.io.load_image("img1.ppm", K.io.ImageLoadType.RGB32)[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 = torch.cat([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 = torch.cat([patch, patch_affine, patch_inv], dim=-1)
imshow(patches_vis, 15, 15)