我正在使用 python Open3d 处理三角形网格,我想向我的网格添加纹理映射(我在文档中没有找到它),这是一个带有简单立方体网格的示例代码:
import numpy as np
import open3d as o3d
vert=[[0,0,0],[0,1,0],[1,1,0],[1,0,0],
[0,0,1],[0,1,1],[1,1,1],[1,0,1]]
faces=[[0, 1, 2], [0, 2, 3], [6, 5, 4],
[7, 6, 4], [5, 1, 0], [0, 4, 5], [3, 2, 6],
[6, 7, 3], [0, 3, 7], [0, 7, 4], [1, 5, 6],
[1, 6, 2]]
m=o3d.geometry.TriangleMesh(o3d.open3d_pybind.utility.Vector3dVector(vert),
o3d.open3d_pybind.utility.Vector3iVector(faces))
m.compute_vertex_normals()
o3d.visualization.draw_geometries([m])
现在我尝试添加纹理:
text=cv2.imread('~/Downloads/cupe_uv.png')
plt.imshow(text)
DX,DY=0.5/2,0.66/2
v_uv=[[DX,DY],[DX,2*DY],[2*DX,2*DY],[2*DX,DY],
[0,DX],[DX,1],[3*DX,2*DY],[3*DX,DY]]
v_uv=np.asarray(v_uv)
v_uv=np.concatenate((v_uv,v_uv,v_uv),axis=0)
m.triangle_uvs = o3d.open3d_pybind.utility.Vector2dVector(v_uv)
m.textures=[o3d.geometry.Image(text)]
o3d.visualization.draw_geometries([m])
我知道我没有设置uv坐标来显示立方体的所有颜色(但有些颜色应该在那里......)。 不管怎样,网格仍然没有纹理(与开始时相同)。
mesh.triangle_uvs
是形状为 (3 * num_triangles, 2)
的数组,而不是 (3 * num_vertices, 2)
。
试试这个:
v_uv = np.random.rand(len(faces) * 3, 2)
m.triangle_uvs = o3d.open3d_pybind.utility.Vector2dVector(v_uv)
顺便说一下,你的Open3D版本似乎很旧。
Open3d
0.10.0
已经推出,并且添加了很多新功能。就像@jing-zhao 在https://stackoverflow.com/a/63005705/15099601中所说,
v_uv = np.random.rand(len(faces) * 3, 2)
m.triangle_uvs = o3d.open3d_pybind.utility.Vector2dVector(v_uv)
会起作用的。对于 Open3d 版本 0.10.0,
material_ids
似乎没有设置。
m.triangle_material_ids = o3d.utility.IntVector([0]*len(faces))
修复了这个问题,以便纹理立方体可以可视化而不会崩溃。
这个例子可能有帮助:
import cv2
import numpy as np
import open3d as o3d
# Define the vertices and faces for a square mesh
vertices = np.array([
[-0.5, -0.5, 0],
[0.5, -0.5, 0],
[0.5, 0.5, 0],
[-0.5, 0.5, 0],
])
faces = np.array([
[0, 1, 2],
[0, 2, 3],
])
# load the texture image
path_to_image = "data/some_image.png"
img = cv2.cvtColor(cv2.imread(path_to_image), cv2.COLOR_BGR2RGB)
# create the uv coordinates
v_uv = np.array([[0, 1], [1, 1], [1, 0],
[0, 1], [1, 0], [0, 0]])
# assign the texture to the mesh
mesh = o3d.geometry.TriangleMesh(o3d.utility.Vector3dVector(vertices), o3d.utility.Vector3iVector(faces))
mesh.textures = [o3d.geometry.Image(img)]
mesh.triangle_uvs = o3d.utility.Vector2dVector(v_uv)
mesh.triangle_material_ids = o3d.utility.IntVector([0] * len(faces))
o3d.visualization.draw_geometries([mesh])