SAM-Geo Python Tutorial: Segmenting Geospatial Imagery for GIS

Image Segmentation in Geospatial Imagery is a crucial process in Geographic Information Systems (GIS), Remote Sensing, and Spatial Data Analysis. It entails the identification and separation of objects or regions from satellite imagery, aerial photography, and drone images. Recent improvements in artificial intelligence (AI) and deep learning have made image segmentation increasingly accurate, automated, and scalable.
SAM-Geo refers to the Segment Anything Model for geospatial applications. It incorporates the capabilities of Meta's Segment Anything Model (SAM) for segmentation operations. This allows the user to perform segmentation of objects in satellite and aerial imagery through prompt-based segmentation approaches. Unlike the process of manually digitizing all geographic features, GIS analysts can apply visual prompts to identify buildings, roads, bodies of water, plants, farmlands, and any other land features.
The Python programming language provides a suitable environment for performing an image segmentation workflow using SAM. Using SAM-Geo alongside geospatial libraries such as Rasterio, GeoPandas, GDAL, and OpenCV allows the user to produce segmentation masks.

What Is SAM-Geo?
SAM-Geo is an application of the Segment Anything Model to geospatial data to make it easier to perform object segmentation tasks using Earth Observation imagery. It introduces prompt-based object segmentation functionality into geospatial datasets, so that it becomes possible to perform object detection using visual prompts in the form of points, bounding boxes, and text.
The Segment Anything Model is an open-source project developed by Meta AI aimed at creating object masks using visual prompts. This project consists of three main parts:
Image Encoder: An architecture built with a Vision Transformer (ViT) that extracts high-dimensional visual embeddings from the input image.
Prompt Encoder: Transforms user prompts such as points and bounding boxes into embeddings.
Mask Decoder: Combines image and prompt embeddings to create masks and confidence scores.
For geospatial tasks, the described functionality can be used to detect geographic objects based on satellite images, aerial photos, and drone imagery data.
Different from the usual pixel classification models, prompt-based segmentation enables the selection of objects without the need for a dedicated segmentation model for each feature type. Nonetheless, the correctness of the outcome relies heavily on the quality of images, resolution, model choice, and object complexity.
Why Use SAM-Geo for Geospatial Image Segmentation?
Standard GIS classification methods tend to employ supervised machine learning models that need labeled training datasets. Preparation of such datasets is not easy and is rather time-consuming, particularly if one works with huge satellite imagery databases.
However, SAM-Geo suggests an interactive segmentation process based on visual prompting that generates object masks.
Main benefits:
Prompt-Based Segmentation: A user can give hints to the algorithm by applying points or bounding boxes to identify particular geographic objects.
Decreased Manual Work: Automated masking decreases manual work needed to identify boundaries of buildings, roads, water, and vegetation areas.
High-Resolution Imagery: SAM-Geo processes allow high-resolution image processing to extract features for further mapping.
GIS Flexibility: Segmentation results may be transformed into geospatial raster masks or polygons.
Python-Based Segmentation Pipeline: The process allows creating scalable segmentation workflows based on Python processing.
Several Applications: The technique may be useful for many geospatial applications, including land cover mapping, urban analysis, environmental monitoring, etc.
However, there needs to be a distinction between segmentation and semantic classification. In general, while SAM detects boundaries of objects, it does not automatically classify geographic objects such as residential buildings, forests, and water.
Installing SAM-Geo and Required Dependencies
SAM-Geo is available as a Python package through PyPI. You can install it using pip.
Step 1: Create a Python environment
python -m venv samgeo_envActivate the environment.
For Windows:
samgeo_env\Scripts\activateFor Linux and macOS:
source samgeo_env/bin/activateStep 2: Install SAM-Geo
pip install samgeoStep 3: Install additional geospatial libraries
pip install rasterio geopandas numpy matplotlib opencv-pythonPyTorch must also be installed with a build compatible with your operating system and CPU or CUDA configuration. Follow the official installation instructions for the appropriate version.
Step 4: Verify the installation
import samgeo
import rasterio
import geopandas as gpd
import numpy as np
print("SAM-Geo environment is ready!")
These steps establish the basic environment for geospatial segmentation. Specific SAM-Geo classes and model checkpoints depend on the installed package version.
Implementing SAM-Geo Using Python
The following workflow demonstrates how to segment geospatial imagery using SAMGeo.
Important package clarification: The current Python distribution is named segment-geospatial, while its Python module is imported as samgeo. Install the package using the following command:
pip install "segment-geospatial[samgeo]"For SAM 2 or SAM 3 workflows, use the corresponding optional dependencies:
pip install "segment-geospatial[samgeo2]"pip install "segment-geospatial[samgeo3]"The selected model version determines the compatible model class, checkpoint, and configuration.
Step 1: Import Required Libraries
Start by importing the required Python modules.
import os
import rasterio
import numpy as np
import matplotlib.pyplot as plt
from samgeo import SamGeo
These libraries provide raster data access, numerical processing, visualization, and segmentation functionality.
Step 2: Define the Input Satellite Image
Specify the path to the georeferenced satellite or aerial image.
image_path = "satellite_image.tif"
with rasterio.open(image_path) as src:
print("Image CRS:", src.crs)
print("Image dimensions:", src.width, src.height)
print("Number of bands:", src.count)
print("Pixel resolution:", src.res)
This step verifies the raster metadata before processing. Ensure that the input image has suitable RGB bands and valid geospatial information.
Step 3: Initialize the SAM Model
Initialize the segmentation model by selecting a model checkpoint.
sam = SamGeo(
model_type="vit_h",
checkpoint="sam_vit_h_4b8939.pth",
automatic=True,
device="cuda"
)
The model_type parameter specifies the model architecture, while checkpoint identifies the pretrained model weights. The device parameter determines whether processing runs on a CUDA-compatible GPU or CPU.
For CPU execution, use:
device="cpu"The checkpoint must be downloaded and placed at the specified location. Select a compatible checkpoint and model configuration from the official documentation.
Step 4: Generate Segmentation Masks
Load the satellite imagery and execute automatic mask generation.
sam. generate(
image_path,
output="segmentation_mask.tif",
foreground=True,
unique=True
)
The model analyzes the image and generates segmentation masks representing detected objects and regions.
Automatic mask generation is useful when the objective is to identify multiple objects across an image without manually specifying each target.
Step 5: Visualize the Segmentation Results
After generating the segmentation mask, visualize the output using Matplotlib.
with rasterio.open("segmentation_mask.tif") as src:
mask = src.read(1)
plt.figure(figsize=(12, 8))
plt.imshow(mask, cmap="tab20")
plt.title("SAM-Geo Segmentation Results")
plt.axis("off")
plt.show()
Working with SAM-Geo in QGIS and Other GIS Platforms
The outputs of SAM-Geo can be easily integrated into current geospatial processes. Following the creation of segmentation masks, users have the option to export their outputs in formats like GeoTIFF, GeoPackage, Shapefile, and GeoJSON.
These datasets can be used in:
QGIS: Viewing segmentation masks, editing polygons, and spatial analysis.
ArcGIS Pro: Incorporating extracted features into mapping, geoprocessing, and spatial database workflows.
GeoPandas: Joining spatially, performing geometry manipulation, and applying attribute filters.
Rasterio and GDAL: Managing georeferenced raster datasets and raster manipulations.
PostGIS: Storing, querying, and analyzing vector features.
Furthermore, SAM-Geo comes with a QGIS plugin enabling users to utilize segmentation tools from within a GIS application environment.
Limitations and Best Practices
While SAM-Geo simplifies the process of geospatial image segmentation, there are a number of technical aspects that should be considered before using the outputs in a production GIS workflow.
Resolution: Low-resolution imagery does not provide enough information to distinguish smaller geographical objects.
Spectral Limitations: Default SAM algorithms are mainly tailored towards visual image processing. Multispectral and hyperspectral datasets might need additional preprocessing steps.
Big Raster Processing: High-resolution satellite imagery may have too much information for memory. Tiling and batching will solve this issue.
Object Classification: The algorithm performs segmentation but does not imply semantics. Separate classification algorithms may be needed to perform this task.
Accuracy of Segmentation Boundaries: Shadows, overlapping objects, dense vegetation, and similar spectral properties may affect the segmentation process.
Geospatial Validation: Output CRS, spatial coordinates of the image, shape of the polygon, and accuracy of the output should be checked before further analysis.
In order to achieve reliable results, SAM-Geo should be combined with GIS validation and other techniques.
The SAM-Geo is a useful solution for segmentation of satellite, aerial, and drone images using Python and applying GIS applications. It utilizes the Segment Anything Model along with geospatial tools in order to create masks for objects, extract geographic features, and create GIS vector datasets from segmentation results.
The prompt-based segmentation functionality provided by SAM-Geo decreases the need for manual digitization and makes object extraction easier for remote sensing specialists, GIS analysts, and GeoAI developers.
If SAM-Geo is used in conjunction with Rasterio, GeoPandas, GDAL, QGIS, and other geospatial technologies, the tool could be applied to building detection, agriculture mapping, water body detection, vegetation analysis, infrastructure monitoring, etc. Nonetheless, the output of segmentation should be checked before its use in real geospatial projects.
As foundation models keep evolving, there are new prospects for geospatial applications based on SAM.
To learn more about SAM-Geo and its geospatial capabilities, click here.
For more information or any questions regarding the LizardTech suite of products, please don't hesitate to contact us at:
Email: info@geowgs84.com
USA (HQ): (720) 702–4849
(A GeoWGS84 Corp Company)





Comments