[英]2D interpolation in Python
MATLAB 的interp2
函数在 Python 中的interp2
函数是什么?
VqR = interp2(rR, fx(:), fy(:));
这是我试图移植到 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
编辑 1:我正在使用 numpy。 在 matlab 代码中 img 是一个图像,flowmap 是变形的网格。 我正在尝试使用流程图扭曲图像。
编辑 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))
我的问题是根据随机变形的变形网格对图像进行变形。 所以我做了一个 UV 映射来纹理网格。 flowmap 就是这个映射。 它是通过以下方式生成的。 我有一个稀疏的 uv 映射(例如,网格上的顶点 [0 0] 对应于图像的像素坐标 [0 0],即左上角)。 那么问题是如何找出网格内的所有其他像素。 对于每个四边形(4 个顶点),您可以通过解决齐次坐标中的最小二乘问题来计算从这些顶点到纹理图像的变换矩阵。 然后对于四边形中的每个像素,我们可以将其坐标与变换矩阵相乘,以找到纹理图像中的像素坐标。
scipy.interpolate.interp2d
就是你要找的。 使用此功能的设置略有不同。 一方面,您需要定义原始 x,y 坐标。 此外,根据具体的flowmap
,您可能需要在那里进行调整(尽管它看起来很适合)。 对于您的颜色通道之一,可能看起来像这样:
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.