尝试使用体素方法将球体缺陷 (.ply) 插入到对象 (.obj) 中,使用 open3d 绕过空域和顶点。请帮忙:)

问题描述 投票:0回答:1

get_voxel_center() 在下面的代码中不起作用:

import open3d as o3d
import numpy as np

def main():
    # File paths for the try.obj and sphere.ply files
    try_obj_path = '/Users/xd_anshul/Desktop/Research/try.obj'
    defect_path = '/Users/xd_anshul/Desktop/Research/Project Defect/sphere.ply'

    # Load meshes
    try_mesh = o3d.io.read_triangle_mesh(try_obj_path)
    defect_mesh = o3d.io.read_triangle_mesh(defect_path)

    # Apply transformations if needed (scaling, translation, rotation)
    scaled_defect = defect_mesh.scale(0.5, center=defect_mesh.get_center())

    # Convert meshes to voxel grids
    voxel_size = 0.2  # Adjust voxel size as needed
    try_voxel_grid = o3d.geometry.VoxelGrid.create_from_triangle_mesh(try_mesh, voxel_size)
    defect_voxel_grid = o3d.geometry.VoxelGrid.create_from_triangle_mesh(scaled_defect, voxel_size)

    # Get coordinates of occupied voxels in the try.obj mesh
    occupied_voxels_coords = np.array(try_voxel_grid.get_voxels())

    # Select a random occupied voxel
    voxel_index = np.random.randint(0, len(occupied_voxels_coords))
    selected_voxel = occupied_voxels_coords[voxel_index]

    # Translate the defect to the selected voxel's position
    translation_vector = try_voxel_grid.get_voxel_center(selected_voxel)
    scaled_defect.translate(translation_vector)

    # Merge meshes
    combined_mesh = try_mesh + scaled_defect

    # Visualize combined mesh
    o3d.visualization.draw_geometries([combined_mesh])

if __name__ == "__main__":
    main()

为了绕过 get_voxel_center(),我手动计算了中心,但此错误仍然存在:

import open3d as o3d
import numpy as np

def main():
    # File paths for the try.obj and sphere.ply files
    try_obj_path = '/Users/xd_anshul/Desktop/Research/try.obj'
    defect_path = '/Users/xd_anshul/Desktop/Research/Project Defect/sphere.ply'

    # Load meshes
    try_mesh = o3d.io.read_triangle_mesh(try_obj_path)
    defect_mesh = o3d.io.read_triangle_mesh(defect_path)

    # Apply transformations if needed (scaling, translation, rotation)
    scaled_defect = defect_mesh.scale(0.5, center=defect_mesh.get_center())

    # Convert meshes to voxel grids
    voxel_size = 0.2  # Adjust voxel size as needed
    try_voxel_grid = o3d.geometry.VoxelGrid.create_from_triangle_mesh(try_mesh, voxel_size)
    defect_voxel_grid = o3d.geometry.VoxelGrid.create_from_triangle_mesh(scaled_defect, voxel_size)

    # Get coordinates of occupied voxels in the try.obj mesh
    occupied_voxels_coords = np.array(try_voxel_grid.get_voxels())

    # Select a random occupied voxel
    voxel_index = np.random.randint(0, len(occupied_voxels_coords))
    selected_voxel = occupied_voxels_coords[voxel_index]

    # Compute the voxel center based on its index and size
    voxel_center = np.array(selected_voxel) * voxel_size + voxel_size / 2

    # Translate the defect to the selected voxel's position
    scaled_defect.translate(voxel_center)

    # Merge meshes
    combined_mesh = try_mesh + scaled_defect

    # Visualize combined mesh
    o3d.visualization.draw_geometries([combined_mesh])

if __name__ == "__main__":
    main()

以下分别是上面 2 个代码的错误:

(base) xd_anshul@MacBook-Air ~ % python -u "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py"
Traceback (most recent call last):
  File "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py", line 39, in <module>
    main()
  File "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py", line 29, in main
    translation_vector = try_voxel_grid.get_voxel_center(selected_voxel)
AttributeError: 'open3d.cpu.pybind.geometry.VoxelGrid' object has no attribute 'get_voxel_center'
(base) xd_anshul@MacBook-Air ~ % python -u "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py"
Traceback (most recent call last):
  File "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py", line 41, in <module>
    main()
  File "/Users/xd_anshul/Desktop/Research/Project Defect/voxel_open3d.py", line 29, in main
    voxel_center = np.array(selected_voxel) * voxel_size + voxel_size / 2
TypeError: unsupported operand type(s) for *: 'open3d.cpu.pybind.geometry.Voxel' and 'float'

尝试使用trimesh和VTK库,但体素化没有发生,这是将缺陷插入到对象的实体区域/部分而不是任何空域中所必需的,因为该对象是空心的并且与缺陷非常不同本身是一个球体。

python vtk voxel open3d trimesh
1个回答
0
投票

您应该使用

get_voxel_center_coordinate
而不是
get_voxel_center
。请参阅 open3d 文档此处

translation_vector = try_voxel_grid.get_voxel_center_coordinate(selected_voxel.grid_index)
© www.soinside.com 2019 - 2024. All rights reserved.