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:
JavaScript
x
12
12
1
import numpy as np
2
from open3d import *
3
4
5
def main():
6
cloud = read_point_cloud("1.ply") # Read the point cloud
7
draw_geometries([cloud]) # Visualize the point cloud
8
9
10
if __name__ == "__main__":
11
main()
12
Code for viewing pointcloud:
JavaScript
1
25
25
1
import pyrealsense2 as rs
2
3
4
pipe = rs.pipeline()
5
config = rs.config()
6
config.enable_stream(rs.stream.depth)
7
pipe.start(config)
8
colorizer = rs.colorizer()
9
10
try:
11
12
frames = pipe.wait_for_frames()
13
colorized = colorizer.process(frames)
14
15
ply = rs.save_to_ply("1.ply")
16
ply.set_option(rs.save_to_ply.option_ply_binary, False)
17
ply.set_option(rs.save_to_ply.option_ply_normals, True)
18
ply.process(colorized)
19
20
print("Done")
21
22
finally:
23
pipe.stop()
24
25
Advertisement
Answer
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:
JavaScript
1
18
18
1
import numpy as np
2
import open3d
3
4
# Read point cloud from PLY
5
pcd1 = open3d.io.read_point_cloud("1.ply")
6
points = np.asarray(pcd1.points)
7
8
# Sphere center and radius
9
center = np.array([1.586, -8.436, -0.242])
10
radius = 0.5
11
12
# Calculate distances to center, set new points
13
distances = np.linalg.norm(points - center, axis=1)
14
pcd1.points = open3d.utility.Vector3dVector(points[distances <= radius])
15
16
# Write point cloud out
17
open3d.io.write_point_cloud("out.ply", pcd1)
18