简体   繁体   English

Python - 在给定边界内插网格

[英]Python - Interpolating a grid in given border

For a project I want a grid like this: 5x5.对于一个项目,我想要这样的网格:5x5。 The points should be movable later but I got that i guess.这些点以后应该可以移动,但我猜我明白了。

标准网格

What i wanna be able to do now is to interpolate for example 100x50 points in this grid of 5x5 marker points but not just linear, CUBIC in both axis.我现在想做的是在这个 5x5 标记点的网格中插入例如 100x50 点,但不仅仅是线性的,两个轴上的 CUBIC。 I cant wrap my head around it.我无法理解它。 I saw how to lay scipy.interpolate.CubicSpline through for example the 5 horizontal markers at the top but how do i combine it with the vertical warp?我看到了如何通过顶部的 5 个水平标记放置 scipy.interpolate.CubicSpline,但我如何将它与垂直扭曲结合起来?

is there a fnc to interpolate a grid in a given frame like this?是否有 fnc 可以像这样在给定帧中插入网格?

Use scipy.interpolate.interp2d :使用scipy.interpolate.interp2d

Interpolate over a 2-D grid.在二维网格上进行插值。 x, y and z are arrays of values used to approximate some function f: z = f(x, y) which returns a scalar value z. x、y 和 z 是 arrays 个值,用于近似某些 function f:z = f(x, y) 返回标量值 z。 This class returns a function whose call method uses spline interpolation to find the value of new points.这个 class 返回一个 function 其调用方法使用样条插值来查找新点的值。

So you have an array orig that you want to generate 100x50 array res using bicubic interpolation所以你有一个数组orig ,你想使用双三次插值生成 100x50 数组res

# Adapted from https://stackoverflow.com/a/58126099/17595968
from scipy import interpolate as interp
import numpy as np

orig = np.random.randint(0, 100, 25).reshape((5, 5))

W, H = orig.shape
new_W, new_H = (100, 50)
map_range = lambda x: np.linspace(0, 1, x)

f = interp.interp2d(map_range(W), map_range(H), orig, kind="cubic")
res = f(range(new_W), range(new_H))

Edit:编辑:

If what you want is coordinates of 100x50 grid in 5x5 grid you can use numpy.meshgrid :如果你想要的是 5x5 网格中 100x50 网格的坐标,你可以使用numpy.meshgrid

#From https://stackoverflow.com/a/32208788/17595968
import numpy as np

W, H = 5, 5
new_W, new_H = 100, 50

x_step = W / new_W
y_step = H / new_H
xy = np.mgrid[0:H:y_step, 0:W:x_step].reshape(2, -1).T
xy = xy.reshape(new_H, new_W, 2)
xy

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

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