0

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:

Initial 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:

result

Not sure exactly what is the algorithm behind the align_corners parameter, but when setting it to false the diff is larger:

align corners false

I am not sure if this is the expected result, and if it is not, is there anything I should take into account?

Dinari
  • 2,487
  • 13
  • 28
  • do you expect IDENTICAL values? why? what's the error so far? your plots lack a colorbar to assess the magnitudes. – Christoph Rackwitz Aug 22 '23 at 17:35
  • Added colorbars, thanks. I have not expected identical values, but neglectable errors, 1.5% error (with align corners) seems a bit large for me, if it is due to some pixel shift for example, it might have large implications on the image. – Dinari Aug 22 '23 at 17:48
  • 1
    opencv's warpAffine() uses fixed point (integer) math with 5 fractional bits in the coordinates – Christoph Rackwitz Aug 22 '23 at 19:42

0 Answers0