## Python中的2D插值

python小能手 2019-01-21 11:38:19 678

MATLAB函数的Python中的等价函数是什么interp2？

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

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

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))``````

• python小能手
2019-07-17 23:26:04

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)

0 0
+ 订阅