如何将2D网格点云插值到连续区域? [英] How do I interpolate a 2D gridded point cloud to a continuous area?
问题描述
我有一个二维Numpy NDarray,其浮点数介于0到8之间.该二维数组的大小为(1000, 1600)
,大约有1400个值(点云中的点),其余值为
I have a 2 dimensional Numpy NDarray filled with floats between 0 and about 8. This 2 dimensional arrays size is (1000, 1600)
and there are about 1400 values, (the points in the point cloud), the remaining values are None
, so matplotlib does not plot these values. You can see the plotted table in the image below. What I'd like to have is, the None-values interpolated with the values next to it to have a gradientlike heatmap. This pointcloud represents the shape of a roof and I want to process this data to an image I can give into a neural network to detect the type of roof.
我用于此情节的代码很短,
The code I used for this plot 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):
推荐答案
tricontourf
您可以使用有效值的tricontour
/tricontourf
图.为此,您首先需要过滤掉所有nan
值(实际上应该使无效值np.nan
而不是None
).
这些值及其坐标可以放入plt.tricontourf()
中,而无需手动插值即可获得等高线图.
tricontourf
You might use a tricontour
/ tricontourf
plot of the valid values. To this end, you first need to filter out all nan
values (you should indeed make the invalid values np.nan
instead of None
).
Those values, together with their coordinates can be put into plt.tricontourf()
to obtain a contour plot without the need of manual interpolation.
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()
然后使用tripcolor
是另一种选择:
Using tripcolor
is another option then:
plt.tripcolor(x,y,z, shading='gouraud')
您还可以先使用matplotlib.mlab.griddata
将数据插值到网格上,然后再使用常规的contourf
绘图
You can also interpolate the data on a grid first, using matplotlib.mlab.griddata
, and then either use a normal contourf
plot,
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
图,
interpolate and imshow
Or in the same manner use an imshow
plot,
plt.imshow(zi)
这篇关于如何将2D网格点云插值到连续区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!