简体   繁体   English

Python 中的 2D 插值

[英]2D interpolation in Python

What is the the equivalent function in Python of MATLAB's interp2 function? MATLAB 的interp2函数在 Python 中的interp2函数是什么?

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

This is the MATLAB code I'm trying to port to python这是我试图移植到 python 的 MATLAB 代码

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.编辑 1:我正在使用 numpy。 In the matlab code img is an image and flowmap is the deformed mesh.在 matlab 代码中 img 是一个图像,flowmap 是变形的网格。 I'm trying to warp the image using flowmap.我正在尝试使用流程图扭曲图像。

EDIT 2: I'm adding the python code translated from matlab.编辑 2:我正在添加从 matlab 翻译的 python 代码。

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.所以我做了一个 UV 映射来纹理网格。 flowmap is this mapping. flowmap 就是这个映射。 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).我有一个稀疏的 uv 映射(例如,网格上的顶点 [0 0] 对应于图像的像素坐标 [0 0],即左上角)。 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.对于每个四边形(4 个顶点),您可以通过解决齐次坐标中的最小二乘问题来计算从这些顶点到纹理图像的变换矩阵。 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.然后对于四边形中的每个像素,我们可以将其坐标与变换矩阵相乘,以找到纹理图像中的像素坐标。

scipy.interpolate.interp2d is what you are looking for. scipy.interpolate.interp2d就是你要找的。 The setup with this function is a little different.使用此功能的设置略有不同。 For one, you'll need to define your original x,y coordinates.一方面,您需要定义原始 x,y 坐标。 Also, depending on what flowmap is exactly, you might have to adjust there (although it looks like it'll fit in just fine).此外,根据具体的flowmap ,您可能需要在那里进行调整(尽管它看起来很适合)。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM