1
votes

What is the the equivalent function in Python of MATLAB's interp2 function?

VqR = interp2(rR, fx(:), fy(:));

This is the MATLAB code I'm trying to port to python

function res = imgMeshWarp( img, flowmap )
img = im2double(img);
rR = img(:, :, 1);
rG = img(:, :, 2);
rB = img(:, :, 3);
fx = flowmap(:, :, 1); fy = flowmap(:, :, 2);
VqR = interp2(rR, fx(:), fy(:));
VqG = interp2(rG, fx(:), fy(:));
VqB = interp2(rB, fx(:), fy(:));
res = cat(3, VqR, VqG, VqB);
res = reshape(res, size(flowmap, 1), size(flowmap, 2), []);    
end

EDIT 1: I'm using numpy. In the matlab code img is an image and flowmap is the deformed mesh. I'm trying to warp the image using flowmap.

EDIT 2: I'm adding the python code translated from matlab.

def image_warp(img, fm):
    img = img[:,:, ::-1]
    rR = img[:, :, 0]
    rG = img[:, :, 1]
    rB = img[:, :, 2]

    fx = fm[:, :, 0]
    fy = fm[:, :, 1]

    VqR = scipy.ndimage.map_coordinates(rR, [fx.ravel(), fy.ravel()], order=1, mode='constant', cval=np.nan).reshape(rR.shape)
    VqG = scipy.ndimage.map_coordinates(rG, [fx.ravel(), fy.ravel()], order=1, mode='constant', cval=np.nan).reshape(rG.shape)
    VqB = scipy.ndimage.map_coordinates(rB, [fx.ravel(), fy.ravel()], order=1, mode='constant', cval=np.nan).reshape(rB.shape)

    res = np.dstack((VqR, VqG, VqB))
    res = np.reshape(res, (fm.shape[0], fm.shape[1], -1))

My problem is to deform an image according to a randomly deformed deformation mesh. So I did a UV mapping to texture the mesh. flowmap is this mapping. It is generated in the following way. I have a sparse uv mapping (for example, vertex [0 0] on the mesh corresponds to pixel coordinates [0 0] of the image, which is top left corner). Then the question is how to figure out all the other pixels within the mesh. For each quad (4 vertices), you can compute a transformation matrix from these vertices to the texture image by solving a least square problem in homogeneous coordinates. Then for each pixel within the quad, we can multiply its coordinates with the transformation matrix to find the pixel coordinates in the texture image.

1
Which python version is this referring to? Are you using numpy? I suggest you provide a small sample of inputs and outputs in addition to mentioning the function you want to use.Dev-iL
@Dev-iL I've added an edit.user7243382
The remaining python code is working as it should in matlab , the only erroneous part is this function, I still can't figure out accurate translation for this function in python.user7243382
Can someone help, please.user7243382

1 Answers

1
votes

scipy.interpolate.interp2d is what you are looking for. The setup with this function is a little different. For one, you'll need to define your original x,y coordinates. Also, depending on what flowmap is exactly, you might have to adjust there (although it looks like it'll fit in just fine). Might look something like this for one of your color channels:

from scipy import interpolate

dy, dx = rR.shape
f = interpolate.interp2d(np.arange(dx), np.arange(dy), rR)
VqR = f(new_x, new_y)