Percentile contrast stretch

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

Percentile contrast stretch is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Normalize high-dynamic-range satellite or medical imagery to [0,1] using per-channel percentile clipping to suppress outliers while preserving relative contrast

How to use this skill

Skill file

Percentile Contrast Stretch

Overview

Satellite and medical images often have extreme pixel outliers that wash out simple min-max normalization. Percentile contrast stretching clips each channel at the 2nd and 98th percentiles, then linearly maps to [0,1]. This preserves meaningful contrast while being robust to dead pixels, sensor noise, and atmospheric artifacts.

Quick Start

import numpy as np

def percentile_stretch(image, lower=2, upper=98):
    out = np.zeros_like(image, dtype=np.float32)
    for c in range(image.shape[2]):
        lo = np.percentile(image[:, :, c], lower)
        hi = np.percentile(image[:, :, c], upper)
        out[:, :, c] = (image[:, :, c] - lo) / (hi - lo + 1e-10)
    return np.clip(out, 0, 1)

rgb_stretched = percentile_stretch(rgb_image)

Workflow

  1. Compute lower and upper percentiles per channel
  2. Linearly map each channel: (pixel - lo) / (hi - lo)
  3. Clip result to [0, 1]
  4. Apply before visualization or as model input normalization

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.