Basic usage
segment-lidar v0.1.5
Import the necessary module:
from segment_lidar import samlidar
Create an instance of the SamLidar class and specify the path to the checkpoint file ckpt_path when instantiating the class:
model = samlidar.SamLidar(ckpt_path="sam_vit_h_4b8939.pth")
Read the point cloud data from a .las/.laz file using the read method of the SamLidar instance. Provide the path to the point cloud file pointcloud.las as an argument:
points = model.read("pointcloud.las")
Apply the Cloth Simulation Filter (CSF) algorithm for ground filtering using the csf method of the SamLidar instance. This method returns the filtered point cloud cloud, the non-ground non_ground and the ground ground indices:
cloud, non_ground, ground = model.csf(points)
Perform segmentation using the segment method of the SamLidar instance. This method requires the filtered point cloud cloud as input, and you can optionally provide an image path image_path and labels path labels_path to save the segmentation results as an image and labels, respectively. The segment method returns the segmentation labels labels:
labels, *_ = model.segment(points=cloud, image_path="raster.tif", labels_path="labeled.tif")
Save results to .las/.laz file using the write method of the SamLidar instance:
model.write(points=points, non_ground=non_ground, ground=ground, segment_ids=labels, save_path="segmented.las")
Now, the entire code should look like this:
from segment_lidar import samlidar
model = samlidar.SamLidar(ckpt_path="sam_vit_h_4b8939.pth")
points = model.read("pointcloud.las")
cloud, non_ground, ground = model.csf(points)
labels, *_ = model.segment(points=cloud, image_path="raster.tif", labels_path="labeled.tif")
model.write(points=points, non_ground=non_ground, ground=ground, segment_ids=labels, save_path="segmented.las")
The resulted point cloud contains a new scalar field called segment_id. For visualization and further processing, we recommand using CloudCompare.
The following figure shows the results of the segmentation on the sample data:

Last updated
Was this helpful?