Moving some code from cv2 to kornia to use in torch, and having a discrepancy between the different warp perspective functions.
As an example, I am using the following image:
Using the following code, made sure to keep the flags and params similar:
import cv2
import torch
import kornia
import numpy as np
from matplotlib import pyplot as plt
image = cv2.imread("grid.png", cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image,(200,200))
image = image.astype(np.float32) / 255
angle = 45
center = (image.shape[1] / 2, image.shape[0] / 2)
rot_mat_cv2 = cv2.getRotationMatrix2D(center, angle, 1)
rot_mat_cv2 = np.vstack([rot_mat_cv2, [0, 0, 1]])
warped_cv2 = cv2.warpPerspective(image, rot_mat_cv2, (image.shape[1], image.shape[0]), flags= cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=0)
image_tensor = torch.tensor(image, dtype=torch.float32).unsqueeze(0).unsqueeze(0)
rot_mat_tensor = torch.tensor(rot_mat_cv2, dtype=torch.float32).unsqueeze(0)
warped_kornia = kornia.geometry.transform.warp_perspective(image_tensor,
rot_mat_tensor,
(image.shape[1], image.shape[0]),
mode="bilinear",
align_corners=True,
padding_mode='zeros',
)
warped_kornia_np = (warped_kornia.squeeze(0).squeeze(0).numpy())
difference = np.abs(warped_cv2 - warped_kornia_np)
This is a plot of the result:
Not sure exactly what is the algorithm behind the align_corners
parameter, but when setting it to false the diff is larger:
I am not sure if this is the expected result, and if it is not, is there anything I should take into account?