> For the complete documentation index, see [llms.txt](https://yarroudh.gitbook.io/segment-lidar/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yarroudh.gitbook.io/segment-lidar/tutorial/basic-usage.md).

# Basic usage

Import the necessary module:

```python
from segment_lidar import samlidar
```

2. Create an instance of the SamLidar class and specify the path to the checkpoint file **ckpt\_path** when instantiating the class:

```python
model = samlidar.SamLidar(ckpt_path="sam_vit_h_4b8939.pth")
```

3. 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:

```python
points = model.read("pointcloud.las")
```

4. 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:

```python
cloud, non_ground, ground = model.csf(points)
```

5. 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:

```python
labels, *_ = model.segment(points=cloud, image_path="raster.tif", labels_path="labeled.tif")
```

6. Save results to **.las/.laz** file using the **write** method of the SamLidar instance:

```python
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:

```python
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")
```

7. The resulted point cloud contains a new scalar field called segment\_id. For visualization and further processing, we recommand using [CloudCompare](https://www.danielgm.net/cc).

The following figure shows the results of the segmentation on the sample data:

<figure><img src="/files/xPSFOkDWMEB7A8xiDN3L" alt=""><figcaption><p>Results of segment-lidar with ground filtering option</p></figcaption></figure>
