从 3D 数据集构建 2D 热图图像

问题描述 投票:0回答:1

我开发了一个脚本,可以根据椭圆的

Area
和许多其他变量创建同心椭圆的 X、Y 点。我还使用了一个函数来创建基于每个椭圆面积的 Z 值。然后根据前一个椭圆的面积和 Z 值对每个值进行加权校正
arf_w
。代码如下:

import numpy as np

center_ft = (0, 0) #Center coordinates
area_mi = 0.5      #Area of largest ellipses
n_ellipses = 15    #Number of concentric ellipses
R = 0.01           #Reduction value for concentric ellipses
Duration = 3       #Variable for area reduction
Ratio = 2.25       #Major to minor axis length ratio
Theta = 45         #Angle of concentric ellipses about center

h, k = center_ft
Area_initial = area_mi * 5280**2

#Building largest ellipses
MinorAxis_initial = np.sqrt(Area_initial / (np.pi * Ratio))
MajorAxis_initial = Ratio * MinorAxis_initial

#Creating area and axis lengths lists for inner concentric ellipses
Area_List = np.linspace(R * Area_initial, Area_initial, n_ellipses).tolist()
Area_List = [x/5280**2 for x in Area_List]
MajorAxis_List = np.linspace(R * MajorAxis_initial, MajorAxis_initial, n_ellipses).tolist()
MinorAxis_List = np.linspace(R * MinorAxis_initial, MinorAxis_initial, n_ellipses).tolist()

x = []
y = []
z = []
arf = []
for (a, b, Area, i) in zip(MajorAxis_List, MinorAxis_List, Area_List, range(n_ellipses)):
  t = np.linspace(0, 2 * np.pi, 100)
  x0 = a * np.cos(t) * np.cos(Theta) - b * np.sin(t) * np.sin(Theta) + h #x coordinates
  y0 = a * np.cos(t) * np.sin(Theta) + b * np.sin(t) * np.cos(Theta) + k #y coordinates
  arf0 = 0.01 * (100 - Duration * (Area**0.46)) #reduction equation
  z0 = np.ones(len(t))*arf0

  arf.append(arf0)
  x.append(x0)
  y.append(y0)
  z.append(z0)

#Weighted reduction factor developed from inner ellipses 
arf_w = []
for (i) in range(n_ellipses):
  arf_w0 = (arf[i-1] * Area_List[i-1] - Area_List[i] * arf [i]) / (Area_List[i-1] - Area_List[i])
  arf_w.append(arf_w0)
arf_w[0] = arf[0]

for i in range(n_ellipses):
  z[i] = arf_w[i]*np.ones(len(t))

目标是生成热图图像,其中每个同心椭圆都根据其等效 Z 值

arf_w
进行填充。最终,这将用作地理空间信息软件中应用程序的 .tiff。我认为我在生成同心椭圆的函数方面有一个良好的开端,但在将同心椭圆移动到图像时遇到了障碍。我熟悉以下库:

https://scikit-image.org/docs/stable/api/skimage.draw.html#skimage.draw.ellipse

https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.ellipse

我没有运气根据我想从这些库中的椭圆函数中使用的变量来复制同心椭圆模式。附件是用

matplotlib
绘制的同心椭圆图,绘图代码已删除,但想显示代码结果的示例。该图像看起来类似于绘图,但每个椭圆都会填充一种颜色,直到下一个椭圆基于其各自的
arf_w
。非常感谢任何帮助或建议。

python arrays numpy image-processing geospatial
1个回答
0
投票

不确定我是否理解

arf_w
值需要发生什么,但您应该能够根据您的需求调整此代码:

x_range = 2000
y_range = 3000
x = np.linspace(-x_range,x_range,1000) + h
y = np.linspace(-y_range,y_range,1000) + k

X,Y = np.meshgrid(x,y)
Z = np.zeros_like(X)

# rewrite ellipse as quadratic form
cT = np.cos(Theta)
sT = np.sin(Theta)
dX = X-h
dY = Y-k
U = dX*cT+dY*sT
V = dX*sT-dY*cT
for i, (a, b) in enumerate(zip(MajorAxis_List[::-1], MinorAxis_List[::-1])):
    u = U/a
    v = V/b
    mask = u*u + v*v <= 1
    Z[mask] = i

plt.figure(2).clf()
plt.imshow(Z, origin="lower", extent=(-x_range,x_range,-y_range,y_range))
© www.soinside.com 2019 - 2024. All rights reserved.