我试着在投影3D数据的点云后,得到前面,后面,左边,右边,顶部,底部。但是结果并不好。有没有更好的方法来获得所需的视图?我已经使用numpy-stl文档和一些投影研究来做到这一点。
import numpy as np
import pandas as pd
import os
import time
import scipy
import math
from scipy import ndimage
from stl import mesh
from matplotlib import pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import cm
%matplotlib inline
# Load an existing stl file:
my_mesh = mesh.Mesh.from_file('Bolt.stl')
# Create a new plot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
# Load the STL files and add the vectors to the plot
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(my_mesh.vectors))
# Auto scale to the mesh size
scale = my_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)
# Show the plot to the screen
pyplot.show()

我已经从grabcad.com下载了bolt.stl。
在那之后,我试着预测它是不同的观点,比如:
points = my_mesh.vectors
x = points[:,0].flatten()
y = points[:,1].flatten()
z = points[:,2].flatten()
fig= plt.figure()
ax= fig.add_subplot(111, projection= '3d')
ax.scatter(x,y,z)
ax.plot(x, z, 'r+', zdir='y', zs=100)
ax.plot(y, z, 'g+', zdir='x', zs=-100)
ax.plot(x, y, 'k+', zdir='z', zs=-100)
ax.set_xlabel('X')
ax.set_xlim([-100, 100])
ax.set_ylabel('Y')
ax.set_ylim([-100, 100])
ax.set_zlabel('Z')
ax.set_zlim([-100, 100])
plt.show()

结果是如此糟糕,我甚至不知道,闪电3D在哪里。
我也尝试过:
X = points[:,0]
Y = points[:,1]
Z = points[:,2]
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
cset = ax.contourf(X, Y, Z, zdir='z', offset=-100, cmap="gray")
cset = ax.contourf(X, Y, Z, zdir='x', offset=-100, cmap="gray")
cset = ax.contourf(X, Y, Z, zdir='y', offset=100, cmap="gray")
ax.set_xlabel('X')
ax.set_xlim(-100, 100)
ax.set_ylabel('Y')
ax.set_ylim(-100, 100)
ax.set_zlabel('Z')
ax.set_zlim(-100, 100)
plt.show()

我也尝试过使用这段代码,它是我在"MVCNN for 3D shape recognition “中找到的。
def point_cloud_to_volume(points, size=32):
vol = np.zeros((size,size,size))
locations = np.copy(points)
locations[:] *= (size-1)
locations = locations.astype(int)
locations[locations >= size] = 0.0
locations[locations < 0] = 0.0
vol[locations[:,0],locations[:,1],locations[:,2]] = 1.0
return vol
def vol2depthmap(volume_array, bg_val=32):
vol = volume_array.argmax(2)
vol[vol == 0] = bg_val
return vol
v = point_cloud_to_volume(points)
%matplotlib inline
from matplotlib import pyplot as plt
plt.imshow(vol2depthmap(v), interpolation='nearest', cmap='Greys')
plt.colorbar()

最后,我尝试了:
front_proj = points[:, :, :].sum(axis=1)
plt.plot(front_proj)
plt.show()

side_proj = points[:, :, :].sum(axis=0)
plt.plot(side_proj)
plt.show()

top_proj = points[:, :, :].sum(axis=2)
plt.plot(top_proj)
plt.show()

我尝试过使用切片,但正如您已经猜到的那样,结果会是什么。
front = points[:,:,0]
back = points[:,:,-1]
top = points[0,:,:]
bottom = points[-1,:,:]
r_side = points[:,-1,:]
l_side = points[:,0,:]






如何获得.stl文件的正确视图,如下所示。请忽略绘图和尺寸。

或者任何其他cad格式都可以。我尝试在.stl文件中使用它,因为到目前为止我还没有找到python库,以加载除stl之外的其他cad格式。
发布于 2021-02-24 01:52:16
你试过这个吗?
figure = plt.figure()
axes = mplot3d.Axes3D(figure)
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(mymesh.vectors))
axes.view_init(90,270) # top view
scale = mymesh.points.flatten('F') #C, F, A, or K
axes.auto_scale_xyz(scale, scale, scale)
plt.show()尝试0,90,180,270的view_init组合以获得其他投影。
发布于 2021-08-16 05:19:08
下面是你可以做的事情:
vertices = my_mesh.vectors.reshape(3*len(my_mesh.vectors), 3)
vertices = np.unique(vertices, axis=0)
x, y, z = zip(*vertices)
figure = plt.figure()
axes = mplot3d.Axes3D(figure)
axes.scatter3D(x, y, z)
plt.show()https://stackoverflow.com/questions/61335792
复制相似问题