How can I interpolate a 2D cloud of grid points to a continuous region?

I have a two-dimensional Nump NDarray filled with floats from 0 to 8. The size of this 2-dimensional array (1000, 1600)is about 1400 values ​​(points in a point cloud), the rest are values None, so matplotlib does not display these values. You can see the table in the figure below. What I would like to have are the None values, interpolated with the values ​​next to it, have a gradient heatmap. This pointcloud is a shape of the roof, and I want to process this data on an image that I can give to the neural network to determine the type of roof.

The code I used for this graph is pretty short,

import matplotlib.pyplot as plt
plt.clf()
#plotGrid is the numpy.ndarray with shape (1000, 1600) and dtype float
plt.imshow(plotGrid, cmap='gray', interpolation='nearest')
plt.colorbar()
plt.show()

Image (click to enlarge and see points): pPRrP.png

+5
2

tricontourf

tricontour/tricontourf. nan ( np.nan None). plt.tricontourf() .

import matplotlib.pyplot as plt
import numpy as np

# Generate some example data
f = lambda x,y : np.exp((-(x-150)**2-(y-150)**2)/3.e3)
plotGrid = np.zeros((300,300))*np.nan
coo = np.random.randint(5,295, size=(150,2) )
for x,y in coo:
    plotGrid[y,x] = f(x,y)
#plotGrid is now a numpy.ndarray with shape (300,300), mostly np.nan, and dtype float

# filter out nan values and get coordinates.
x,y = np.indices(plotGrid.shape)
x,y,z = x[~np.isnan(plotGrid)], y[~np.isnan(plotGrid)], plotGrid[~np.isnan(plotGrid)]

plt.tricontourf(x,y,z)

plt.colorbar()
plt.show()

enter image description here

tripcolor

tripcolor - :

plt.tripcolor(x,y,z, shading='gouraud')

enter image description here

contourf

, matplotlib.mlab.griddata, contourf ,

xi = np.linspace(0, plotGrid.shape[1], plotGrid.shape[1])
yi = np.linspace(0, plotGrid.shape[0], plotGrid.shape[0])
zi = mlab.griddata(x, y, z, xi, yi, interp='linear')
plt.contourf(xi, yi, zi, 15)

imshow

imshow,

plt.imshow(zi)

enter image description here

+3

, scipy.interpolate.interp2d , :

import scipy.interpolate

z_all = plotGrid.astype(float)            # convert nones to nan
x_all, y_all = np.indices(plotGrid.shape) # get x and y coordinates

# convert to 1d arrays of coordinates
valid = ~np.isnan(z_all)
x, y, z = x_all[valid], y_all[valid], z_all[valid]

# interpolate
interp = scipy.interpolate.interp2d(x, y, z)
filled_data = interp(x_all[:,0], y_all[0,:])  # this is kinda gross, but `interp` doesn't
                                              # do normal broadcasting

plt.imshow(filled_data)
0

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


All Articles