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.