Sparse mesh interpolation using python (preferably scipy)

I have a large (2000 x 2000) pixel grid that has values โ€‹โ€‹defined only in specific (x, y) coordinates. For example, a simplified version would look like this:

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

How can I do interpolation using interpolation or closest neighbor so that I can have a specific value in all places of the grid.

+4
source share
3 answers

Using the Scipy Function:

 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' 
+3
source

Here is my hit on him.

 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') 

This is how I interpolate unevenly distributed points onto a regular grid. I have not tried to use any other interpolation method (i.e. linear).

+2
source

You can get the nearest neighbor interpolation very simply using the following lines:

 from scipy import ndimage as nd indices = nd.distance_transform_edt(invalid_cell_mask, return_distances=False, return_indices=True) data = data[tuple(ind)] 

where invalid_cell_mask is the Boolean mask of the cells in the array, which are undefined and data is the array to be filled.

I posted the answer with a complete example in Filling the spaces in a numpy array .

0
source

Source: https://habr.com/ru/post/1440147/


All Articles