image.vision\ocr\document_enhance.mbt

85.6% · 101 / 118 points · 344 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/// # 文档增强 ocr/document_enhance
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 文档图像增强
  7. 7// 文档增强、阴影去除、背景归一化
  8. 8
  9. 9///|
  10. 10/// 文档图像增强(去噪+对比度+锐化)
  11. 11/// 适用于扫描文档和照片文档的预处理
  12. 12pub fn document_enhance(
  13. 13 img : @types.Image,
  14. 14) -> @types.Image raise @types.LoadError {
  15. 15 // 输入验证
  16. 16● if img.width <= 0 || img.height <= 0 {
  17. 17● raise @types.LoadError::DecodeFailed(
  18. 18 "document_enhance: 图像尺寸必须大于0",
  19. 19 )
  20. 20 }
  21. 21 if img.channels < 1 || img.channels > 4 {
  22. 22● raise @types.LoadError::DecodeFailed(
  23. 23 "document_enhance: 通道数必须在1-4之间",
  24. 24 )
  25. 25 }
  26. 26 let expected_size = img.width * img.height * img.channels
  27. 27● if img.data.length() != expected_size {
  28. 28○ raise @types.LoadError::DecodeFailed(
  29. 29 "document_enhance: 数据大小不匹配,期望 " +
  30. 30○ expected_size.to_string() +
  31. 31 ",实际 " +
  32. 32○ img.data.length().to_string(),
  33. 33 )
  34. 34 }
  35. 35 let mut result = img
  36. 36 // 1. 转灰度(如果是彩色图像)
  37. 37 if result.channels != 1 {
  38. 38● result = to_grayscale_doc(result)
  39. 39 }
  40. 40 // 2. 对比度增强(拉伸直方图)
  41. 41● result = contrast_stretch_doc(result)
  42. 42 // 3. 轻度去噪(中值滤波)
  43. 43● result = median_denoise_doc(result, 3)
  44. 44 // 4. 锐化(增强文字边缘)
  45. 45● result = sharpen_doc(result, 0.5)
  46. 46 result
  47. 47}
  48. 48
  49. 49///|
  50. 50/// 转灰度(文档专用,BT.601)
  51. 51fn to_grayscale_doc(img : @types.Image) -> @types.Image {
  52. 52● if img.channels == 1 {
  53. 53○ return img
  54. 54 }
  55. 55 let n = img.width * img.height
  56. 56 let ch = img.channels
  57. 57● let out : Array[Byte] = Array::make(n, b'\x00')
  58. 58 let data = img.data
  59. 59 for i in 0..<n {
  60. 60● let base = i * ch
  61. 61● let r = data[base].to_int()
  62. 62● let g = data[base + 1].to_int()
  63. 63● let b = data[base + 2].to_int()
  64. 64 let gray = (r * 299 + g * 587 + b * 114) / 1000
  65. 65● out[i] = gray.to_byte()
  66. 66 }
  67. 67 @types.Image::{
  68. 68 width: img.width,
  69. 69 height: img.height,
  70. 70 channels: 1,
  71. 71● data: Bytes::from_array(out),
  72. 72 }
  73. 73}
  74. 74
  75. 75///|
  76. 76/// 对比度拉伸(文档专用)
  77. 77fn contrast_stretch_doc(img : @types.Image) -> @types.Image {
  78. 78● let n = img.width * img.height
  79. 79 let data = img.data
  80. 80 // 计算最小和最大值(忽略 1% 极值)
  81. 81● let histogram : Array[Int32] = Array::make(256, 0)
  82. 82 for i in 0..<n {
  83. 83● histogram[data[i].to_int()] += 1
  84. 84 }
  85. 85 let cutoff = n / 100
  86. 86 let mut count = 0
  87. 87 let mut min_val = 0
  88. 88 for i in 0..<256 {
  89. 89● count += histogram[i]
  90. 90 if count >= cutoff {
  91. 91● min_val = i
  92. 92 break
  93. 93 }
  94. 94 }
  95. 95 count = 0
  96. 96 let mut max_val = 255
  97. 97 for i in 0..<256 {
  98. 98● count += histogram[255 - i]
  99. 99 if count >= cutoff {
  100. 100● max_val = 255 - i
  101. 101 break
  102. 102 }
  103. 103 }
  104. 104 // 应用拉伸
  105. 105● let range = if max_val > min_val { max_val - min_val } else { 1 }
  106. 106● let out : Array[Byte] = Array::make(n, b'\x00')
  107. 107 for i in 0..<n {
  108. 108● let v = data[i].to_int()
  109. 109 let stretched = (v - min_val) * 255 / range
  110. 110● out[i] = @types.clamp_byte(stretched)
  111. 111 }
  112. 112 @types.Image::{
  113. 113 width: img.width,
  114. 114 height: img.height,
  115. 115 channels: 1,
  116. 116● data: Bytes::from_array(out),
  117. 117 }
  118. 118}
  119. 119
  120. 120///|
  121. 121/// 中值去噪(文档专用,3x3 窗口)
  122. 122fn median_denoise_doc(img : @types.Image, size : Int32) -> @types.Image {
  123. 123● let w = img.width
  124. 124 let h = img.height
  125. 125 let radius = size / 2
  126. 126 let data = img.data
  127. 127● let out : Array[Byte] = Array::make(w * h, b'\x00')
  128. 128 for y in 0..<h {
  129. 129● for x in 0..<w {
  130. 130● let values : Array[Int32] = []
  131. 131 for dy in -radius..<(radius + 1) {
  132. 132● for dx in -radius..<(radius + 1) {
  133. 133● let nx = x + dx
  134. 134 let ny = y + dy
  135. 135 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  136. 136● values.push(data[ny * w + nx].to_int())
  137. 137 }
  138. 138 }
  139. 139 }
  140. 140 // 简单排序取中值
  141. 141● for i in 0..<values.length() {
  142. 142● for j in (i + 1)..<values.length() {
  143. 143● if values[j] < values[i] {
  144. 144● let tmp = values[i]
  145. 145 values[i] = values[j]
  146. 146 values[j] = tmp
  147. 147 }
  148. 148 }
  149. 149 }
  150. 150● let median = values[values.length() / 2]
  151. 151● out[y * w + x] = median.to_byte()
  152. 152 }
  153. 153 }
  154. 154 @types.Image::{
  155. 155 width: w,
  156. 156 height: h,
  157. 157 channels: 1,
  158. 158● data: Bytes::from_array(out),
  159. 159 }
  160. 160}
  161. 161
  162. 162///|
  163. 163/// 锐化(文档专用,USM 简化版)
  164. 164fn sharpen_doc(img : @types.Image, amount : Double) -> @types.Image {
  165. 165● let w = img.width
  166. 166 let h = img.height
  167. 167 let data = img.data
  168. 168● let out : Array[Byte] = Array::make(w * h, b'\x00')
  169. 169 for y in 0..<h {
  170. 170● for x in 0..<w {
  171. 171● let center = data[y * w + x].to_int().to_double()
  172. 172 // 计算邻域均值
  173. 173 let mut sum = 0.0
  174. 174 let mut count = 0
  175. 175 for dy in -1..<2 {
  176. 176● for dx in -1..<2 {
  177. 177● let nx = x + dx
  178. 178 let ny = y + dy
  179. 179 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  180. 180● sum += data[ny * w + nx].to_int().to_double()
  181. 181 count += 1
  182. 182 }
  183. 183 }
  184. 184 }
  185. 185● let mean = sum / count.to_double()
  186. 186 // USM: result = center + amount * (center - mean)
  187. 187 let sharpened = center + amount * (center - mean)
  188. 188● out[y * w + x] = @types.clamp_byte(sharpened.to_int())
  189. 189 }
  190. 190 }
  191. 191 @types.Image::{
  192. 192 width: w,
  193. 193 height: h,
  194. 194 channels: 1,
  195. 195● data: Bytes::from_array(out),
  196. 196 }
  197. 197}
  198. 198
  199. 199///|
  200. 200/// 阴影去除(基于大核高斯模糊的背景估计)
  201. 201/// 通过估计背景光照并去除,实现阴影去除
  202. 202pub fn shadow_removal(
  203. 203 img : @types.Image,
  204. 204 kernel_size~ : Int32 = 51,
  205. 205) -> @types.Image raise @types.LoadError {
  206. 206 // 输入验证
  207. 207● if img.width <= 0 || img.height <= 0 {
  208. 208● raise @types.LoadError::DecodeFailed(
  209. 209 "shadow_removal: 图像尺寸必须大于0",
  210. 210 )
  211. 211 }
  212. 212 if img.channels != 1 {
  213. 213○ raise @types.LoadError::DecodeFailed(
  214. 214 "shadow_removal: 仅支持单通道灰度图",
  215. 215 )
  216. 216 }
  217. 217 let expected_size = img.width * img.height
  218. 218● if img.data.length() != expected_size {
  219. 219○ raise @types.LoadError::DecodeFailed(
  220. 220 "shadow_removal: 数据大小不匹配,期望 " +
  221. 221○ expected_size.to_string() +
  222. 222 ",实际 " +
  223. 223○ img.data.length().to_string(),
  224. 224 )
  225. 225 }
  226. 226 if kernel_size <= 0 || kernel_size % 2 == 0 {
  227. 227○ raise @types.LoadError::DecodeFailed(
  228. 228 "shadow_removal: 核大小必须为正奇数",
  229. 229 )
  230. 230 }
  231. 231 let w = img.width
  232. 232 let h = img.height
  233. 233 let data = img.data
  234. 234 // 估计背景(大核均值模糊)
  235. 235 let radius = kernel_size / 2
  236. 236● let background : Array[Int32] = Array::make(w * h, 0)
  237. 237 for y in 0..<h {
  238. 238● for x in 0..<w {
  239. 239● let mut sum = 0
  240. 240 let mut count = 0
  241. 241 for dy in -radius..<(radius + 1) {
  242. 242● for dx in -radius..<(radius + 1) {
  243. 243● let nx = x + dx
  244. 244 let ny = y + dy
  245. 245 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  246. 246● sum += data[ny * w + nx].to_int()
  247. 247 count += 1
  248. 248 }
  249. 249 }
  250. 250 }
  251. 251 background[y * w + x] = sum / count
  252. 252 }
  253. 253 }
  254. 254 // 去除阴影:result = (original / background) * 255
  255. 255● let out : Array[Byte] = Array::make(w * h, b'\x00')
  256. 256 for i in 0..<(w * h) {
  257. 257● let orig = data[i].to_int()
  258. 258 let bg = background[i]
  259. 259◐ let result = if bg > 0 { orig * 255 / bg } else { 255 }
  260. 260● out[i] = @types.clamp_byte(result)
  261. 261 }
  262. 262 @types.Image::{
  263. 263 width: w,
  264. 264 height: h,
  265. 265 channels: 1,
  266. 266● data: Bytes::from_array(out),
  267. 267 }
  268. 268}
  269. 269
  270. 270///|
  271. 271/// 背景归一化(将背景统一为白色)
  272. 272/// 适用于背景不均匀的文档图像
  273. 273pub fn background_normalization(
  274. 274 img : @types.Image,
  275. 275 kernel_size~ : Int32 = 31,
  276. 276) -> @types.Image raise @types.LoadError {
  277. 277 // 输入验证
  278. 278● if img.width <= 0 || img.height <= 0 {
  279. 279● raise @types.LoadError::DecodeFailed(
  280. 280 "background_normalization: 图像尺寸必须大于0",
  281. 281 )
  282. 282 }
  283. 283 if img.channels != 1 {
  284. 284● raise @types.LoadError::DecodeFailed(
  285. 285 "background_normalization: 仅支持单通道灰度图",
  286. 286 )
  287. 287 }
  288. 288 let expected_size = img.width * img.height
  289. 289● if img.data.length() != expected_size {
  290. 290○ raise @types.LoadError::DecodeFailed(
  291. 291 "background_normalization: 数据大小不匹配,期望 " +
  292. 292○ expected_size.to_string() +
  293. 293 ",实际 " +
  294. 294○ img.data.length().to_string(),
  295. 295 )
  296. 296 }
  297. 297 if kernel_size <= 0 || kernel_size % 2 == 0 {
  298. 298○ raise @types.LoadError::DecodeFailed(
  299. 299 "background_normalization: 核大小必须为正奇数",
  300. 300 )
  301. 301 }
  302. 302 let w = img.width
  303. 303 let h = img.height
  304. 304 let data = img.data
  305. 305 // 估计背景(大核中值模糊)
  306. 306 let radius = kernel_size / 2
  307. 307● let background : Array[Int32] = Array::make(w * h, 0)
  308. 308 for y in 0..<h {
  309. 309● for x in 0..<w {
  310. 310● let values : Array[Int32] = []
  311. 311 for dy in -radius..<(radius + 1) {
  312. 312● for dx in -radius..<(radius + 1) {
  313. 313● let nx = x + dx
  314. 314 let ny = y + dy
  315. 315 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  316. 316● values.push(data[ny * w + nx].to_int())
  317. 317 }
  318. 318 }
  319. 319 }
  320. 320 // 取最大值作为背景估计(文字是暗的,背景是亮的)
  321. 321 let mut max_val = 0
  322. 322 for v in values {
  323. 323● if v > max_val {
  324. 324● max_val = v
  325. 325 }
  326. 326 }
  327. 327 background[y * w + x] = max_val
  328. 328 }
  329. 329 }
  330. 330 // 归一化:result = original * (255 / background)
  331. 331● let out : Array[Byte] = Array::make(w * h, b'\x00')
  332. 332 for i in 0..<(w * h) {
  333. 333● let orig = data[i].to_int()
  334. 334 let bg = background[i]
  335. 335● let result = if bg > 0 { orig * 255 / bg } else { 255 }
  336. 336● out[i] = @types.clamp_byte(result)
  337. 337 }
  338. 338 @types.Image::{
  339. 339 width: w,
  340. 340 height: h,
  341. 341 channels: 1,
  342. 342● data: Bytes::from_array(out),
  343. 343 }
  344. 344}