feat: refine BM3D denoising using sensor noise profiles and adaptive sigma scaling
This commit is contained in:
1 parent
c7efb32f52
commit
110e2905ac
2 files changed
+55
-36
No files matched your search
@@ -53,10 +53,8 @@ object BM3DShaders {
|
||||
yuv.x - 0.3441 * cb - 0.7141 * cr,
|
||||
yuv.x + 1.772 * cb);
|
||||
}
|
||||
float noiseScaleForLuma(float luma) {
|
||||
float x = max(luma, 0.001);
|
||||
float snr = x / sqrt(max(uNoiseModel.x * x + uNoiseModel.y, 1e-10));
|
||||
return 1.0 / snr;
|
||||
float noiseSigmaForLuma(float luma) {
|
||||
return sqrt(max(uNoiseModel.x * max(luma, 0.0) + uNoiseModel.y, 1e-10));
|
||||
}
|
||||
|
||||
void main() {
|
||||
@@ -69,7 +67,7 @@ object BM3DShaders {
|
||||
|
||||
// Chroma noise is low-frequency → large spatial step scale
|
||||
float stepScale = 6.5;
|
||||
float localH = uH * noiseScaleForLuma(yuv.x);
|
||||
float localH = uH * noiseSigmaForLuma(yuv.x);
|
||||
float invChromaH2 = 1.0 / max((localH * 1.5) * (localH * 1.5), 1e-6);
|
||||
float invLumaH2 = 1.0 / max((localH * 0.5) * (localH * 0.5), 1e-6);
|
||||
|
||||
@@ -137,10 +135,8 @@ object BM3DShaders {
|
||||
float gate = 1.0 - smoothstep(t * 0.35, t * 1.55, ssd);
|
||||
return soft * gate;
|
||||
}
|
||||
float noiseScaleForLuma(float luma) {
|
||||
float x = max(luma, 0.001);
|
||||
float snr = x / sqrt(max(uNoiseModel.x * x + uNoiseModel.y, 1e-10));
|
||||
return 1.0 / snr;
|
||||
float noiseSigmaForLuma(float luma) {
|
||||
return sqrt(max(uNoiseModel.x * max(luma, 0.0) + uNoiseModel.y, 1e-10));
|
||||
}
|
||||
|
||||
void main() {
|
||||
@@ -161,7 +157,7 @@ object BM3DShaders {
|
||||
float c20 = dot(texture(uInputTexture, vTexCoord + vec2( 1.0,-1.0)*T).rgb, LW);
|
||||
float c21 = dot(texture(uInputTexture, vTexCoord + vec2( 1.0, 0.0)*T).rgb, LW);
|
||||
float c22 = dot(texture(uInputTexture, vTexCoord + vec2( 1.0, 1.0)*T).rgb, LW);
|
||||
float localH = uH * noiseScaleForLuma(c11);
|
||||
float localH = uH * noiseSigmaForLuma(c11);
|
||||
|
||||
// BM3D hard threshold: τ = factor × σ² × patch_size
|
||||
// factor 2.7 is the standard BM3D constant, tuned here for 3×3 patches
|
||||
@@ -283,10 +279,8 @@ object BM3DShaders {
|
||||
float gate = 1.0 - smoothstep(t * 0.35, t * 1.55, ssd);
|
||||
return soft * gate;
|
||||
}
|
||||
float noiseScaleForLuma(float luma) {
|
||||
float x = max(luma, 0.001);
|
||||
float snr = x / sqrt(max(uNoiseModel.x * x + uNoiseModel.y, 1e-10));
|
||||
return 1.0 / snr;
|
||||
float noiseSigmaForLuma(float luma) {
|
||||
return sqrt(max(uNoiseModel.x * max(luma, 0.0) + uNoiseModel.y, 1e-10));
|
||||
}
|
||||
float wideGuidedLumaBase(vec2 uv, vec2 texelSize, float centerGuideLuma, float sigma_n2) {
|
||||
float sumY = 0.0;
|
||||
@@ -336,9 +330,9 @@ object BM3DShaders {
|
||||
float c21 = dot(texture(uBasicTexture, vTexCoord + vec2( 1.0, 0.0)*T).rgb, LW);
|
||||
float c22 = dot(texture(uBasicTexture, vTexCoord + vec2( 1.0, 1.0)*T).rgb, LW);
|
||||
|
||||
// Sensor noise model: variance = S * luma + O, normalized so uH keeps
|
||||
// its existing mid-gray strength while local denoise follows brightness.
|
||||
float localH = uH * noiseScaleForLuma(c11);
|
||||
// Sensor noise model in this pass' linear-light domain:
|
||||
// variance = S * luma + O. uH is a dimensionless strength multiplier.
|
||||
float localH = uH * noiseSigmaForLuma(c11);
|
||||
float sigma_n2 = localH * localH;
|
||||
float threshold = sigma_n2 * 9.0 * 2.7; // same τ as Pass 1, adapted by luma
|
||||
|
||||
|
||||
@@ -137,6 +137,8 @@ class RawDemosaicProcessor {
|
||||
private const val TAG = "RawDemosaicProcessor"
|
||||
private const val RAW_HDR_HIGHLIGHT_START = 0.72f
|
||||
private const val RAW_HDR_WHITE_POINT_SCENE_LUMA = 2.4f
|
||||
private const val BM3D_SIGMA_STRENGTH_AT_SLIDER_ONE = 5.0f
|
||||
private const val BM3D_REFERENCE_MID_GRAY_SIGMA = 0.012f
|
||||
|
||||
init {
|
||||
// 加载 JNI 库
|
||||
@@ -657,6 +659,7 @@ class RawDemosaicProcessor {
|
||||
width = actualWidth,
|
||||
height = actualHeight,
|
||||
metadata = actualMetadata,
|
||||
linearExposureGain = computeLinearExposureGain(actualMetadata, effectiveExposureCompensation, resolvedDcpRenderPlan),
|
||||
denoiseValue = denoiseValue,
|
||||
chromaDenoiseValue = denoiseValue,
|
||||
)
|
||||
@@ -1134,6 +1137,7 @@ class RawDemosaicProcessor {
|
||||
width: Int,
|
||||
height: Int,
|
||||
metadata: RawMetadata,
|
||||
linearExposureGain: Float,
|
||||
denoiseValue: Float?,
|
||||
chromaDenoiseValue: Float?,
|
||||
) {
|
||||
@@ -1147,26 +1151,24 @@ class RawDemosaicProcessor {
|
||||
val texelW = 1.0f / width
|
||||
val texelH = 1.0f / height
|
||||
|
||||
// 动态 NLM 参数计算
|
||||
// 增益由三部分组成:传感器 ISO、ISP 数字增益、后期 Tonemap 增益
|
||||
// Camera2 SENSOR_NOISE_PROFILE is defined in the linear RAW domain.
|
||||
// Convert it into the linear RGB texture domain consumed by BM3D, so
|
||||
// the user strength remains a dimensionless multiplier of measured sigma.
|
||||
val isoGain = metadata.iso / 100.0f
|
||||
val digitalGain = metadata.postRawSensitivityBoost
|
||||
val postGain = ExposureNormalization.compute(metadata)
|
||||
val totalGain = (isoGain * digitalGain * postGain).coerceAtLeast(0f)
|
||||
|
||||
var (s, o) = resolveDenoiseNoiseModel(metadata, totalGain)
|
||||
|
||||
if (s <= 0 || o <= 0) {
|
||||
s = 1E-4f * totalGain
|
||||
o = 4.5E-7f * sqrt(totalGain)
|
||||
}
|
||||
val fallbackGain = (isoGain * digitalGain).coerceAtLeast(1f)
|
||||
val (s, o) = resolveDenoiseNoiseModel(metadata, linearExposureGain, fallbackGain)
|
||||
|
||||
val denoiseValue = denoiseValue ?: 0f
|
||||
val chromaDenoiseValue = chromaDenoiseValue ?: 0f
|
||||
|
||||
val h = denoiseValue * denoiseValue
|
||||
val ch = chromaDenoiseValue * chromaDenoiseValue + 0.5f
|
||||
PLog.d(TAG, "Dynamic BM3D: s=$s o=$o, h=$h ch=$ch iso=${metadata.iso}")
|
||||
val noiseAdaptiveScale = computeDenoiseStrengthScale(s, o)
|
||||
val h = denoiseValue.coerceAtLeast(0f) * BM3D_SIGMA_STRENGTH_AT_SLIDER_ONE * noiseAdaptiveScale
|
||||
val ch = chromaDenoiseValue.coerceAtLeast(0f) * BM3D_SIGMA_STRENGTH_AT_SLIDER_ONE * noiseAdaptiveScale + 0.2f
|
||||
PLog.d(
|
||||
TAG,
|
||||
"Dynamic BM3D: s=$s o=$o, h=$h ch=$ch scale=$noiseAdaptiveScale iso=${metadata.iso} linearGain=$linearExposureGain"
|
||||
)
|
||||
|
||||
val identityMatrix = FloatArray(16)
|
||||
GlMatrix.setIdentityM(identityMatrix, 0)
|
||||
@@ -1230,16 +1232,29 @@ class RawDemosaicProcessor {
|
||||
checkGlError("renderNLMDenoise")
|
||||
}
|
||||
|
||||
private fun resolveDenoiseNoiseModel(metadata: RawMetadata, totalGain: Float): Pair<Float, Float> {
|
||||
private fun resolveDenoiseNoiseModel(
|
||||
metadata: RawMetadata,
|
||||
linearExposureGain: Float,
|
||||
fallbackGain: Float
|
||||
): Pair<Float, Float> {
|
||||
var s = metadata.noiseProfile.getOrElse(0) { 0f }
|
||||
var o = metadata.noiseProfile.getOrElse(1) { 0f }
|
||||
|
||||
if (s <= 0f || o <= 0f) {
|
||||
s = 1E-4f * totalGain
|
||||
o = 4.5E-7f * sqrt(totalGain)
|
||||
s = 1E-4f * fallbackGain
|
||||
o = 4.5E-7f * sqrt(fallbackGain)
|
||||
}
|
||||
|
||||
return s.coerceAtLeast(1e-10f) to o.coerceAtLeast(1e-10f)
|
||||
val frameNoiseScale = 1f / metadata.frameCount.coerceAtLeast(1).toFloat()
|
||||
val gain = linearExposureGain.coerceAtLeast(1e-6f)
|
||||
val transformedS = s * frameNoiseScale * gain
|
||||
val transformedO = o * frameNoiseScale * gain * gain
|
||||
return transformedS.coerceAtLeast(1e-10f) to transformedO.coerceAtLeast(1e-10f)
|
||||
}
|
||||
|
||||
private fun computeDenoiseStrengthScale(s: Float, o: Float): Float {
|
||||
val midGraySigma = sqrt((s * 0.18f + o).coerceAtLeast(1e-10f))
|
||||
return (midGraySigma / BM3D_REFERENCE_MID_GRAY_SIGMA).coerceIn(1f, 2.8f)
|
||||
}
|
||||
|
||||
private fun dhtSetCommonUniforms(program: Int, metadata: RawMetadata) {
|
||||
@@ -2182,7 +2197,7 @@ class RawDemosaicProcessor {
|
||||
TAG,
|
||||
"compute: normalizationGain=$normalizationGain baseline=${metadata.baselineExposure} dcpBaselineOffset=$dcpBaselineExposureOffset"
|
||||
)
|
||||
val exposureGain = normalizationGain * 2.0f.pow(rawExposureCompensation + dcpBaselineExposureOffset)
|
||||
val exposureGain = computeLinearExposureGain(metadata, rawExposureCompensation, dcpRenderPlan)
|
||||
GLES30.glUniform1f(GLES30.glGetUniformLocation(linearProgram, "uExposureGain"), exposureGain)
|
||||
val (blackLevel, whiteLevel) = resolveLinearInputLevels(
|
||||
metadata = metadata,
|
||||
@@ -2200,6 +2215,16 @@ class RawDemosaicProcessor {
|
||||
drawQuad(linearProgram)
|
||||
}
|
||||
|
||||
private fun computeLinearExposureGain(
|
||||
metadata: RawMetadata,
|
||||
rawExposureCompensation: Float,
|
||||
dcpRenderPlan: DcpRenderPlan?
|
||||
): Float {
|
||||
val normalizationGain = ExposureNormalization.compute(metadata)
|
||||
val dcpBaselineExposureOffset = dcpRenderPlan?.baselineExposureOffset ?: 0f
|
||||
return normalizationGain * 2.0f.pow(rawExposureCompensation + dcpBaselineExposureOffset)
|
||||
}
|
||||
|
||||
private suspend fun resolveRawAutoExposureEv(
|
||||
context: Context,
|
||||
metadata: RawMetadata,
|
||||
|
||||
Reference in new issue
Block a user