Skip to content

Download notebook

Segmenting remote sensing imagery with box prompts

image image

This notebook shows how to generate object masks from box prompts with the Segment Anything Model (SAM).

Make sure you use GPU runtime for this notebook. For Google Colab, go to Runtime -> Change runtime type and select GPU as the hardware accelerator.

Install dependencies

Uncomment and run the following cell to install the required dependencies.

1
# %pip install segment-geospatial
1
2
3
import leafmap
from samgeo.common import tms_to_geotiff
from samgeo import SamGeo

Create an interactive map

1
2
3
m = leafmap.Map(center=[-22.17615, -51.253043], zoom=18, height="800px")
m.add_basemap("SATELLITE")
m

Download a sample image

Pan and zoom the map to select the area of interest. Use the draw tools to draw a polygon or rectangle on the map

1
2
3
bbox = m.user_roi_bounds()
if bbox is None:
    bbox = [-51.2565, -22.1777, -51.2512, -22.175]
1
2
image = "Image.tif"
tms_to_geotiff(output=image, bbox=bbox, zoom=19, source="Satellite", overwrite=True)

You can also use your own image. Uncomment and run the following cell to use your own image.

1
# image = '/path/to/your/own/image.tif'

Display the downloaded image on the map.

1
2
3
m.layers[-1].visible = False
m.add_raster(image, layer_name="Image")
m

Initialize SAM class

Set automatic=False to disable the SamAutomaticMaskGenerator and enable the SamPredictor.

1
2
3
4
5
sam = SamGeo(
    model_type="vit_h",
    automatic=False,
    sam_kwargs=None,
)

Specify the image to segment.

1
sam.set_image(image)

Display the map. Use the drawing tools to draw some rectangles around the features you want to extract, such as trees, buildings.

1
m

Create bounding boxes

If no rectangles are drawn, the default bounding boxes will be used as follows:

1
2
3
4
5
6
7
if m.user_rois is not None:
    boxes = m.user_rois
else:
    boxes = [
        [-51.2546, -22.1771, -51.2541, -22.1767],
        [-51.2538, -22.1764, -51.2535, -22.1761],
    ]

Segment the image

Use the predict() method to segment the image with specified bounding boxes. The boxes parameter accepts a list of bounding box coordinates in the format of [[left, bottom, right, top], [left, bottom, right, top], ...], a GeoJSON dictionary, or a file path to a GeoJSON file.

1
sam.predict(boxes=boxes, point_crs="EPSG:4326", output="mask.tif", dtype="uint8")

Display the result

Add the segmented image to the map.

1
2
m.add_raster("mask.tif", cmap="viridis", nodata=0, layer_name="Mask")
m

Use an existing vector file as box prompts

Alternatively, you can specify a file path to a vector file. Let's download a sample vector file from GitHub.

1
2
3
url = "https://opengeos.github.io/data/sam/tree_boxes.geojson"
geojson = "tree_boxes.geojson"
leafmap.download_file(url, geojson)

Display the vector data on the map.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
m = leafmap.Map()
m.add_raster("Image.tif", layer_name="image")
style = {
    "color": "#ffff00",
    "weight": 2,
    "fillColor": "#7c4185",
    "fillOpacity": 0,
}
m.add_vector(geojson, style=style, zoom_to_layer=True, layer_name="Bounding boxes")
m

Segment image with box prompts

Segment the image using the specified file path to the vector mask.

1
sam.predict(boxes=geojson, point_crs="EPSG:4326", output="mask2.tif", dtype="uint8")

Display the segmented masks on the map.

1
2
m.add_raster("mask2.tif", cmap="Greens", nodata=0, opacity=0.5, layer_name="Tree masks")
m