robust_median_stretch
Stretch a linear view in place so its median lands on a target value, using robust MAD statistics to set black point and midtones transfer. Supports linked channels, multiple passes, and highlight compression.
Instructions
Stretch a linear view in place so its median lands on target_median. Per pass: from the median M and the unnormalized median absolute deviation D of the image (range clipping off, alpha excluded), sigma = 1.4826·D and the black point c0 = max(0, M − black_point_sigma·sigma); samples are rescaled to u = clamp((x − c0)/(1 − c0), 0, 1), and the midtones transfer function MTF(m, u) = (m − 1)·u / ((2m − 1)·u − m) is applied with the balance m solved so that MTF(m, u(M)) = target_median exactly. linked true takes one set of statistics over all channels jointly; false takes them per channel. Each further pass repeats this on the previous output, re-measuring the statistics; passes before the last run on a scratch copy, so the view is written once and is left unmodified when any pass cannot be solved (a median on the black point, e.g. D = 0). With highlight_knee h and highlight_midtones mh, values y above h become h + (1 − h)·MTF(mh, (y − h)/(1 − h)) after the last pass. The result is JSON: per pass and per statistics group M, D, sigma, c0, the rescaled median xbar and m.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| linked | No | Colour images only, required for them: true = joint statistics over all channels and one transform for all; false = statistics and transform per channel. Ignored for a grayscale image. | |
| passes | No | Number of times the whole procedure runs, each on the previous output (>= 1; omitted = 1) | |
| view_id | Yes | View to stretch (modified in place) | |
| target_median | Yes | Median of the output, strictly between 0 and 1 | |
| highlight_knee | No | Output level above which values are compressed, strictly between 0 and 1. Given together with highlight_midtones; omitted = no highlight compression. | |
| black_point_sigma | Yes | Distance of the black point below the median, in units of 1.4826·MAD (>= 0) | |
| highlight_midtones | No | MTF balance applied to the segment above highlight_knee, strictly between 0.5 and 1. Given together with highlight_knee. |