image.pro\recipe\doc_scan.mbt

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