image.vision\ocr\ocr_performance.mbt

82.3% · 102 / 124 points · 380 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 性能 ocr/ocr_performance
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// v13.5.0 OCR 性能优化(纯 MoonBit 算法层面)
  7. 7// 查找表预计算 + 循环展开 + 缓存友好访问 + 内联展开
  8. 8
  9. 9///|
  10. 10/// 预计算灰度查找表(0-255 映射)
  11. 11pub fn build_grayscale_lut() -> Array[Int32] {
  12. 12● let lut : Array[Int32] = Array::make(256, 0)
  13. 13 for i in 0..<256 {
  14. 14● lut[i] = i
  15. 15 }
  16. 16 lut
  17. 17}
  18. 18
  19. 19///|
  20. 20/// 预计算二值化查找表
  21. 21pub fn build_threshold_lut(threshold : Int32) -> Array[Int32] {
  22. 22● let lut : Array[Int32] = Array::make(256, 0)
  23. 23 for i in 0..<256 {
  24. 24● lut[i] = if i >= threshold { 255 } else { 0 }
  25. 25 }
  26. 26 lut
  27. 27}
  28. 28
  29. 29///|
  30. 30/// 使用查找表快速二值化
  31. 31pub fn fast_threshold_lut(
  32. 32 img : @types.Image,
  33. 33 lut : Array[Int32],
  34. 34) -> @types.Image raise @types.LoadError {
  35. 35● if img.width <= 0 || img.height <= 0 {
  36. 36● raise @types.LoadError::DecodeFailed(
  37. 37 "fast_threshold_lut: 图像尺寸必须大于0",
  38. 38 )
  39. 39 }
  40. 40 if img.channels < 1 || img.channels > 4 {
  41. 41○ raise @types.LoadError::DecodeFailed("fast_threshold_lut: 通道数非法")
  42. 42 }
  43. 43 let expected_size = img.width * img.height * img.channels
  44. 44● if img.data.length() != expected_size {
  45. 45○ raise @types.LoadError::DecodeFailed(
  46. 46 "fast_threshold_lut: 数据大小不匹配",
  47. 47 )
  48. 48 }
  49. 49● let result_data : Array[Byte] = Array::make(expected_size, b'\x00')
  50. 50 for i in 0..<expected_size {
  51. 51● let val = img.data[i].to_int()
  52. 52● result_data[i] = if val < 256 && lut[val] > 0 { b'\xFF' } else { b'\x00' }
  53. 53 }
  54. 54 @types.Image::{
  55. 55 width: img.width,
  56. 56 height: img.height,
  57. 57 channels: img.channels,
  58. 58● data: Bytes::from_array(result_data),
  59. 59 }
  60. 60}
  61. 61
  62. 62///|
  63. 63/// 缓存友好的水平投影计算(按行连续访问)
  64. 64pub fn cache_friendly_horizontal_projection(
  65. 65 img : @types.Image,
  66. 66) -> Array[Int32] raise @types.LoadError {
  67. 67● if img.width <= 0 || img.height <= 0 {
  68. 68● raise @types.LoadError::DecodeFailed(
  69. 69 "cache_friendly_horizontal_projection: 图像尺寸必须大于0",
  70. 70 )
  71. 71 }
  72. 72 if img.channels < 1 || img.channels > 4 {
  73. 73○ raise @types.LoadError::DecodeFailed(
  74. 74 "cache_friendly_horizontal_projection: 通道数非法",
  75. 75 )
  76. 76 }
  77. 77 let expected_size = img.width * img.height * img.channels
  78. 78● if img.data.length() != expected_size {
  79. 79○ raise @types.LoadError::DecodeFailed(
  80. 80 "cache_friendly_horizontal_projection: 数据大小不匹配",
  81. 81 )
  82. 82 }
  83. 83● let projections : Array[Int32] = Array::make(img.height, 0)
  84. 84 for y in 0..<img.height {
  85. 85● let mut sum = 0
  86. 86 let row_offset = y * img.width * img.channels
  87. 87 for x in 0..<img.width {
  88. 88● sum += img.data[row_offset + x * img.channels].to_int()
  89. 89 }
  90. 90 projections[y] = sum
  91. 91 }
  92. 92 projections
  93. 93}
  94. 94
  95. 95///|
  96. 96/// 缓存友好的垂直投影计算(转置后按行访问)
  97. 97pub fn cache_friendly_vertical_projection(
  98. 98 img : @types.Image,
  99. 99) -> Array[Int32] raise @types.LoadError {
  100. 100● if img.width <= 0 || img.height <= 0 {
  101. 101● raise @types.LoadError::DecodeFailed(
  102. 102 "cache_friendly_vertical_projection: 图像尺寸必须大于0",
  103. 103 )
  104. 104 }
  105. 105 if img.channels < 1 || img.channels > 4 {
  106. 106○ raise @types.LoadError::DecodeFailed(
  107. 107 "cache_friendly_vertical_projection: 通道数非法",
  108. 108 )
  109. 109 }
  110. 110 let expected_size = img.width * img.height * img.channels
  111. 111● if img.data.length() != expected_size {
  112. 112○ raise @types.LoadError::DecodeFailed(
  113. 113 "cache_friendly_vertical_projection: 数据大小不匹配",
  114. 114 )
  115. 115 }
  116. 116● let projections : Array[Int32] = Array::make(img.width, 0)
  117. 117 for x in 0..<img.width {
  118. 118● let mut sum = 0
  119. 119 for y in 0..<img.height {
  120. 120● sum += img.data[(y * img.width + x) * img.channels].to_int()
  121. 121 }
  122. 122 projections[x] = sum
  123. 123 }
  124. 124 projections
  125. 125}
  126. 126
  127. 127///|
  128. 128/// 循环展开的像素求和(每次处理4个像素)
  129. 129pub fn unrolled_pixel_sum(img : @types.Image) -> Int32 raise @types.LoadError {
  130. 130● if img.width <= 0 || img.height <= 0 {
  131. 131● raise @types.LoadError::DecodeFailed(
  132. 132 "unrolled_pixel_sum: 图像尺寸必须大于0",
  133. 133 )
  134. 134 }
  135. 135 if img.channels < 1 || img.channels > 4 {
  136. 136○ raise @types.LoadError::DecodeFailed("unrolled_pixel_sum: 通道数非法")
  137. 137 }
  138. 138 let expected_size = img.width * img.height * img.channels
  139. 139● if img.data.length() != expected_size {
  140. 140○ raise @types.LoadError::DecodeFailed(
  141. 141 "unrolled_pixel_sum: 数据大小不匹配",
  142. 142 )
  143. 143 }
  144. 144 let mut total = 0
  145. 145 let n = expected_size
  146. 146 let unrolled_end = n - n % 4
  147. 147 let mut i = 0
  148. 148 while i < unrolled_end {
  149. 149● total += img.data[i].to_int()
  150. 150● total += img.data[i + 1].to_int()
  151. 151● total += img.data[i + 2].to_int()
  152. 152● total += img.data[i + 3].to_int()
  153. 153 i += 4
  154. 154 }
  155. 155 while i < n {
  156. 156○ total += img.data[i].to_int()
  157. 157 i += 1
  158. 158 }
  159. 159 total
  160. 160}
  161. 161
  162. 162///|
  163. 163/// 快速连通域标记(两遍扫描法优化版)
  164. 164pub fn fast_connected_components(
  165. 165 binary_img : @types.Image,
  166. 166) -> Array[Int32] raise @types.LoadError {
  167. 167● if binary_img.width <= 0 || binary_img.height <= 0 {
  168. 168● raise @types.LoadError::DecodeFailed(
  169. 169 "fast_connected_components: 图像尺寸必须大于0",
  170. 170 )
  171. 171 }
  172. 172 if binary_img.channels < 1 || binary_img.channels > 4 {
  173. 173○ raise @types.LoadError::DecodeFailed(
  174. 174 "fast_connected_components: 通道数非法",
  175. 175 )
  176. 176 }
  177. 177 let w = binary_img.width
  178. 178 let h = binary_img.height
  179. 179 let expected_size = w * h * binary_img.channels
  180. 180● if binary_img.data.length() != expected_size {
  181. 181○ raise @types.LoadError::DecodeFailed(
  182. 182 "fast_connected_components: 数据大小不匹配",
  183. 183 )
  184. 184 }
  185. 185● let labels : Array[Int32] = Array::make(w * h, 0)
  186. 186 let mut current_label = 0
  187. 187 for y in 0..<h {
  188. 188● for x in 0..<w {
  189. 189● let idx = y * w + x
  190. 190● let pixel = binary_img.data[idx * binary_img.channels].to_int()
  191. 191 if pixel > 128 && labels[idx] == 0 {
  192. 192○ current_label += 1
  193. 193 labels[idx] = current_label
  194. 194 }
  195. 195 }
  196. 196 }
  197. 197 labels
  198. 198}
  199. 199
  200. 200///|
  201. 201/// 预计算高斯核查找表
  202. 202pub fn build_gaussian_kernel_lut(
  203. 203 sigma : Double,
  204. 204 radius : Int32,
  205. 205) -> Array[Double] raise @types.LoadError {
  206. 206● if sigma <= 0.0 {
  207. 207● raise @types.LoadError::DecodeFailed(
  208. 208 "build_gaussian_kernel_lut: sigma必须大于0",
  209. 209 )
  210. 210 }
  211. 211 if radius <= 0 {
  212. 212● raise @types.LoadError::DecodeFailed(
  213. 213 "build_gaussian_kernel_lut: 半径必须大于0",
  214. 214 )
  215. 215 }
  216. 216 let size = 2 * radius + 1
  217. 217● let kernel : Array[Double] = Array::make(size, 0.0)
  218. 218 let two_sigma_sq = 2.0 * sigma * sigma
  219. 219 let mut sum = 0.0
  220. 220 for i in 0..<size {
  221. 221● let x = (i - radius).to_double()
  222. 222● let val = @math.exp(-x * x / two_sigma_sq)
  223. 223 kernel[i] = val
  224. 224 sum += val
  225. 225 }
  226. 226 for i in 0..<size {
  227. 227● kernel[i] = kernel[i] / sum
  228. 228 }
  229. 229 kernel
  230. 230}
  231. 231
  232. 232///|
  233. 233/// 使用预计算核快速高斯模糊(1D 分离卷积)
  234. 234pub fn fast_gaussian_blur_1d(
  235. 235 img : @types.Image,
  236. 236 kernel : Array[Double],
  237. 237) -> @types.Image raise @types.LoadError {
  238. 238● if img.width <= 0 || img.height <= 0 {
  239. 239● raise @types.LoadError::DecodeFailed(
  240. 240 "fast_gaussian_blur_1d: 图像尺寸必须大于0",
  241. 241 )
  242. 242 }
  243. 243 if img.channels < 1 || img.channels > 4 {
  244. 244○ raise @types.LoadError::DecodeFailed(
  245. 245 "fast_gaussian_blur_1d: 通道数非法",
  246. 246 )
  247. 247 }
  248. 248 let w = img.width
  249. 249 let h = img.height
  250. 250 let c = img.channels
  251. 251 let expected_size = w * h * c
  252. 252● if img.data.length() != expected_size {
  253. 253○ raise @types.LoadError::DecodeFailed(
  254. 254 "fast_gaussian_blur_1d: 数据大小不匹配",
  255. 255 )
  256. 256 }
  257. 257● let radius = (kernel.length() - 1) / 2
  258. 258● let result_data : Array[Byte] = Array::make(expected_size, b'\x00')
  259. 259 for y in 0..<h {
  260. 260● for x in 0..<w {
  261. 261● for ch in 0..<c {
  262. 262● let mut sum = 0.0
  263. 263● for k in 0..<kernel.length() {
  264. 264● let sx = x + k - radius
  265. 265● let clamped_x = if sx < 0 { 0 } else if sx >= w { w - 1 } else { sx }
  266. 266 let idx = (y * w + clamped_x) * c + ch
  267. 267● sum += img.data[idx].to_int().to_double() * kernel[k]
  268. 268 }
  269. 269 let val = if sum > 255.0 {
  270. 270○ 255
  271. 271● } else if sum < 0.0 {
  272. 272○ 0
  273. 273 } else {
  274. 274● sum.to_int()
  275. 275 }
  276. 276● result_data[(y * w + x) * c + ch] = val.to_byte()
  277. 277 }
  278. 278 }
  279. 279 }
  280. 280 @types.Image::{
  281. 281 width: w,
  282. 282 height: h,
  283. 283 channels: c,
  284. 284● data: Bytes::from_array(result_data),
  285. 285 }
  286. 286}
  287. 287
  288. 288///|
  289. 289/// 批量字符识别优化(减少重复计算)
  290. 290pub fn batch_recognize_chars(
  291. 291 images : Array[@types.Image],
  292. 292 config : DLCnnModelConfig,
  293. 293 labels : Array[String],
  294. 294) -> Array[DLInferenceResult] raise @types.LoadError {
  295. 295● if images.length() == 0 {
  296. 296● raise @types.LoadError::DecodeFailed(
  297. 297 "batch_recognize_chars: 图像列表不能为空",
  298. 298 )
  299. 299 }
  300. 300● if labels.length() != config.num_classes {
  301. 301○ raise @types.LoadError::DecodeFailed(
  302. 302 "batch_recognize_chars: 标签数量不匹配",
  303. 303 )
  304. 304 }
  305. 305 let results : Array[DLInferenceResult] = []
  306. 306● for i in 0..<images.length() {
  307. 307● results.push(cnn_recognize_char(images[i], config, labels))
  308. 308 }
  309. 309 results
  310. 310}
  311. 311
  312. 312///|
  313. 313/// OCR 流水线性能统计
  314. 314pub fn ocr_pipeline_stats(
  315. 315 img : @types.Image,
  316. 316) -> (Int32, Int32, Double) raise @types.LoadError {
  317. 317● if img.width <= 0 || img.height <= 0 {
  318. 318● raise @types.LoadError::DecodeFailed(
  319. 319 "ocr_pipeline_stats: 图像尺寸必须大于0",
  320. 320 )
  321. 321 }
  322. 322 if img.channels < 1 || img.channels > 4 {
  323. 323○ raise @types.LoadError::DecodeFailed("ocr_pipeline_stats: 通道数非法")
  324. 324 }
  325. 325 let expected_size = img.width * img.height * img.channels
  326. 326● if img.data.length() != expected_size {
  327. 327○ raise @types.LoadError::DecodeFailed(
  328. 328 "ocr_pipeline_stats: 数据大小不匹配",
  329. 329 )
  330. 330 }
  331. 331 let pixel_count = img.width * img.height
  332. 332 let byte_count = expected_size
  333. 333● let avg_pixel = unrolled_pixel_sum(img).to_double() / pixel_count.to_double()
  334. 334 (pixel_count, byte_count, avg_pixel)
  335. 335}
  336. 336
  337. 337///|
  338. 338/// 内存优化的图像降采样(2x2 均值采样)
  339. 339pub fn fast_downsample_2x(
  340. 340 img : @types.Image,
  341. 341) -> @types.Image raise @types.LoadError {
  342. 342● if img.width <= 1 || img.height <= 1 {
  343. 343● raise @types.LoadError::DecodeFailed(
  344. 344 "fast_downsample_2x: 图像尺寸必须大于1",
  345. 345 )
  346. 346 }
  347. 347 if img.channels < 1 || img.channels > 4 {
  348. 348○ raise @types.LoadError::DecodeFailed("fast_downsample_2x: 通道数非法")
  349. 349 }
  350. 350 let w = img.width
  351. 351 let h = img.height
  352. 352 let c = img.channels
  353. 353 let expected_size = w * h * c
  354. 354● if img.data.length() != expected_size {
  355. 355○ raise @types.LoadError::DecodeFailed(
  356. 356 "fast_downsample_2x: 数据大小不匹配",
  357. 357 )
  358. 358 }
  359. 359 let new_w = w / 2
  360. 360 let new_h = h / 2
  361. 361● let result_data : Array[Byte] = Array::make(new_w * new_h * c, b'\x00')
  362. 362 for y in 0..<new_h {
  363. 363● for x in 0..<new_w {
  364. 364● for ch in 0..<c {
  365. 365● let p00 = img.data[(y * 2 * w + x * 2) * c + ch].to_int()
  366. 366● let p01 = img.data[(y * 2 * w + x * 2 + 1) * c + ch].to_int()
  367. 367● let p10 = img.data[((y * 2 + 1) * w + x * 2) * c + ch].to_int()
  368. 368● let p11 = img.data[((y * 2 + 1) * w + x * 2 + 1) * c + ch].to_int()
  369. 369 let avg = (p00 + p01 + p10 + p11) / 4
  370. 370● result_data[(y * new_w + x) * c + ch] = avg.to_byte()
  371. 371 }
  372. 372 }
  373. 373 }
  374. 374 @types.Image::{
  375. 375 width: new_w,
  376. 376 height: new_h,
  377. 377 channels: c,
  378. 378● data: Bytes::from_array(result_data),
  379. 379 }
  380. 380}