image.vision\ocr\glyph_bank.mbt
96.4% · 81 / 84 points · 153 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 内建 5×7 字形库(S30) - 3
/// - 4
/// 模板匹配型 OCR 的天花板由**字库质量**决定,而不是距离度量。旧实现(`generate_char_template_simple`) - 5
/// 是 8×8 手绘简化形状,且多个字符共用一个形状(`"0"|"O"`、`"1"|"I"`、`"2"|"Z"`、`"4"|"A"`、 - 6
/// `"5"|"S"`、`"8"|"B"`),天生不可能区分——这就是 GAP-22 里 289/348 的来源。 - 7
/// - 8
/// 这里换成每个字符独立一份 5×7 点阵(0-9 + A-Z,小写折叠到大写),并提供与样本渲染器 - 9
/// (`image.core/testgen` 的 `render_letters`)同款的整数放大渲染,使模板与样本走 - 10
/// **同一条几何链路**:同字体 → 同放大 → 同裁剪 → 同特征。匹配的难点被还原成真实问题: - 11
/// 退化(缩放抖动、噪声、模糊)下的鲁棒性,而不是"模板根本没画对"。 - 12
- 13
///| - 14
/// 单个字符的 5×7 点阵:7 行,每行 5 bit(高位在左)。 - 15
/// - 16
/// 小写折叠到大写(模板匹配器默认 `case_sensitive=false`,分开建小写模板反而更容易互相抢)。 - 17
/// 不认识的字符返回 `None`,调用方据此拒绝建模板而不是塞一个空点阵进去。 - 18
pub fn glyph_rows(ch : Char) -> Array[Int32]? { - 19●
match ch { - 20●
'0' => Some([0b01110, 0b10011, 0b10101, 0b10101, 0b10101, 0b11001, 0b01110]) - 21●
'1' => Some([0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110]) - 22●
'2' => Some([0b01110, 0b10001, 0b00001, 0b00010, 0b00100, 0b01000, 0b11111]) - 23●
'3' => Some([0b11100, 0b00010, 0b00010, 0b01110, 0b10001, 0b10001, 0b01110]) - 24●
'4' => Some([0b00010, 0b00100, 0b01000, 0b11111, 0b00100, 0b00100, 0b00100]) - 25●
'5' => Some([0b11111, 0b10000, 0b10000, 0b11110, 0b00001, 0b00001, 0b01110]) - 26●
'6' => Some([0b00110, 0b01000, 0b10000, 0b10110, 0b10001, 0b10001, 0b01110]) - 27●
'7' => Some([0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b10000, 0b00000]) - 28●
'8' => Some([0b01110, 0b10001, 0b01110, 0b10001, 0b10001, 0b10001, 0b01110]) - 29●
'9' => Some([0b01110, 0b10001, 0b10001, 0b01110, 0b00001, 0b00010, 0b11100]) - 30●
'A' => Some([0b01110, 0b10001, 0b10001, 0b11111, 0b10001, 0b10001, 0b10001]) - 31●
'B' => Some([0b11110, 0b10001, 0b10001, 0b11110, 0b10001, 0b10001, 0b11110]) - 32●
'C' => Some([0b01110, 0b10001, 0b10000, 0b10000, 0b10000, 0b10001, 0b01110]) - 33●
'D' => Some([0b11100, 0b10010, 0b10001, 0b10001, 0b10001, 0b10010, 0b11100]) - 34●
'E' => Some([0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b11111]) - 35●
'F' => Some([0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b10000]) - 36●
'G' => Some([0b01110, 0b10001, 0b10000, 0b10111, 0b10001, 0b10001, 0b01111]) - 37●
'H' => Some([0b10001, 0b10001, 0b10001, 0b11111, 0b10001, 0b10001, 0b10001]) - 38●
'I' => Some([0b01110, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110]) - 39●
'J' => Some([0b00111, 0b00010, 0b00010, 0b00010, 0b00010, 0b10010, 0b01100]) - 40●
'K' => Some([0b10001, 0b10010, 0b10100, 0b11000, 0b10100, 0b10010, 0b10001]) - 41●
'L' => Some([0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b11111]) - 42●
'M' => Some([0b10001, 0b11011, 0b10101, 0b10101, 0b10001, 0b10001, 0b10001]) - 43●
'N' => Some([0b10001, 0b11001, 0b10101, 0b10011, 0b10001, 0b10001, 0b10001]) - 44●
'O' => Some([0b01110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110]) - 45●
'P' => Some([0b11110, 0b10001, 0b10001, 0b11110, 0b10000, 0b10000, 0b10000]) - 46●
'Q' => Some([0b01110, 0b10001, 0b10001, 0b10001, 0b10101, 0b10010, 0b01101]) - 47●
'R' => Some([0b11110, 0b10001, 0b10001, 0b11110, 0b10100, 0b10010, 0b10001]) - 48●
'S' => Some([0b01111, 0b10000, 0b10000, 0b01110, 0b00001, 0b00001, 0b11110]) - 49●
'T' => Some([0b11111, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100]) - 50●
'U' => Some([0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110]) - 51●
'V' => Some([0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01010, 0b00100]) - 52●
'W' => Some([0b10001, 0b10001, 0b10001, 0b10101, 0b10101, 0b11011, 0b10001]) - 53●
'X' => Some([0b10001, 0b10001, 0b01010, 0b00100, 0b01010, 0b10001, 0b10001]) - 54●
'Y' => Some([0b10001, 0b10001, 0b01010, 0b00100, 0b00100, 0b00100, 0b00100]) - 55●
'Z' => Some([0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b10000, 0b11111]) - 56●
c => { - 57●
let code = c.to_int() - 58
if code >= 97 && code <= 122 { - 59●
match (code - 32).to_char() { - 60●
Some(upper) => glyph_rows(upper) - 61○
None => None - 62
} - 63
} else { - 64●
None - 65
} - 66
} - 67
} - 68
} - 69
- 70
///| - 71
/// 字形是否在库内(0-9 / A-Z / a-z)。 - 72
pub fn glyph_known(ch : Char) -> Bool { - 73●
match glyph_rows(ch) { - 74●
Some(_) => true - 75●
None => false - 76
} - 77
} - 78
- 79
///| - 80
/// 把单个字符渲染成灰度图(白底 255 / 黑字 0),整数放大 `scale` 倍、四周留 `margin`。 - 81
/// - 82
/// 画法是**圆点**而不是实心方块:每个置位单元画成一个半径 `0.9 * scale` 的圆。 - 83
/// 理由是可分割性——5×7 点阵里 `A`、`N`、`Z` 这类字符靠**对角**笔画相连,方块渲染在放大后 - 84
/// 对角两格只有角接触,四邻域连通域会把它切成一堆碎片(实测 `A` 被切成 4 个"字符")。 - 85
/// 圆点使得对角中心距 `1.414s` < 半径和 `1.8s`,字形自然连通,分割才拿得回一个字符。 - 86
/// - 87
/// 几何仍是 `(5+2m)s × (7+2m)s`,与 `image.core/testgen` 的 `render_letters` 同尺寸。 - 88
pub fn render_glyph( - 89
ch : Char, - 90
scale~ : Int32 = 4, - 91
margin~ : Int32 = 4, - 92
) -> @types.Image? { - 93●
match glyph_rows(ch) { - 94●
None => None - 95●
Some(rows) => { - 96◐
let s = if scale < 1 { 1 } else { scale } - 97◐
let m = if margin < 0 { 0 } else { margin } - 98
let gw = 5 - 99
let gh = 7 - 100
let w = (gw + 2 * m) * s - 101
let h = (gh + 2 * m) * s - 102●
let data : Array[Byte] = Array::make(w * h, b'\x00') - 103
let r2 = 0.81 - 104
let ink = fn(gx : Int32, gy : Int32) -> Bool { - 105●
if gx < 0 || gy < 0 || gx >= gw || gy >= gh { - 106●
false - 107
} else { - 108●
((rows[gy] >> (gw - 1 - gx)) & 1) == 1 - 109
} - 110
} - 111
for y in 0..<h { - 112●
for x in 0..<w { - 113●
let gx = (x.to_double() + 0.5) / s.to_double() - m.to_double() - 114●
let gy = (y.to_double() + 0.5) / s.to_double() - m.to_double() - 115●
let ix = gx.floor().to_int() - 116●
let iy = gy.floor().to_int() - 117
let mut hit = false - 118
// 半径 0.9 只可能盖到 3×3 邻域里的单元,查 9 格足够 - 119
let mut dy = -1 - 120
while dy <= 1 && !hit { - 121●
let mut dx = -1 - 122
while dx <= 1 && !hit { - 123●
if ink(ix + dx, iy + dy) { - 124●
let ox = gx - (ix + dx).to_double() - 0.5 - 125●
let oy = gy - (iy + dy).to_double() - 0.5 - 126
if ox * ox + oy * oy <= r2 { - 127●
hit = true - 128
} - 129
} - 130
dx += 1 - 131
} - 132
dy += 1 - 133
} - 134
if hit { - 135●
data[y * w + x] = b'\xFF' - 136
} - 137
} - 138
} - 139
Some(@types.Image::{ - 140
width: w, - 141
height: h, - 142
channels: 1, - 143●
data: Bytes::from_array(data), - 144
}) - 145
} - 146
} - 147
} - 148
- 149
///| - 150
/// 库容量:0-9 + A-Z,共 36 类(小写折叠)。 - 151
pub fn glyph_count() -> Int32 { - 152●
36 - 153
}