this question is related with my previous question How to use numpy interpolation to increase a vector size, but this time I'm looking for a method to do increase the 2D array size and not a vector.
The idea is that I have couples of coordinates (x;y) and I want to smooth the line with a desired number of (x;y) pairs
for a Vector solution I use the answer of @AGML user with very good results
from scipy.interpolate import UnivariateSpline
def enlargeVector(vector, size):
old_indices = np.arange(0,len(a))
new_length = 11
new_indices = np.linspace(0,len(a)-1,new_length)
spl = UnivariateSpline(old_indices,a,k=3,s=0)
return spl(new_indices)
You can use the function map_coordinates from the scipy.ndimage.interpolation module.
import numpy as np
from scipy.ndimage.interpolation import map_coordinates
A = np.random.random((10,10))
new_dims = []
for original_length, new_length in zip(A.shape, (100,100)):
new_dims.append(np.linspace(0, original_length-1, new_length))
coords = np.meshgrid(*new_dims, indexing='ij')
B = map_coordinates(A, coords)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With