# 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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yarroudh.gitbook.io/segment-lidar/tutorial/basic-usage.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
