pythonpoint-cloudsrealsenseopen3d

Open3d(Python), How to remove points from .ply


I get pointcloud from my Intel RealSense depth camera. And I want to remove extra points that are far away, how do I put a condition in the code?

Code for getting pointcloud:

import numpy as np
from open3d import *


def main():
    cloud = read_point_cloud("1.ply") # Read the point cloud
    draw_geometries([cloud]) # Visualize the point cloud


if __name__ == "__main__":
    main()

Code for viewing pointcloud:

import pyrealsense2 as rs


pipe = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth)
pipe.start(config)
colorizer = rs.colorizer()

try:

    frames = pipe.wait_for_frames()
    colorized = colorizer.process(frames)

    ply = rs.save_to_ply("1.ply")
    ply.set_option(rs.save_to_ply.option_ply_binary, False)
    ply.set_option(rs.save_to_ply.option_ply_normals, True)
    ply.process(colorized)

    print("Done")

finally:
    pipe.stop()

That what i want delete: enter image description here


Solution

  • The question does not say what points exactly are to be removed. Assuming you can provide a sphere of known radius and center location, the following code will remove any points outside that sphere:

    import numpy as np
    import open3d
    
    # Read point cloud from PLY
    pcd1 = open3d.io.read_point_cloud("1.ply")
    points = np.asarray(pcd1.points)
    
    # Sphere center and radius
    center = np.array([1.586, -8.436, -0.242])
    radius = 0.5
    
    # Calculate distances to center, set new points
    distances = np.linalg.norm(points - center, axis=1)
    pcd1.points = open3d.utility.Vector3dVector(points[distances <= radius])
    
    # Write point cloud out
    open3d.io.write_point_cloud("out.ply", pcd1)