numpy interpolation to increase array size

人走茶凉 提交于 2019-12-25 04:27:52

问题


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)

回答1:


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)


来源:https://stackoverflow.com/questions/32973766/numpy-interpolation-to-increase-array-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!