簡體   English   中英

將 stl 代碼 plot 從 Matlab 轉換為 Python

[英]Converting stl code plot from Matlab to Python

我希望能夠 plot Python 中的 stl 文件。我可以使用以下代碼輕松地在 Matlab 中完成此操作:

phacon = stlread("Spine_PhaconModel.stl");

hold on

figure = trisurf(phacon,'FaceColor',[0.6 0.6 0.6], 'Edgecolor','none','FaceLighting','gouraud','AmbientStrength', 0.15, 'MarkerEdgeColor',"r")

view([180, -1])

camlight('headlight');

material('dull');

但是當我嘗試將代碼更改為 Python 時,output 並不是我想要的。 我正在尋找的 output plot 類似於下面所附的內容: Spine ouput from matlab

我嘗試使用 mesh.Mesh.from_file 等函數來獲取數據,相當於 Matlab function stlread() 和 plot_trisurf,相當於 Matlab function trisurf()。 我試過的代碼是:

fig = plt.figure()

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

stl_data = mesh.Mesh.from_file(Spine_PhaconModel.stl')

points = stl_data.points.reshape([-1, 3])

x = points[:,0]

y = points[:,1]

z = points[:,2]

collec = ax.plot_trisurf(x,y,z,linewidth=0.2)

但是,我不知道如何使它在視覺上看起來與第一張附加圖像相同。 我得到的是這個Spine Phacon output 使用 Python

非常感謝您的幫助,非常感謝!

文檔看來你需要 plot 作為 3d 多邊形,下面的答案顯示了如何 plot 它,以下是一個修改過的例子,因為文檔已經過時,matplotlib 從那時起似乎已經改變了。

from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot

# Create a new plot
figure = pyplot.figure()
axes = figure.add_subplot(projection='3d')

# Load the STL files and add the vectors to the plot
your_mesh = mesh.Mesh.from_file(r'your_mesh_path.stl')
poly_collection = mplot3d.art3d.Poly3DCollection(your_mesh.vectors)
poly_collection.set_color((0.7,0.7,0.7))  # play with color
axes.add_collection3d(poly_collection)

# Show the plot to the screen
pyplot.show()

如果你想要比 matlab 繪制的更好的東西,那么你應該使用vtkplotlib ,因為它使用你的 GPU 而不是 matplotlib CPU 光柵化,一個例子在這個答案https://stackoverflow.com/a/57501486/15649230

編輯:我修改了上面的代碼以刪除自動縮放,現在要自己縮放,你用鼠標左鍵旋轉,用鼠標中鍵移動,用鼠標右鍵縮放,這個答案在代碼https 中指定了如何做到這一點:/ /stackoverflow.com/a/65017422/15649230

暫無
暫無

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

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