Python-在给定边界内插网格

发布于 2025-01-12 09:47:09 字数 357 浏览 1 评论 0原文

对于一个项目,我想要一个像这样的网格:5x5。这些点稍后应该可以移动,但我猜我明白了。

标准网格

我现在想要做的是在这个 5x5 标记点网格中插入例如 100x50 点,但不仅仅是线性的,在两个轴上都是三次的。我无法理解它。我看到了如何通过顶部的 5 个水平标记来放置 scipy.interpolate.CubicSpline,但如何将它与垂直扭曲结合起来?

是否有一个 fnc 可以像这样在给定帧中插入网格?

For a project I want a grid like this: 5x5. The points should be movable later but I got that i guess.

standard grid

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. 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?

is there a fnc to interpolate a grid in a given frame like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

树深时见影 2025-01-19 09:47:09

使用 scipy.interpolate.interp2d

在二维网格上进行插值。
x、y 和 z 是用于近似某个函数 f 的值数组:z = f(x, y),它返回标量值 z。此类返回一个函数,其调用方法使用样条插值来查找新点的值。

所以你有一个数组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))

编辑:

如果你想要的是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

Use 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. This class returns a function whose call method uses spline interpolation to find the value of new points.

So you have an array orig that you want to generate 100x50 array res using bicubic interpolation

# 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:

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文