Spectral index segmentation

From wenmin-wu/ds-skills · View interactive version

Spectral index segmentation is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Compute normalized spectral band ratios (NDWI, CCCI, NDVI) from multispectral imagery and threshold for binary segmentation of water, vegetation, or other targets

How to use this skill

Skill file

Spectral Index Segmentation

Overview

Multispectral satellites capture bands beyond visible RGB (NIR, red-edge, SWIR). Normalized difference indices combine these bands into single-channel images where target features (water, vegetation, chlorophyll) have distinct value ranges. A simple threshold on the index produces a binary segmentation mask without any ML model.

Quick Start

import numpy as np
from skimage.transform import resize

def ndwi(green, nir):
    return (green - nir) / (green + nir + 1e-10)

def ccci(red, red_edge, mir):
    return ((mir - red_edge) / (mir + red_edge + 1e-10) *
            (mir - red) / (mir + red + 1e-10))

def ndvi(red, nir):
    return (nir - red) / (nir + red + 1e-10)

ms = load_16band_image(image_id)
re = resize(ms[5], rgb.shape[:2])
mir = resize(ms[7], rgb.shape[:2])
r = rgb[:, :, 0].astype(float)

index = ccci(r, re, mir)
water_mask = (index > 0.11).astype(np.uint8)

Workflow

  1. Load multispectral bands and resize to a common spatial resolution
  2. Select bands for the target index (e.g., green+NIR for NDWI)
  3. Compute the normalized ratio — values typically range [-1, 1]
  4. Threshold to produce a binary mask
  5. Optionally apply morphological cleaning (opening/closing)

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.