Percentile Contrast Stretch

SkillDev tools

Normalize high-dynamic-range satellite or medical imagery to [0,1] using per-channel percentile clipping to suppress outliers while preserving relative contrast

Available today. Use it from your connected AI after setup.

Connect ahel once, and every AI you use reads what you have installed.

Then ask your AI: use the Percentile Contrast Stretch skill

What this skill tells your AI

The instructions your AI receives, as published by wenmin-wu/ds-skills in skills/cv/percentile-contrast-stretch/SKILL.md and read by ahel’s review.

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

  • Percentile range: 2/98 is standard; use 1/99 for less aggressive clipping
  • Per-channel vs global: per-channel preserves color balance across bands
  • vs histogram equalization: percentile stretch is linear and invertible; CLAHE introduces nonlinearity
  • Multi-spectral: works on any number of channels, not just RGB

References

Signals

GitHub stars
60
Forks
4
Last commit
Apr 2026
Advanced
Catalog kind
skill
Gateway key
cv-percentile-contrast-stretch
Source
github.com/wenmin-wu/ds-skills