image.pro\recipe\doc_scan.mbt
66% · 35 / 53 points · 157 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # doc-scan:文档拍照增强 - 3
/// - 4
/// 手机拍纸张的典型退化是「光照不均 + 低对比 + 轻微噪声」,文字本身并不糊。 - 5
/// 所以链路不是猛锐化,而是:灰度 → CLAHE 把局部光照拉平 → 局部自适应二值化把墨/纸切开。 - 6
/// - 7
/// 验收口径用可核查的数,而不是"看起来清楚":`count_text_lines` 对原图与产图各数一次 - 8
/// 文本行(行投影上连续墨行段),`steps` 里记 `before` / `after`,调用方直接比。 - 9
- 10
///| - 11
/// 取按 BT.709 近似的灰度值(1 通道直接返回)。 - 12
fn ds_gray(img : @types.Image, x : Int32, y : Int32) -> Int32 { - 13●
let ch = img.channels - 14
let base = (y * img.width + x) * ch - 15
if ch == 1 { - 16●
return img.data[base].to_int() - 17
} - 18○
let r = img.data[base].to_int() - 19○
let g = img.data[base + 1].to_int() - 20○
let b = img.data[base + 2].to_int() - 21
(r * 77 + g * 150 + b * 29) / 256 - 22
} - 23
- 24
///| - 25
/// 数文本行:某行暗像素(<128)达到宽度 1/4 记为「墨行」,连续墨行段高 ≥3 算一行。 - 26
/// - 27
/// 采样步长 2——行计数只要行间结构,不必逐像素;3000×4000 的照片也只扫 1/4 的点。 - 28
pub fn count_text_lines(img : @types.Image) -> Int32 { - 29●
let w = img.width - 30
let h = img.height - 31
if w <= 0 || h <= 0 { - 32○
return 0 - 33
} - 34
let cols = (w + 1) / 2 - 35◐
let need = if cols / 4 > 2 { cols / 4 } else { 2 } - 36
let mut lines = 0 - 37
let mut run = 0 - 38
for y in 0..<h { - 39●
let mut ink = 0 - 40
let mut x = 0 - 41
while x < w { - 42●
if ds_gray(img, x, y) < 128 { - 43●
ink = ink + 1 - 44
} - 45
x = x + 2 - 46
} - 47
if ink >= need { - 48●
run = run + 1 - 49
} else { - 50●
if run >= 3 { - 51●
lines = lines + 1 - 52
} - 53
run = 0 - 54
} - 55
} - 56
if run >= 3 { - 57●
lines = lines + 1 - 58
} - 59
lines - 60
} - 61
- 62
///| - 63
/// doc-scan Recipe:读 → 灰度 → CLAHE 去光照 → 自适应高斯二值化 → 编码。 - 64
/// - 65
/// 输出固定 1 通道(黑白文本图),适合再给 OCR;`longest` 会在二值化之后按长边缩一次, - 66
/// 让「先增强再缩」而不是「先缩再增强」——后者会把细笔画先糊掉。 - 67
fn recipe_doc_scan( - 68
data : Bytes, - 69
opts : RecipeOpts, - 70
) -> RecipeResult raise @types.ImageError { - 71●
let steps : Array[StepRec] = [] - 72●
let img = step_decode(data, steps) - 73●
let before = count_text_lines(img) - 74●
let gray = @color.to_grayscale(img) catch { - 75○
e => raise @types.image_error_from_load(e) - 76
} - 77●
steps.push({ - 78
name: "grayscale", - 79
note: "", - 80
width: gray.width, - 81
height: gray.height, - 82
bytes: -1, - 83●
ms: seg_ms(), - 84
}) - 85●
let flat = @color.clahe(gray, 8, 2.0) catch { - 86○
e => raise @types.image_error_from_load(e) - 87
} - 88●
steps.push({ - 89
name: "flatten", - 90
note: "clahe tile=8 clip=2", - 91
width: flat.width, - 92
height: flat.height, - 93
bytes: -1, - 94●
ms: seg_ms(), - 95
}) - 96●
let bin = @color.adaptive_threshold_gaussian(flat, 15, 10) catch { - 97○
e => raise @types.image_error_from_load(e) - 98
} - 99●
steps.push({ - 100
name: "binarize", - 101
note: "gaussian block=15 c=10", - 102
width: bin.width, - 103
height: bin.height, - 104
bytes: -1, - 105●
ms: seg_ms(), - 106
}) - 107●
let after = count_text_lines(bin) - 108●
steps.push({ - 109
name: "text-lines", - 110
note: "before=\{before} after=\{after}", - 111
width: bin.width, - 112
height: bin.height, - 113
bytes: -1, - 114●
ms: seg_ms(), - 115
}) - 116
let mut cur = bin - 117●
let (tw, th) = fit_longest(cur, opts.longest) - 118
if tw != cur.width || th != cur.height { - 119○
cur = @transform.resize_to_contain( - 120
cur, - 121
tw, - 122
th, - 123○
Array::make(1, (255 : Byte)), - 124
) catch { - 125○
e => raise @types.image_error_from_load(e) - 126
} - 127○
steps.push({ - 128
name: "resize", - 129
note: "", - 130
width: cur.width, - 131
height: cur.height, - 132
bytes: -1, - 133○
ms: seg_ms(), - 134
}) - 135
} - 136●
let fmt0 = pick_format(cur, opts.format) - 137●
let (bytes, q0) = encode_fit(cur, fmt0, opts.quality, opts.max_bytes) - 138●
steps.push({ - 139
name: "encode", - 140
note: "quality=\{q0}", - 141
width: cur.width, - 142
height: cur.height, - 143●
bytes: bytes.length().to_int(), - 144●
ms: seg_ms(), - 145
}) - 146
{ - 147
recipe: "doc-scan", - 148
image: cur, - 149
bytes, - 150
format: fmt0, - 151
steps, - 152
quality: q0, - 153
redacted: 0, - 154
blurred: false, - 155
codes: [], - 156
} - 157
}