image.vision\ocr\glyph_bank.mbt

96.4% · 81 / 84 points · 153 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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