Converting coordinates vector to numpy 2D matrix

こ雲淡風輕ζ 提交于 2019-12-12 13:24:18

问题


I have a set of 3D coordinates points: [lat,long,elevation] ([X,Y,Z]), derived from LIDAR data. The points are not sorted and the steps size between the points is more or less random.

My goal is to build a function that converts this set of points to a 2D numpy matrix of a constant number of pixels where each (X,Y) cell hold the Z value, then plot it as elevations heatmap.

  • scales must remain realistic, X and Y should have same step size.
  • the matrix doesn't have to catch the exact elevations picture, It will obviously need some kind of resolution reduction in order to have a constant number of pixels.

The solution I was thinking of is to build a bucket for each pixel, iterate over the points and put each in a bucket according to it's (X,Y) values. At last create a matrix where each sell holds the mean of the Z values in the corresponding bucket.

  1. Since I don't have lots of experience in this field I would love to hear some tips and specially if there are better ways to address this task.
  2. Is there a numpy function for converting my set of points to the desired matrix? (maybe meshgrid with steps of a constant value?)
  3. If I build very sparse matrix, where the step size is

    min[min{Xi,Xj} , min{Yk,Yl}] for all i,j,k,l

    is there a way to "reduce" the resolution and convert it to a matrix with the required size?

Thanks!


回答1:


You don't need to reinvent the bicycle.

from matplotlib.mlab import griddata
import numpy as np

#-- Your coordinates
x = np.random.random(100)
y = np.random.random(100)
z = np.random.random(100)*10
#--

#-- Your new grid
xsteps=200    # resolution in x
ysteps=200    # resolution in y
xi = linspace(min(x), max(x), xsteps)
yi = linspace(min(y), max(y), ysteps)
Z = griddata(x, y, z, xi, yi)  # interpolates between points in your data
#--

plt.pcolormesh(xi, yi, Z, cmap=plt.cm.hot)   # plot your elevation map :D
plt.show()



回答2:


I am aware that I am not answering half of your questions but this is how I would do it:

  1. Create a 2D array of the desired resolution,
  2. The "leftmost" values correspond to the smallest values of x and so forth
  3. Fill the array with the elevation value of the closest match in terms of x and y values
  4. Smoothen the result.


来源:https://stackoverflow.com/questions/33926704/converting-coordinates-vector-to-numpy-2d-matrix

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