Image Registration¶
Image registration is the process of transforming different sets of data into one coordinate system. Data may be multiple photographs, data from different sensors, times, depths, or viewpoints. It is used in computer vision, medical imaging, and compiling and analyzing images and data from satellites. Registration is necessary in order to be able to compare or integrate the data obtained from these different measurements.
Learn more: https://paperswithcode.com/task/image-registration
We provide the ImageRegistrator
API from which you can leverage to align automatically two images by a process of direct optimisation using the PyTorch Autograd differentiability.
from kornia.geometry import ImageRegistrator
img_src = torch.rand(1, 1, 32, 32)
img_dst = torch.rand(1, 1, 32, 32)
registrator = ImageRegistrator('similarity')
homo = registrator.register(img_src, img_dst)
Then, if you want to perform a more sophisticated process:
import os
import cv2
import imageio
import torch
import kornia as K
import kornia.geometry as KG
def load_timg(file_name):
"""Loads the image with OpenCV and converts to torch.Tensor."""
assert os.path.isfile(file_name), f"Invalid file {file_name}" # nosec
# load image with OpenCV
img = cv2.imread(file_name, cv2.IMREAD_COLOR)
# convert image to torch tensor
tensor = K.image_to_tensor(img, None).float() / 255.0
return K.color.bgr_to_rgb(tensor)
registrator = KG.ImageRegistrator("similarity")
img1 = K.resize(load_timg("/Users/oldufo/datasets/stewart/MR-CT/CT.png"), (400, 600))
img2 = K.resize(load_timg("/Users/oldufo/datasets/stewart/MR-CT/MR.png"), (400, 600))
model, intermediate = registrator.register(img1, img2, output_intermediate_models=True)
video_writer = imageio.get_writer("medical_registration.gif", fps=2)
timg_dst_first = img1.clone()
timg_dst_first[0, 0, :, :] = img2[0, 0, :, :]
video_writer.append_data(K.tensor_to_image((timg_dst_first * 255.0).byte()))
with torch.no_grad():
for m in intermediate:
timg_dst = KG.homography_warp(img1, m, img2.shape[-2:])
timg_dst[0, 0, :, :] = img2[0, 0, :, :]
video_writer.append_data(K.tensor_to_image((timg_dst_first * 255.0).byte()))
video_writer.close()
To reproduce the same results as in the showed video you can go through or full tutorial using Colab found here .
Interactive Demo¶
Visit the image registration demo on the Hugging Face Spaces.