Equivalent imwarp Matlab's function in python

115 Views Asked by At

I want to apply a perspective transformation to a matrix in python. In particular, I want to have the same effect of the function imwarp in Matlab. According to this reference, the equivalent function is wrap of the library scikit-image. However, applying the same transformation to the same matrix, I got a different result.

In the following, I give you a small example for Matlab:

t = -pi / 32;
P = [
  [cos(t), sin(t), 0]; 
  [-sin(t), cos(t), 0]; 
  [0.001, 0.002, 1.0]
];
tform = projtform2d(P);
X = [
  [1.0, 2.0, 3.0];
  [6.0, 7.0, 13.0];
  [9.0, 2.0, 34.0]
]
I = imwarp(X, tform, "interp", "cubic", "FillValues", 0);
% I = [
%   [0,      0,       0, 0];
%   [0, 4.9524,  5.8901, 0];
%   [0, 4.1178, 12.2952, 0]
%   [0,      0,       0, 0];
% ]

In the following, I give you the "same" code in Python:

import numpy as np
import skimage.transform as transform

def test():
    t = - np.pi / 32
    P = np.array([
        [np.cos(t), np.sin(t), 0],
        [-np.sin(t), np.cos(t), 0],
        [0.001, 0.002, 1.0]
    ], dtype=np.float64)
    X = np.array([
        [1.0, 2.0, 3.0],
        [6.0, 7.0, 13.0],
        [9.0, 2.0, 34.0]
    ], dtype=np.float64)
    P = transform.ProjectiveTransform(matrix=P)
    I = transform.warp(X, P, output_shape=(4, 4), order=3, mode="constant", cval=0, preserve_range=True)
    return I

print(test())
# [[1.000000 2.406609 4.363007 0.063944]
# [5.559468 6.579829 16.993064 1.780567]
# [8.157382 0.940842 27.913211 5.627432]
# [0.129737 0.000000 0.000000 0.000000]]

Why I get two different outcomes? Can I have the same outcome? How?

I thank you in advance for your answers!

0

There are 0 best solutions below