簡體   English   中英

Python中的點雲數據創建表面網格

[英]Create surface grid from point cloud data in Python

這是一個創建點雲的示例,然后我想將網格表面擬合到該點雲。 當我嘗試將 meshgrid arrays 傳遞給插值數據的 function 時,問題出現在最后:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

# Create some point cloud data:
c = 1
a = 3
b = 4

slice = {}
t = np.linspace(0,2*np.pi,50)

for s in np.linspace(1,9,10):
    c = 5*s
    r = (-s**2+10.0*s)/10.0
    X = r*np.cos(t)
    Y = r*np.sin(t)
    Z = c*(Y**2/b**2 - X**2/a**2) + c
    slice[str(int(s))] = np.vstack([X,Y,Z])


# Visualize it:

fig = plt.figure()
ax = fig.gca(projection = '3d')

for k,v in slice.iteritems():
    print type(v)
    print np.shape(v)
    X = v[0,:]
    Y = v[1,:]
    Z = v[2,:]
    ax.scatter(X,Y,Z)
plt.show()

它看起來像這樣:

在此處輸入圖像描述

我現在需要根據這些點創建一個表面網格。 在這種情況下,表面有多種解釋,因為我只有一個點雲,而不是 function z = f(x,y),但在這種情況下,正確的表面應該是創建空心“扭曲圓柱體”的表面。 我想像這樣解決這個問題:

# stack all points from each slice into one vector for each coordinate:
Xs = []
Ys = []
Zs = []
for k,v in slice.iteritems():
    #ax.plot_surface(X,Y,Z)
    X = v[0,:]
    Y = v[1,:]
    Z = v[2,:]
    Xs = np.hstack((Xs,X))
    Ys = np.hstack((Ys,Y))
    Zs = np.hstack((Zs,Z))

XX, YY = np.meshgrid(Xs,Ys)

from scipy import interpolate
f = interpolate.interp2d(Xs,Ys,Zs, kind = 'cubic')
ZZ = f(XX,YY)

然后我可以使用 plot

fig = plt.figure()
ax = fig.gca(projection = '3d')

ax.plot_surface(XX, YY, ZZ)
plt.show()

但是,插值 function 似乎不接受 arrays 作為輸入,因此此方法可能無效。 誰能就如何正確執行此操作提出建議?

編輯:

實際上,數據顯然不能表示為 function,因為它不是一對一的。

我偶然發現了同樣的問題,想知道為什么在過去的 7 年里它沒有得到解決。 這是我基於plot_trisurf (以及相應的代碼示例)為任何未來讀者提供的解決方案。

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.tri as mtri

# Create some point cloud data:
a = 3
b = 4

# def grid of parametric variables
u = np.linspace(0,2*np.pi,50)
v = np.linspace(1,9,50)
U, V = np.meshgrid(u, v)
U, V = U.flatten(), V.flatten()

# Triangulate parameter space to determine the triangles
tri = mtri.Triangulation(U, V)

# get the transformed data as list
X,Y,Z = [],[],[]
for _u in u:
  for _v in v:
    r = (-_v**2+10.0*_v)/10.0
    x = r*np.cos(_u)
    y = r*np.sin(_u)
    z = 5*_v*(y**2/b**2 - x**2/a**2) + 5*_v
    X.append(x)
    Y.append(y)
    Z.append(z)

# Visualize it:
fig = plt.figure()
ax = fig.gca(projection = '3d')
ax.scatter(X,Y,Z, s=1, c='r')
ax.plot_trisurf(X, Y, Z, triangles=tri.triangles, alpha=.5)
plt.show()

這將產生以下 plot。 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM