5

特定の (x,y) 座標でのみ定義された値を持つ大きな (2000 x 2000) ピクセル グリッドがあります。たとえば、これを簡略化したバージョンは次のようになります。

-5-3--
---0--
-6--4-
-4-5--
---0--
-6--4-

グリッド内のすべての位置で定義された値を取得できるように、線形補間または最近傍補間を行うにはどうすればよいですか。

4

3 に答える 3

3

Scipy 関数の使用:

import numpy as np
from scipy.interpolate import griddata # not quite the same as `matplotlib.mlab.griddata`

grid = np.random.random((10, 10))
mask = np.random.random((10, 10)) < 0.2

points = mask.nonzero()
values = grid[points]
gridcoords = np.meshgrid[:grid.shape(0), :grid.shape(1)]

outgrid = griddata(points, values, gridcoords, method='nearest') # or method='linear', method='cubic'
于 2012-10-20T05:09:35.853 に答える
2

これが私の刺し傷です。

import numpy as np
from matplotlib.mlab import griddata

##Generate a random sparse grid
grid = np.random.random((6,6))*10
grid[grid>5] = np.nan

## Create Boolean array of missing values
mask = np.isfinite(grid)

## Get all of the finite values from the grid
values = grid[mask].flatten()

## Find indecies of finite values
index = np.where(mask==True)

x,y = index[0],index[1]

##Create regular grid of points
xi = np.arange(0,len(grid[0,:]),1)
yi = np.arange(0,len(grid[:,0]),1)

## Grid irregular points to regular grid using delaunay triangulation
ivals = griddata(x,y,values,xi,yi,interp='nn')

これが、不均一に分布したポイントを通常のグリッドに補間する方法です。私は他のタイプの内挿法(すなわち線形)を試していません。

于 2012-10-20T04:46:41.773 に答える
0

次の行を使用して、非常に簡単に最近傍補間を取得できます。

from scipy import ndimage as nd

indices = nd.distance_transform_edt(invalid_cell_mask, return_distances=False, return_indices=True)
data = data[tuple(ind)]

ここinvalid_cell_maskで、 は未定義の配列セルのブール値マスクであり、data埋められる配列です。

Filling gaps in a numpy array に完全な例を含む回答を投稿しました。

于 2013-08-28T13:00:01.243 に答える