0

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?

JKR
  • 11
  • 3

1 Answers1

0

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
freshpasta
  • 556
  • 2
  • 11
  • What is arr in your example? is it orig or res? – JKR Mar 22 '22 at 06:52
  • Also i dont wanna interpolate values to be clear, i just need to know, if i interpolated between the 5x5 points where the 100x50 points would be distributed to – JKR Mar 22 '22 at 07:10
  • Fixed code. So you want to have a 2D array of where the 100x50 points will map to on the original grid? If not please provide sample input and output I'm having a hard time getting what you want – freshpasta Mar 22 '22 at 17:02