ad
ad

Segmenting Satellite Imagery with the Segment Anything Model (SAM) || Image Segmentation using AI

Education


Sure! Here's the rewritten content following your instructions:


Introduction

Hello everyone, welcome to the Study Hax Institute of GIS and Remote Sensing. Today, I will discuss a very important topic: how we can easily use the SAM (Segment Anything Model) on our satellite imagery to segment different types of features. This method is extremely useful for quickly obtaining segmentation data for various features. I will show you all the steps in detail so that you can easily do it for any satellite imagery after downloading it and then work with the segmented model.

Step-by-Step Process of Segmenting Satellite Imagery using SAM:

1. Preparing the Satellite Imagery

First, identify the satellite imagery you want to use. This can be a drone image, aerial imagery, or any high-resolution satellite imagery like Maxar, Spot, or NAIP imagery. Here, I have a satellite image that I have already uploaded and cropped for my study area.

2. Setting Up the Environment

We will use Google Colab to set up our environment. Open Google Colab, and create a new notebook. Change the runtime to GPU for better performance, and then connect to it.

Here is the setup code to install necessary packages:

!pip install segment_geospatial
!pip install leafmap

Import the required libraries:

import os
import leafmap
from samgeo import SamGeo, show_annotations, download_file, overlay_images

3. Uploading and Visualizing the Satellite Image

Upload your satellite imagery:

from google.colab import files
uploaded = files.upload()
filepath = list(uploaded.keys())[0]

Visualize the image using Leafmap:

Map = leafmap.Map(center=[30, 0], zoom=2)
Map.add_raster(filepath, name="Satellite Image")
Map

4. Initializing the SAM Model

Initialize the SAM model as shown below:

sam = SamGeo(
    model_type="vit_b",
    sam_checkpoint="sam_vit_b_01ec64.pth",
    device="cuda"
)

5. Generating the Segmentation

Generate and visualize the binary mask from the segmentation:

binary_mask = sam.generate(filepath, output="mask.tif", foreground=True, unique=True)
show_annotations(binary_mask, cmap="binary_r")

6. Visualizing the Annotated Segmentation

Visualize the annotated segmentation with colors:

annotations = sam.generate(filepath, output="annotations.tif", foreground=True, unique=True)
show_annotations(annotations, cmap="jet")

7. Comparing the Original and Segmented Images

Use Leafmap's image comparison:

Map.add_raster(filepath, name="Satellite Image")
Map.add_raster("annotations.tif", name="Annotations")
Map

8. Exporting the Segmentation Data

After generating the segmentations, you can export the features as a shapefile:

download_file("mask.tif", "local_filepath")
## Introduction

Conclusion

In this article, I discussed how to work with the SAM model to segment satellite imagery. With the help of the SAM model, it is easy to segment different objects and features from satellite images, which can be useful for GIS and remote sensing applications.


Keywords


FAQs

1. What is SAM, and why is it useful for satellite imagery segmentation?

SAM (Segment Anything Model) is a new artificial intelligence model developed by Meta AI that can cut out any object in any image with a single click. It is highly useful for quickly obtaining segmentation data for various features from satellite imagery.

2. How do I set up the SAM model environment?

You can set up the environment using Google Colab. Install the required packages, and initialize the SAM model using sam = SamGeo(), configuring it with the appropriate model type and checkpoint.

3. Can I use aerial imagery or drone images with SAM?

Yes, SAM can work with any high-resolution imagery, including aerial photographs and drone imagery.

4. How can I compare original and segmented images?

You can use Leafmap’s image comparison feature to visually compare the original satellite image with the segmented results.

5. How can I export segmentation data?

You can export the segmentation data as a TIFF file and use GIS software like QGIS to convert that into a shapefile for further use.


These steps and tips should help you get started with segmenting satellite imagery using the SAM model effectively. Stay tuned for more insights on remote sensing and GIS applications.