简体   繁体   English

如何使用open3d提取底层体素?

[英]How to extract the bottom layer voxels using open3d?

I want to extract the bottom layer voxels.我想提取底层体素。 For example, extract the part from the armadillo.例如,从犰狳中提取部分。 Does anyone know how to do that?有谁知道这是怎么做到的吗?

在此处输入图像描述

This is how I find the lowest voxel.这就是我找到最低体素的方法。

#Get voxels center coordinate
def get_voxel_center_coordinate(voxel_grid):
    voxels = voxel_grid.get_voxels()
    voxel_center = []
    
    for voxel in voxels:
        voxel_center.append(voxel_grid.get_voxel_center_coordinate(voxel.grid_index))
    
    return voxel_center

#Find lowest voxels index
def find_lowest_voxel_index(voxel_grid):
    voxels = voxel_grid.get_voxels()
    voxel_center = get_voxel_center_coordinate(voxel_grid)
    
    for i in range(0, len(voxel_center)):
        if i == 0:
            min_z = voxel_center[i, 2]
        else:
            if min_z > voxel_center[i, 2]:
                min_z = voxel_center[i, 2]
    ind = 0
    lowest_voxel_index = []
    
    for center in voxel_center:
        if center[2] == min_z:
            print(center[2])
            lowest_voxel_index.append(voxels[ind].grid_index)
            ind += 1
    
    return lowest_voxel_index

Your solution is almost correct.您的解决方案几乎是正确的。 I'll point out a few things that you should change and then a way to verify the solution.我会指出一些你应该改变的事情,然后是一种验证解决方案的方法。

Solution解决方案

Things that should change in the given solution:给定解决方案中应该改变的事情:

  1. You cannot access the z-axis through voxel_center[i, 2] since voxel_center is a list, not a numpy array.您无法通过voxel_center[i, 2]访问 z 轴,因为voxel_center是一个列表,而不是一个 numpy 数组。 You should instead access them as voxel_center[i][2] .您应该改为以voxel_center[i][2]的形式访问它们。
  2. You should increment ind at each loop, not only when the condition is true.您应该在每个循环中增加ind ,而不仅仅是在条件为真时。 Alternatively, you can use enumerate which overcomes this cleanly.或者,您可以使用enumerate干净地克服了这个问题。

Therefore, the correct implementation would be the following:因此,正确的实现如下:

#Get voxels center coordinate
def get_voxel_center_coordinate(voxel_grid):
    voxels = voxel_grid.get_voxels()
    voxel_center = []
    
    for voxel in voxels:
        voxel_center.append(voxel_grid.get_voxel_center_coordinate(voxel.grid_index))
    
    return voxel_center

#Find lowest voxels index
def find_lowest_voxel_index(voxel_grid):
    voxels = voxel_grid.get_voxels()
    voxel_center = get_voxel_center_coordinate(voxel_grid)
    
    for i in range(0, len(voxel_center)):
        if i == 0:
            min_z = voxel_center[i][2]
        else:
            if min_z > voxel_center[i][2]:
                min_z = voxel_center[i][2]
    ind = 0
    lowest_voxel_index = []
    
    for center in voxel_center:
        if center[2] == min_z:
            print(center[2])
            lowest_voxel_index.append(voxels[ind].grid_index)
        ind += 1
    
    return lowest_voxel_index

Verification确认

To verify the solution, you might want to visualize the results.要验证解决方案,您可能需要可视化结果。 However, VoxelGrid does not have any method that allows the user to eg, change the color of a particular voxel.然而, VoxelGrid没有任何方法允许用户更改特定体素的颜色。 Therefore, you can think of creating extra meshes to visualize the filtered voxels.因此,您可以考虑创建额外的网格来可视化过滤后的体素。 Since VoxelGrid provides get_voxel_bounding_points , you can make use of these points to create an AxisAlignedBoundingBox .由于VoxelGrid提供get_voxel_bounding_points ,您可以利用这些点来创建AxisAlignedBoundingBox

lowest_index = find_lowest_voxel_index(voxel_grid)  # this uses your solution after modifications

bounding_box = []
for i in lowest_index:
    bb = o3d.geometry.AxisAlignedBoundingBox.create_from_points(voxel_grid.get_voxel_bounding_points(i))
    bb.color = np.array([1,0,1])  # feel free to choose any other color
    bounding_box.append(bb)


axes = o3d.geometry.TriangleMesh.create_coordinate_frame(origin=np.array([100,100,100]), size=100)  # in order to be sure of the direction

o3d.visualization.draw_geometries([voxel_grid, *bounding_box, axes])

This is how running this code on an armadillo mesh looks like.这就是在犰狳网格上运行此代码的样子。 Note that I added the coordinate frame to make sure of the direction.请注意,我添加了坐标系以确保方向。 Blue arrow points toward +z.蓝色箭头指向 +z。 The voxel with the lowest z value has purple edges. z 值最低的体素具有紫色边缘。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM