How to use open3d - 10 common examples

To help you get started, we’ve selected a few open3d examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github agisoft-llc / metashape-scripts / src / align_model_to_model.py View on Github external
def global_registration(v1, v2, global_voxel_size):
    # See http://www.open3d.org/docs/release/tutorial/Advanced/global_registration.html#global-registration
    source = to_point_cloud(v1)
    target = to_point_cloud(v2)
    source_down = downscale_point_cloud(source, global_voxel_size)
    target_down = downscale_point_cloud(target, global_voxel_size)
    source_fpfh = estimate_points_features(source_down, global_voxel_size)
    target_fpfh = estimate_points_features(target_down, global_voxel_size)

    distance_threshold = global_voxel_size * 2.0
    max_validation = np.min([len(source_down.points), len(target_down.points)]) // 2
    global_registration_result = o3d.registration.registration_ransac_based_on_feature_matching(
        source_down, target_down,
        source_fpfh, target_fpfh,
        distance_threshold,
        o3d.registration.TransformationEstimationPointToPoint(False), 4, [
            o3d.registration.CorrespondenceCheckerBasedOnEdgeLength(0.9),
            o3d.registration.CorrespondenceCheckerBasedOnDistance(distance_threshold)
        ], o3d.registration.RANSACConvergenceCriteria(4000000, max_validation))
    return source_down, target_down, global_registration_result
github intel-isl / Open3D / examples / Python / Basic / mesh.py View on Github external
mesh = o3d.io.read_triangle_mesh("../../TestData/knot.ply")
    print(mesh)
    print(np.asarray(mesh.vertices))
    print(np.asarray(mesh.triangles))
    print("")

    print("Try to render a mesh with normals (exist: " +
          str(mesh.has_vertex_normals()) + ") and colors (exist: " +
          str(mesh.has_vertex_colors()) + ")")
    o3d.visualization.draw_geometries([mesh])
    print("A mesh with no normals and no colors does not seem good.")

    print("Computing normal and rendering it.")
    mesh.compute_vertex_normals()
    print(np.asarray(mesh.triangle_normals))
    o3d.visualization.draw_geometries([mesh])

    print("We make a partial mesh of only the first half triangles.")
    mesh1 = copy.deepcopy(mesh)
    mesh1.triangles = o3d.utility.Vector3iVector(
        np.asarray(mesh1.triangles)[:len(mesh1.triangles) // 2, :])
    mesh1.triangle_normals = o3d.utility.Vector3dVector(
        np.asarray(mesh1.triangle_normals)[:len(mesh1.triangle_normals) //
                                           2, :])
    print(mesh1.triangles)
    o3d.visualization.draw_geometries([mesh1])

    print("Painting the mesh")
    mesh1.paint_uniform_color([1, 0.706, 0])
    o3d.visualization.draw_geometries([mesh1])
github intel-isl / Open3D / examples / Python / Misc / sampling.py View on Github external
if __name__ == "__main__":
    o3d.utility.set_verbosity_level(o3d.utility.VerbosityLevel.Debug)

    path = "[path_to_reconstruction_system_output]"
    out_path = "[path_to_sampled_frames_are_located]"
    make_clean_folder(out_path)
    make_clean_folder(os.path.join(out_path, "depth/"))
    make_clean_folder(os.path.join(out_path, "image/"))
    make_clean_folder(os.path.join(out_path, "scene/"))
    sampling_rate = 30

    depth_image_path = get_file_list(os.path.join(path, "depth/"),
                                     extension=".png")
    color_image_path = get_file_list(os.path.join(path, "image/"),
                                     extension=".jpg")
    pose_graph_global = o3d.io.read_pose_graph(
        os.path.join(path, template_global_posegraph_optimized))
    n_fragments = len(depth_image_path) // n_frames_per_fragment + 1
    pose_graph_fragments = []
    for i in range(n_fragments):
        pose_graph_fragment = o3d.io.read_pose_graph(
            os.path.join(path, template_fragment_posegraph_optimized % i))
        pose_graph_fragments.append(pose_graph_fragment)

    depth_image_path_new = []
    color_image_path_new = []
    traj = []
    cnt = 0
    for i in range(len(depth_image_path)):
        if i % sampling_rate == 0:
            metadata = [cnt, cnt, len(depth_image_path) // sampling_rate + 1]
            print(metadata)
github intel-isl / Open3D / src / UnitTest / Python / test_octree.py View on Github external
def test_octree_convert_from_point_cloud():
    octree = o3d.geometry.Octree(1, [0, 0, 0], 2)

    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(_eight_cubes_points)
    pcd.colors = o3d.utility.Vector3dVector(_eight_cubes_colors)
    octree.convert_from_point_cloud(pcd)
github intel-isl / Open3D / src / UnitTest / Python / test_octree.py View on Github external
def test_octree_OctreeNodeInfo():
    origin = [0, 0, 0]
    size = 2.0
    depth = 5
    child_index = 7

    node_info = o3d.geometry.OctreeNodeInfo(origin, size, depth, child_index)
    np.testing.assert_equal(node_info.origin, origin)
    np.testing.assert_equal(node_info.size, size)
    np.testing.assert_equal(node_info.depth, depth)
    np.testing.assert_equal(node_info.child_index, child_index)
github intel-isl / Open3D / src / UnitTest / Python / test_octree.py View on Github external
def test_octree_voxel_grid_convert():
    pwd = os.path.dirname(os.path.realpath(__file__))
    data_dir = os.path.join(pwd, os.pardir, os.pardir, os.pardir, "examples",
                            "TestData")
    pcd_path = os.path.join(data_dir, "fragment.ply")
    pcd = o3d.io.read_point_cloud(pcd_path)
    octree = o3d.geometry.Octree(8)
    octree.convert_from_point_cloud(pcd)

    voxel_grid = octree.to_voxel_grid()
    octree_copy = voxel_grid.to_octree(max_depth=8)
github intel-isl / Open3D / src / UnitTest / Python / test_octree.py View on Github external
def test_locate_leaf_node():
    pwd = os.path.dirname(os.path.realpath(__file__))
    data_dir = os.path.join(pwd, os.pardir, os.pardir, os.pardir, "examples",
                            "TestData")
    pcd_path = os.path.join(data_dir, "fragment.ply")
    pcd = o3d.io.read_point_cloud(pcd_path)

    max_depth = 5
    octree = o3d.geometry.Octree(max_depth)
    octree.convert_from_point_cloud(pcd, 0.01)

    # Try locating a few points
    for idx in range(0, len(pcd.points), 200):
        point = pcd.points[idx]
        node, node_info = octree.locate_leaf_node(np.array(point))
        # The located node must be in bound
        assert octree.is_point_in_bound(point, node_info.origin, node_info.size)
        # Leaf node must be located
        assert node_info.depth == max_depth
        # Leaf node's size must match
        assert node_info.size == octree.size / np.power(2, max_depth)
github felixchenfy / 3D-Scanner-by-Baxter / test_ros / read_cloud_and_pub_by_open3d.py View on Github external
# Main
if __name__ == "__main__":

    # Params setting
    node_name = "read_cloud_and_pub_by_open3d"
    topic_name_rgbd_cloud = rospy.get_param( "/topic_name_rgbd_cloud",
        "camera/depth_registered/points")
    file_folder=rospy.get_param("file_folder")
    file_name=rospy.get_param("file_name")
    filename = file_folder+file_name

    # Set node
    rospy.init_node(node_name, anonymous=True)

    # Read file
    open3d_cloud = open3d.read_point_cloud(filename)
    rospy.loginfo("Loading cloud file from: \n  " + filename)
    print(open3d_cloud)
    ros_cloud = convertCloudFromOpen3dToRos(open3d_cloud)

    # Publish
    pub = rospy.Publisher(topic_name_rgbd_cloud, PointCloud2, queue_size=10)
    cnt = 0
    while not rospy.is_shutdown():
        rospy.sleep(1)
        pub.publish(ros_cloud)
        rospy.loginfo("Publish {:d}th cloud...\n".format(cnt))
        cnt += 1
    rospy.loginfo("this node stops: "+node_name)
github Ryan-Zirui-Wang / farthest-point-sampling / main_group.py View on Github external
fps_v0.fit()
    print("FPS sampling finished.")

    labels = fps_v0.group(radius)
    print("FPS grouping finished.")

    pcd_obj = o3d.geometry.PointCloud()
    pcd_obj.points = o3d.utility.Vector3dVector(pcd_xyz)

    pcd_color = np.zeros_like(pcd_xyz)
    for i, l in enumerate(labels):
        color = colormap[l]
        pcd_color[i] = color

    pcd_obj.colors = o3d.utility.Vector3dVector(pcd_color)

    return pcd_obj
github Ryan-Zirui-Wang / farthest-point-sampling / main_sample.py View on Github external
pcd_xyz = load_pcd(example_data)
    print("Loaded ", example_data, "with shape: ", pcd_xyz.shape)

    if n_samples > pcd_xyz.shape[0]:
        print("WARNING: required {0:d} samples but the loaded point cloud only has {1:d} points.\n "
              "Change the n_sample to {2:d}.".format(n_samples, pcd_xyz.shape[0], pcd_xyz.shape[0]))
        print("WARNING: sampling")
        n_samples = pcd_xyz.shape[0]

    fps = FPS(pcd_xyz, n_samples)
    print("Initialised FPS sampler successfully.")
    print("Running FPS over {0:d} points and geting {1:d} samples.".format(pcd_xyz.shape[0], n_samples))

    # Init visualisation
    pcd_all = o3d.geometry.PointCloud()
    pcd_all.points = o3d.utility.Vector3dVector(fps.pcd_xyz)
    pcd_all.paint_uniform_color([0, 1, 0])  # original: green

    pcd_selected = o3d.geometry.PointCloud()

    if manually_step is False:
        fps.fit()  # Get all samples.
        print("FPS sampling finished.")

        pcd_selected.points = o3d.utility.Vector3dVector(fps.get_selected_pts())
        pcd_selected.paint_uniform_color([1, 0, 0])  # selected: red

        o3d.visualization.draw_geometries([pcd_all, pcd_selected])
        print("You can step the sampling process by setting \"--manually_step\" to True and press \"N/n\".")
    else: