修复文字糊成一团:用gamma曲线代替线性分段LUT

线性分段把0-210全压到0-30,笔画间灰度差异丢失导致糊成墨团。
改用gamma=2.5的幂曲线,文字整体变黑但保留笔画间的灰度渐变,
文字清晰可辨。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-19 17:25:43 +08:00
parent c51bbc18e8
commit b99ec63306

View File

@@ -366,22 +366,16 @@ public static class DocumentScanner
Mat normU8 = new Mat(h, w, MatType.CV_8U); Mat normU8 = new Mat(h, w, MatType.CV_8U);
Marshal.Copy(resultData, 0, normU8.Data, resultData.Length); Marshal.Copy(resultData, 0, normU8.Data, resultData.Length);
// --- d: 非线性对比度增强(让文字更黑)--- // --- d: 非线性对比度增强 ---
// 用 LUT 做分段映射: // 用 gamma 曲线代替线性分段,保留文字笔画间的灰度层次
// 0-210: 线性映射到 0-30文字区域强力压暗 // gamma > 1中间调压暗文字变黑但保留灰度渐变不糊成一团
// 210-255: 线性映射到 30-255背景区域拉亮
byte[] lut = new byte[256]; byte[] lut = new byte[256];
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
{ {
if (i <= 210) double x = i / 255.0;
{ // gamma=2.5 让文字明显变黑,但笔画间灰度差异保留
lut[i] = (byte)(i * 30 / 210); double y = Math.Pow(x, 2.5);
} lut[i] = (byte)Math.Min(255, Math.Max(0, (int)(y * 255.0)));
else
{
lut[i] = (byte)(30 + (i - 210) * 225 / 45);
}
if (lut[i] > 255) lut[i] = 255;
} }
Mat lutMat = new Mat(1, 256, MatType.CV_8U, lut); Mat lutMat = new Mat(1, 256, MatType.CV_8U, lut);
Mat contrasted = new Mat(); Mat contrasted = new Mat();