feat: luma-guided chroma detail and point SNR fading

This commit is contained in:
zhoubinjia committed 2026-11-06 20:48:59 +08:00
1 parent 172db82a80
commit 64c53eafb0
7 files changed
+99 -11

No files matched your search

@@ -177,10 +177,23 @@ internal object PhotonDenoiseShaders {
// (σ²_Y, σ²_Cb, σ²_Cr, σ_CbCr) at level pixel p; mu receives the camera signal.
// gain = lscGain(levelCentre(p, uLevel)).
vec4 modelNoiseGain(ivec2 p, vec3 base, vec3 gain, out vec3 mu) {
// §2.1 per-channel source variance of the camera signal at base; mu receives that signal.
vec3 pointVariance(ivec2 p, vec3 base, out vec3 mu) {
mu = max(rgbFromYcc(base) / uScale / uWb, vec3(0.0));
vec2 merge = mergeRatio(levelCentre(p, uLevel));
vec3 v = merge.x * uRead + merge.y * uShot * mu;
return merge.x * uRead + merge.y * uShot * mu;
}
// Point luma SNR Y / σ_Y of one source sample, the same at every level (§7.2).
float pointLumaSnr(ivec2 p, vec3 base) {
vec3 mu;
vec3 aY = uScale * vec3(0.25, 0.5, 0.25) * uWb;
float variance = dot(aY * aY, pointVariance(p, base, mu));
return variance > 0.0 ? base.x / sqrt(variance) : 1e30;
}
vec4 modelNoiseGain(ivec2 p, vec3 base, vec3 gain, out vec3 mu) {
vec3 v = pointVariance(p, base, mu);
vec3 aY = uScale * vec3(0.25, 0.5, 0.25) * uWb;
vec3 aCb = uScale * vec3(0.0, -1.0, 1.0) * uWb;
vec3 aCr = uScale * vec3(1.0, -1.0, 0.0) * uWb;
@@ -824,8 +837,27 @@ internal object PhotonDenoiseShaders {
float gateY = sumS2 > 0.0 ? smoothstep(uGateLuma.x, uGateLuma.y, 9.0 * sumL2 / max(sumS2, 1e-30)) : 1.0;
float stat = mahalanobis(lap.yz, noise);
float gateC = uChromaGate == 0 ? 0.0 : stat >= 0.0 ? smoothstep(uGateChroma.x, uGateChroma.y, stat) : 1.0;
float gate = max(gateY, gateC);
chromaBand = (uChromaStrength * gate + (1.0 - uChromaStrength)) * lap.yz;
// §7.2: where only luma structure opens the gate, the detail is the chroma the 5×5
// luma regression predicts, not the noisy band itself.
vec2 num = vec2(0.0);
float den = 0.0;
for (int dy = -2; dy <= 2; ++dy) {
for (int dx = -2; dx <= 2; ++dx) {
ivec2 q = p + ivec2(dx, dy);
float l = LUMA_ENABLED ? pilotAt(q).x : lapAt(q).x;
num += l * lapAt(q).yz;
den += l * l + uChromaGuide * noiseAt(q).x;
}
}
float l0 = LUMA_ENABLED ? pilotAt(p).x : lap.x;
vec2 guided = den > 0.0 ? num / max(den, 1e-30) * l0 : vec2(0.0);
vec2 detail = gateC * lap.yz + (1.0 - gateC) * gateY * guided;
// §7.2: below chromaDetailFrom the detail fades out with the point luma SNR.
if (uChromaDetail != 0) {
detail *= smoothstep(uChromaDetailSnr.x, uChromaDetailSnr.y, pointLumaSnr(p, base));
}
if (noise.x <= 0.0) detail = lap.yz; // clipped: no model, detail kept
chromaBand = uChromaStrength * detail + (1.0 - uChromaStrength) * lap.yz;
if (BILATERAL_ENABLED) {
vec2 sum = vec2(0.0);
@@ -884,6 +916,9 @@ internal object PhotonDenoiseShaders {
uniform vec2 uGateLuma;
uniform vec2 uGateChroma;
uniform int uChromaGate; // §7.2 Mahalanobis gate on at this level
uniform int uChromaDetail; // §7.2 chroma detail faded by the point luma SNR at this level
uniform vec2 uChromaDetailSnr;
uniform float uChromaGuide; // §7.2 luma-noise regularisation of the guided chroma detail
uniform float uBilateralLuma;
uniform float uBilateralChroma;
@@ -34,6 +34,12 @@ internal object PhotonDenoiseTuning {
val gateChroma: FloatArray,
/** First level whose chroma detail the Mahalanobis gate may keep; finer ones use the luma gate only (§7.2). */
val chromaGateFrom: Int,
/** Levels below this keep their chroma detail only where the point luma SNR allows (§7.2). */
val chromaDetailFrom: Int,
/** Point luma SNR (low, high) over which those levels' chroma detail fades in (§7.2). */
val chromaDetailSnr: FloatArray,
/** Luma-noise regularisation ε of the luma-guided chroma detail at luma structure (§7.2). */
val chromaGuide: Float,
/** Coarsest chroma levels receiving the luma-guided joint bilateral (§7.3). */
val bilateralLevels: Int,
val bilateralLuma: Float,
@@ -42,7 +48,7 @@ internal object PhotonDenoiseTuning {
init {
require(alpha.size == MAX_LEVELS && grain.size == MAX_LEVELS && gamma.size == MAX_LEVELS &&
outlier.size == MAX_LEVELS)
require(gateLuma.size == 2 && gateChroma.size == 2)
require(gateLuma.size == 2 && gateChroma.size == 2 && chromaDetailSnr.size == 2)
}
}
@@ -68,6 +74,13 @@ internal object PhotonDenoiseTuning {
// At L0 / L1 the χ²₂ tail of demosaic-clustered chroma noise opens the gate as isolated
// colour dots (ISO 9100, 8 frames: all of them); real colour edges there carry luma (§7.2).
chromaGateFrom = 2,
// Luma-coupled colour dots of 4–8 px pass the L2 luma gate in the shadows (point SNR
// 1.8–2.8 at ISO 9100); the clean mid-grey wall sits at 5.4, highlights near 38 (§7.2).
chromaDetailFrom = 3,
chromaDetailSnr = floatArrayOf(4f, 10f),
// Chroma noise along luma edges passed the luma gate whole (ISO 9100 charger edge); the
// 5×5 luma regression brings it to the floor of dropping L0–L2 chroma (§7.2).
chromaGuide = 2f,
bilateralLevels = 3,
bilateralLuma = 4f,
bilateralChroma = 4f,
@@ -157,6 +170,9 @@ internal object PhotonDenoiseTuning {
gateLuma = mix(a.gateLuma, b.gateLuma),
gateChroma = mix(a.gateChroma, b.gateChroma),
chromaGateFrom = if (f < 0.5f) a.chromaGateFrom else b.chromaGateFrom,
chromaDetailFrom = if (f < 0.5f) a.chromaDetailFrom else b.chromaDetailFrom,
chromaDetailSnr = mix(a.chromaDetailSnr, b.chromaDetailSnr),
chromaGuide = a.chromaGuide + f * (b.chromaGuide - a.chromaGuide),
bilateralLevels = if (f < 0.5f) a.bilateralLevels else b.bilateralLevels,
bilateralLuma = a.bilateralLuma + f * (b.bilateralLuma - a.bilateralLuma),
bilateralChroma = a.bilateralChroma + f * (b.bilateralChroma - a.bilateralChroma),
@@ -565,6 +565,9 @@ internal class PhotonMultiScaleDenoiser {
uniform1f(program, "uGamma", params.gamma[level])
uniform1f(program, "uChromaStrength", minOf(params.chromaStrength, 1f))
uniform1i(program, "uChromaGate", if (level >= params.chromaGateFrom) 1 else 0)
uniform1i(program, "uChromaDetail", if (level < params.chromaDetailFrom) 1 else 0)
uniform2f(program, "uChromaDetailSnr", params.chromaDetailSnr)
uniform1f(program, "uChromaGuide", params.chromaGuide)
val thresholdScale = params.chromaThresholdScale()
GLES31.glUniform2f(location(program, "uGateLuma"),
params.gateLuma[0] * thresholdScale, params.gateLuma[1] * thresholdScale)
@@ -664,6 +667,8 @@ internal class PhotonMultiScaleDenoiser {
private fun uniform1f(program: Int, name: String, value: Float) = GLES31.glUniform1f(location(program, name), value)
private fun uniform2i(program: Int, name: String, size: IntArray) =
GLES31.glUniform2i(location(program, name), size[0], size[1])
private fun uniform2f(program: Int, name: String, v: FloatArray) =
GLES31.glUniform2f(location(program, name), v[0], v[1])
private fun uniform3f(program: Int, name: String, v: FloatArray) =
GLES31.glUniform3f(location(program, name), v[0], v[1], v[2])
@@ -67,6 +67,9 @@ internal class PhotonDenoiseParams(
val gateLuma: FloatArray,
val gateChroma: FloatArray,
val chromaGateFrom: Int,
val chromaDetailFrom: Int,
val chromaDetailSnr: FloatArray,
val chromaGuide: Float,
val bilateralLevels: Int,
val bilateralLuma: Float,
val bilateralChroma: Float,
@@ -154,6 +157,9 @@ internal class PhotonDenoiseParams(
put("gateLuma", JSONArray(gateLuma.map(Float::toDouble)))
put("gateChroma", JSONArray(gateChroma.map(Float::toDouble)))
put("chromaGateFrom", chromaGateFrom)
put("chromaDetailFrom", chromaDetailFrom)
put("chromaDetailSnr", JSONArray(chromaDetailSnr.map(Float::toDouble)))
put("chromaGuide", chromaGuide.toDouble())
put("bilateralLevels", bilateralLevels)
put("bilateralLuma", bilateralLuma.toDouble())
put("bilateralChroma", bilateralChroma.toDouble())
@@ -353,6 +359,9 @@ internal object PhotonNoiseModel {
gateLuma = tuning.gateLuma.copyOf(),
gateChroma = tuning.gateChroma.copyOf(),
chromaGateFrom = tuning.chromaGateFrom,
chromaDetailFrom = tuning.chromaDetailFrom,
chromaDetailSnr = tuning.chromaDetailSnr.copyOf(),
chromaGuide = tuning.chromaGuide,
bilateralLevels = tuning.bilateralLevels,
bilateralLuma = tuning.bilateralLuma,
bilateralChroma = tuning.bilateralChroma,
+8 -3
View File
@@ -308,11 +308,12 @@ out = ŝ + β_l · (L − ŝ)
```
ζ_Y = 亮度 pilot ŝ₁ 在 3×3 内的显著度 = Σ ŝ₁² / Σ σ_Y² (亮度不处理的层用 L_Y)
ζ_C = L_Cᵀ Σ_C,l⁻¹ L_C (马氏距离平方)
gate = max( smooth(ζ_Y; χ²_9 分位 τ_Y), smooth(ζ_C; χ²_2 分位 τ_C) )
out = gate · L_C
g_Y = smooth(ζ_Y; χ²_9 分位 τ_Y), g_C = smooth(ζ_C; χ²_2 分位 τ_C)
Ĉ = ŝ₁(p) · Σ_{5×5} ŝ₁ L_C / Σ_{5×5} (ŝ₁² + ε σ_Y²) (亮度回归出的色度, ε = chromaGuide)
out = f(SNR_p) · ( g_C · L_C + (1 − g_C) · g_Y · Ĉ ) (f 只作用于 l < chromaDetailFrom)
```
- 亮度边缘处的色度细节保留(真实颜色边界几乎总伴随亮度边界);
- 亮度边缘处保留的是与亮度相关的色度 `Ĉ`(真实颜色边界几乎总伴随亮度边界),不是带噪的 `L_C` 本身;
- 等亮度色边缘由色度自身马氏距离超阈保留(`τ_C` 即离群阈值,以卡方分位数表示);
- 其余细节清零,不做颗粒回填(`t_C = 0`)。
@@ -326,6 +327,10 @@ out = gate · L_C
代价是 L0 / L1 上不伴随亮度边界的颜色边缘不再单独保留。ISO 694 人像上,唇边、眼周的色度变化 rms 约为信号的 0.65%,1:1 并排看不出差别。
**亮度边缘的色度。** `ζ_Y` 在真实亮度边缘处全开,按 `gate · L_C` 会把边缘两侧的色度噪声整段放过(ISO 9100 白色充电器左缘暗侧,提亮后贴边一串红绿点)。改为在 5×5 内用 pilot 对色度带做回归 `Ĉ`,只保留随亮度变化的色度,`ε = chromaGuide = 2` 让亮度带只有噪声时回归系数趋零。贴边 8 px 条带内沿边方向的色度高通 rms:`gate · L_C` 0.0073 / 0.0044,加暗部衰减后 0.0041 / 0.0037,再换成 `Ĉ` 0.0030 / 0.0033;把 L0–L2 色度带直接清零的下限为 0.0025 / 0.0029,`ε` 取 1–4 结果相同。
**暗部的细层色度。** 带暗环的 4–8 px 红点同时有亮度跳变,在 L2 由 `ζ_Y` 放行,色度门从哪层开始都去不掉;手机全图缩放观看时正是这类点。旧 MGC 的色度后处理(3×3 亮度钳位、3×3 去极值色度均值、方向色度滤波、两级 IIR 低通)实际上丢掉了全部细尺度色度。这里只在暗部这样做:`l < chromaDetailFrom = 3` 的色度细节乘以 `f = smooth(SNR_p; chromaDetailSnr = 4, 10)`,`SNR_p = Y / σ_Y` 是单个源样本的亮度信噪比(§2.1 点协方差,不含 κ、残差与 §5.1 强度,各层相同)。同一张 ISO 9100 实拍:有彩点的暗区 `SNR_p` 中位 1.8–2.8,无彩点的中灰墙 5.4,亮部约 38;清零 L0–L2 色度后红点与淡彩斑全部消失。ISO 694 人像(`SNR_p` 中位 24)上与原输出相对差 0.01–0.06%。
### 7.3 粗层联合双边滤波
亮度门控只处理细节层;低频色斑存在于粗层的高斯层本身。对色度最粗的 2–3 层,在 `base_C` 上额外做亮度引导的联合双边滤波:
+4 -1
View File
@@ -56,6 +56,9 @@ def params_from_json(p: dict) -> pd.Params:
gate_chroma=tuple(p["gateChroma"]),
bilateral_levels=p["bilateralLevels"],
chroma_gate_from=p.get("chromaGateFrom", 0),
chroma_detail_from=p.get("chromaDetailFrom", 0),
chroma_detail_snr=tuple(p.get("chromaDetailSnr", (0.0, 0.0))),
chroma_guide=p.get("chromaGuide", 0.0),
bilateral_luma=p["bilateralLuma"],
bilateral_chroma=p["bilateralChroma"],
luma_strength=p["lumaStrength"],
@@ -201,7 +204,7 @@ def self_test() -> bool:
np.full(4, 0.2, dtype=F32), np.full(4, 2.5, dtype=F32), (16.919, 27.877), (5.991, 13.816),
2, 4.0, 4.0, 1.0, 1.0, lsc,
merge_map=pd.MergeMap(rng.uniform(0.1, 1.0, (3, 4, 2)).astype(F32), (0, 0), (w, h)))
params = replace(params, gamma=np.array([0.5, 0.5, 0.5, 1.0], dtype=F32), chroma_gate_from=2,
params = replace(params, gamma=np.array([0.5, 0.5, 0.5, 1.0], dtype=F32), chroma_gate_from=2, chroma_detail_from=3, chroma_detail_snr=(4.0, 10.0), chroma_guide=2.0,
strength_cap_luma=rng.uniform(0.5, 6.0, 4).astype(F32),
strength_cap_chroma=rng.uniform(0.01, 6.0, 4).astype(F32))
trace = pd.run(rgb, params)
+17 -2
View File
@@ -149,6 +149,9 @@ class Params:
merge_map: "MergeMap | None" = None # section 2.2 per-pixel ratios; None: the scalars
gamma: np.ndarray | None = None # [levels] section 6.1 second-stage gain exponent; None: 1 (MMSE)
chroma_gate_from: int = 0 # section 7.2 first level whose Mahalanobis gate is on
chroma_detail_from: int = 0 # section 7.2 levels below fade their chroma detail with the point luma SNR
chroma_detail_snr: tuple[float, float] = (0.0, 0.0)
chroma_guide: float = 0.0 # section 7.2 luma-noise regularisation of the guided chroma detail
def luma_grain(self, level: int) -> float:
t, s = float(self.grain[level]), self.luma_strength
@@ -538,9 +541,21 @@ def final(lap: np.ndarray, base: np.ndarray, noise: np.ndarray, pilot_tex: np.nd
F32(np.inf))
low, high = params.chroma_thresholds(params.gate_chroma)
gate_c = _smoothstep(low, high, chroma_stat) if level >= params.chroma_gate_from else np.zeros_like(chroma_stat)
gate = np.maximum(gate_y, gate_c)
# Section 7.2: where only luma structure opens the gate, the detail is the chroma the 5x5
# luma regression predicts, not the noisy band itself.
num = sum(_shift(gate_luma * lap[1:3], dy, dx) for dy in range(-2, 3) for dx in range(-2, 3))
den = sum(_shift(gate_luma * gate_luma + F32(params.chroma_guide) * noise_y, dy, dx)
for dy in range(-2, 3) for dx in range(-2, 3))
guided = np.where(den > 0, num / np.maximum(den, F32(1e-30)), F32(0)) * gate_luma
detail = gate_c * lap[1:3] + (1 - gate_c) * gate_y * guided
if level < params.chroma_detail_from:
# Section 7.2: point luma SNR of one source sample (level independent).
cov, _ = point_covariance(base, params, level)
snr = np.where(cov[0, 0] > 0, base[0] / np.sqrt(np.maximum(cov[0, 0], F32(1e-30))), F32(1e30))
detail = detail * _smoothstep(params.chroma_detail_snr[0], params.chroma_detail_snr[1], snr)
detail = np.where(noise_y > 0, detail, lap[1:3]) # clipped: no model, detail kept
s = F32(min(params.chroma_strength, 1.0))
chroma_band = (s * gate + (1 - s)) * lap[1:3]
chroma_band = s * detail + (1 - s) * lap[1:3]
base_chroma = base[1:3]
if params.chroma_strength > 0 and level >= params.levels - params.bilateral_levels: